Java connection SQL Server 2008 database code _java

Source: Internet
Author: User
Tags sql 2008

Java connection to SQL Server 2008 database:

1. To Microsoft official download JDBC and decompression, get Sqljdbc.jar and Sqljdbc4.jar, because the use is JDK1.7, so use Sqljdbc4.jar,

2. Copy the file Sqljdbc4.jar to the JDK directory \jdk1.7.0\jre\lib\ext.

Configure system variable classpath variable path D:\Java\jdk1.7.0\jre\lib\ext\sqljdbc4.jar

Test program:

Copy Code code as follows:

Import java.sql.*;

public class t1{

public static void Main (String []args)

{

try{

Class.forName ("Com.microsoft.sqlserver.jdbc.SQLServerDriver");

System.out.println ("Successfully loaded SQL Driver");

}

catch (Exception e) {

SYSTEM.OUT.PRINTLN ("SQL driver not Found");

}

System.out.println ("Hello huuuu!!");

}

}


3. Start-〉 program-〉sql server 2008-〉 Configuration Tool-〉sql server Configuration Manager. Start the SQL 2008 service. Click on the SQL server2008 Network Configuration node and select the SQL Server Protocols node.
Enable the TCP/IP protocol, set the Ipall TCP port in the IP address to 1433, and then restart SQL Server in the service (very important ...) )。

Test program:

Copy Code code as follows:

Import java.sql.*; public class t2{

public static void Main (String []args)

{

 try{                                                                       

        class.forname ("Com.microsoft.sqlserver.jdbc.SQLServerDriver");                                                                                    System.out.println ("Successfully loaded SQL Driver");                               

}

catch (Exception e) {

SYSTEM.OUT.PRINTLN ("SQL driver not Found");

}

try{

       connectioncon=drivermanager.getconnection "Jdbc:sqlserver://localhost :1433; 

              databasename=sqltest ",          "sa", "123");                                                                                                                                                                                                                                                                                                      

 

        Statement stmt = con.createstatement ();                                                                                                                                  System.out.println ("database connection succeeded");                                    

}

catch (Exception e) {

           System.out.println ("Database connection failed");                                                                                                                                                       }                             

}

}


Note the databasename in the above program, you need to first create a database named Sqltest in the database. And then start the connection again.

OK, after the above configuration and testing, you can use Java to operate the database.

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.