Java connection SQL Server database code

Source: Internet
Author: User

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

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.