Java connection to SQL server 2008 database code

  • 2020-04-01 01:27:57
  • OfStack

Steps for Java connection to SQLServer 2008 database:

1. Download JDBC from Microsoft and unzip it to get sqljdb44.jar and sqljdb44.jar. Since it USES JDK1.7, sqljdb44.jar is used.

2. Copy the file sqljdb44.jar to 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 procedures:


 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 The driver ");                               
         }                               
    catch(Exception e){
          System.out.println(" Can't find SQL The driver ");                                                             
          }                              
    System.out.println("hello huuuu!!");                          
   }
}

 

3. Start - > program - > sql  Server  2008- > Configuration tool - > SQL Server Configuration Manager. Start the sql  2008 service. Click   Sql  Server2008 network configuration node, and select the "SQLserver protocol" node.
Enable TCP/IP, set the IPALL TCP port in the IP address to 1433, and then restart SQLServer in the service. .

Test procedures:


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 The driver ");                              
    }                                
   catch(Exception e){
            System.out.println(" Can't find SQL The driver ");                                                              
         }                                                             
   try{                                                                     
       Connectioncon=DriverManager.getConnection"jdbc:sqlserver://localhost:1433;  
              DatabaseName=SQLTest",         "sa" . "123");                                                                                                                                                                                                                                                                                        
 
        Statement stmt = con.createStatement();                                                                                                                           System.out.println(" Database connection successful ");                                   
     }                                  
    catch(Exception e){                                                                                              
           System.out.println(" Database connection failed ");                                                                                                                                                }                            
 }
}

Notice the DatabaseName in the program above, you need to first create a database in the database called SQLTest. Then start connecting again.

Now, after the configuration and testing described above, you are ready to operate on the database in Java.


Related articles: