程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA編程入門知識 >> java連接sql server 2008數據庫代碼

java連接sql server 2008數據庫代碼

編輯:JAVA編程入門知識

Java連接SQLServer 2008數據庫的步驟:

1.到微軟官方下載jdbc 並解壓,得到sqljdbc.jar和sqljdbc4.jar,由於使用的是JDK1.7,所以使用sqljdbc4.jar,

2.復制文件sqljdbc4.jar到jdk目錄\jdk1.7.0\jre\lib\ext下。

配置系統變量classpath 變量路徑 D:\Java\jdk1.7.0\jre\lib\ext\sqljdbc4.jar

測試程序:
代碼如下:

 import java.sql.*;

 public class T1{

       public static void main(String []args)

     {                             

     try{                                                                    

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

      System.out.println("成功加載SQL驅動程序");                              

         }                              

    catch(Exception e){

          System.out.println("找不到SQL驅動程序");                                                            

          }                             

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

   }

}

 

3.開始-〉程序-〉sql  server  2008-〉配置工具-〉SQL Server Configuration Manager。啟動sql  2008服務。點擊  sql  server2008網絡配置節點,並選中”SQLserver的協議“節點。
啟用tcp/ip協議,將IP地址中的IPALL TCP端口設置為1433,然後到服務中重啟 SQLServer(很重要啊。。。)。

測試程序:
代碼如下:

import java.sql.*; public class T2{

     public static void main(String []args)

     {                             

 try{                                                                    

        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");                                                                               System.out.println("成功加載SQL驅動程序");                             

    }                               

   catch(Exception e){

            System.out.println("找不到SQL驅動程序");                                                             

         }                                                            

   try{                                                                    

       Connectioncon=DriverManager.getConnection"jdbc:sqlserver://localhost:1433; 

              DatabaseName=SQLTest",         "sa","123");                                                                                                                                                                                                                                                                                       

        Statement stmt = con.createStatement();                                                                                                                           System.out.println("數據庫連接成功");                                  

     }                                 

    catch(Exception e){                                                                                             

           System.out.println("數據庫連接失敗");                                                                                                                                                }                           

 }

}

注意上述程序中的DatabaseName,需要先在數據庫中創建名為SQLTest的數據庫。然後再開始連接。

好了,經過上述的配置和測試,以後就可以用Java對數據庫進行操作。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved