Java connection SQL Server database code

Source: Internet
Author: User
Tags tostring

Link Database Code

Import java.sql.*;

public class Connect

{

Static Connection Conn;

static Statement SQL;

Static ResultSet Res;

Static String drivername= "Com.microsoft.sqlserver.jdbc.SQLServerDriver";

Static String dburl= "Jdbc:sqlserver://127.0.0.1:1433;integratedsecurity=true;"

dburl=dburl+ "Databasename= (here is the name of the database you want to connect to)";

public static Connection GetConnect ()

{

try{

Class.forName (drivername);

Conn=drivermanager.getconnection (Dburl);

System.out.println ("SQLSERVER2008 Connection success!");

}catch (Exception e)

{

E.printstacktrace ();

}

Return conn;

}

Execute the statement of adding, deleting and modifying

The public static int exesqloper (String strSQL)//method is called directly in other functions.

{

int intreturn=0;

try{

Conn=getconnect ();

Sql=conn.createstatement ();

Intreturn=sql.executeupdate (strSQL);

Conn.close ();

}catch (Exception e)

{

System.out.println (E.tostring ());

}

return intreturn;

}

Executing a SELECT statement

public static ResultSet Exesqlquery (String strSQL)//is also directly invoked using the

{

try{

Conn=getconnect ();

Sql=conn.createstatement ();

Res=sql.executequery (strSQL);

}catch (Exception e)

{

System.out.println (E.tostring ());

}

return res;

}

Main function

public static void Main (String arges[])

{

GetConnect ();

}

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.