JAVA connection to SQLServer Database

Source: Internet
Author: User

Pay attention to the following points:

1. Import sqljdbd to the Project

2. If Tomcat is used, msbase, mssqlservr, and msutil jar packages must be added to the lib directory.

3. If you write the JAVA client directly, you must add the Three JAR files to the project's Libraries.

1 String driver = "com. microsoft. jdbc. sqlserver. SQLServerDriver ";
2 String url = "jdbc: microsoft: sqlserver: // 192.168.0.20: 1433; DatabaseName = fqit ";
3
4 try {
5 Class. forName (driver );
6 Connection conn = DriverManager. getConnection (url, "it11", "luck2000 ");
7 PreparedStatement pstmt = conn. prepareStatement ("select * from dept ");
8 ResultSet rs1_pstmt.exe cuteQuery ();
9 while (rs. next ()){
10 out. println (rs. getString ("dept "));
11 out. println ("<br> ");
12}
13 rs. close ();
14 pstmt. close ();
15 conn. close ();
16} catch (ClassNotFoundException e ){
17 e. printStackTrace ();
18} catch (SQLException e ){
19 e. printStackTrace ();
20}

 

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.