How to connect to SQL Server using JDBC driver in Java under win7

Source: Internet
Author: User
Tags how to connect to sql server

Http://hi.baidu.com/fpbest/blog/item/c9c7ce13df11e0e0c3ce790b.html

 

Step 1: download and installMicrosoft SQL Server 2000 Service Pack 4, That isSQL2000OfSP4Patch

The address is as follows:

Http://www.microsoft.com/downloads/zh-cn/details.aspx? Familyid = 8e2dfc8d-c20e-4446-99a9-b7f0127f8bc5 & displaylang = ZH-CN

Step 2: DownloadJDBCDriver,Decompress the package to any location ,:

Http://www.microsoft.com/downloads/zh-cn/details.aspx? Familyid = a737000d-68d0-4531-b65d-da0f2a735707

 

Step 3: CreateJavaProject, and thenWindows> preferences> JAVA> installed jres

 

 

 

 

 

 

Use the followingCodeTest:

 

Import java. SQL. connection;

Import java. SQL. drivermanager;

Import java. SQL. resultset;

Import java. SQL. statement;

 

Public class connectaccess {

 

 

Public static void main (string [] ARGs ){

Try {

Class. forname ("com. Microsoft. sqlserver. JDBC. sqlserverdriver ");

Connection con = drivermanager. getconnection ("JDBC: sqlserver: // localhost: 1433; databasename = pubs", "sa", "123456 ");

Statement stmt = con. createstatement ();

Resultset Rs into stmt.exe cutequery ("select fname, minit, lname from employee ");

While (Rs. Next ()){

System. Out. Print ("\ t fname:" + Rs. getstring (1 ));

System. Out. Print ("\ t minit:" + Rs. getstring (2 ));

System. Out. Print ("\ t lname:" + Rs. getstring (3 ));

System. Out. println ();

}

Rs. Close ();

Stmt. Close ();

Con. Close ();

} Catch (exception e ){

E. printstacktrace ();

}

}

 

}

Success! Haha !!!

Run the code before running it.SQLServer, don't forget it !!!

 

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.