Java connection to SQL server instance 1

Source: Internet
Author: User
Import Java. SQL. * ;

Public   Class DB
{
Public   Static   Void Main (string [] ARGs)
{
Try
{
// Connect to an SQL Server 2005 instanceCode
Class. forname ( " Com. Microsoft. sqlserver. JDBC. sqlserverdriver " ). Newinstance ();
String URL = " JDBC: sqlserver: // localhost: 1433; databasename = demo " ;
// Pubs for your database
String user = " SA " ;
String Password = " Zxm10 " ;
Connection Conn = Drivermanager. getconnection (URL, user, password );
Statement stmt = Conn. createstatement (resultset. type_scroll_sensitive, resultset. concur_updatable );
String SQL = " Select * From t_user " ;
Resultset rs = Stmt.exe cutequery (SQL );
While (Rs. Next ())
{
System. Out. println ( " The first field is: "   + Rs. getstring ( 1 ));
System. Out. println ( " The second field is: "   + Rs. getstring ( 2 ));
}
Rs. Close ();
Stmt. Close ();
Conn. Close ();

/* Connect to the SQL Server 2000 instance code
Class. forname ("com. Microsoft. JDBC. sqlserver. sqlserverdriver"). newinstance ();
String url = "JDBC: Microsoft: sqlserver: // localhost: 1433; databasename = pubs ";
// Pubs for your database
String user = "sa ";
String Password = "hxh ";
Connection conn = drivermanager. getconnection (URL, user, password );
Statement stmt = conn. createstatement (resultset. type_scroll_sensitive, resultset. concur_updatable );
String SQL = "select * from jobs ";
Resultset rs1_stmt.exe cutequery (SQL );
While (Rs. Next ())
{
System. Out. println ("the first field is:" + Rs. getstring (1) + "<br> ");
System. Out. println ("the second field is:" + Rs. getstring (2) + "<br> ");
}
Rs. Close ();
Stmt. Close ();
Conn. Close (); */
System. Out. println ( " Asdfas " );
}
Catch (Exception ex)
{
System. Out. Print ( " Error. "   + Ex. getmessage ());
}
}
}

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.