Java串連並操作SQLServer資料庫

來源:互聯網
上載者:User

標籤:int   log   伺服器   成功   declare   mss   html   tco   window   

本人只需在項目中引入sqljdbc4.jar 包即可

-----------------------------------------

在JAVA中如何串連SQL Server資料庫 - hanghangde的部落格 - 部落格頻道 - CSDN.NET
http://blog.csdn.net/hanghangde/article/details/50463658

這篇文章說得很好,有連結 2000和 2008的伺服器版本;

Java串連SqlServer2008資料庫
首先下載JDBC::http://www.microsoft.com/zh-cn/download/details.aspx?id=21599
下載 完成後,是個exe檔案,點擊運行,會提示你選擇解壓目錄.
解壓完成後,進入 <你解壓到得目錄>\sqljdbc_3.0\chs,裡邊有兩個我們需要的東東
一個是:sqljdbc.jar,另外一個是sqljdbc4.jar
這裡使用sqljdbc4.jar
首先配置sa身分識別驗證:
由於安裝sqlServer2008時是以windows身分識別驗證安裝的,並沒有為sqlServer2008添加sqlServer身份使用者,因此首先添加使用者:
開啟Microsoft SQL Server Managerment Studio並以windows驗證方式登入,左側的物件總管->安全性->登入名稱,右擊sa->屬性,為sa使用者添加密碼,選擇sqlServer身分識別驗證,在"狀態"選項中授予串連到資料庫和登入啟用.右擊物件總管的根節點,選擇屬性->安全性->sqlServer和windows身分識別驗證模式,這樣就為sql server 2008建立了以sql server身分識別驗證的使用者sa.
在java代碼中用兩種方式串連sqlserver2008資料庫,一種是sa身分識別驗證模式,另外一種是混合身分識別驗證模式:
第一種:sa身分識別驗證模式,用下邊java代碼的url

import java.sql.Connection;    import java.sql.DriverManager;    import java.sql.ResultSet;    import java.sql.Statement;        public class Test {            public static void main(String args[]) {            // Create a variable for the connection string.              String url = "jdbc:sqlserver://127.0.0.1:1368;databaseName=mydb;user=sa;password=qiaoning";//sa身份串連                String url2 = "jdbc:sqlserver://127.0.0.1:1368;databaseName=mydb;integratedSecurity=true;";//windows整合模式串連                // Declare the JDBC objects.            Connection con = null;            Statement stmt = null;            ResultSet rs = null;                try {                // Establish the connection.                System.out.println("begin.");                Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");                con = DriverManager.getConnection(url);                System.out.println("end.");                    // Create and execute an SQL statement that returns some data.                String SQL = "SELECT TOP 10 * FROM aud_t_basis";                stmt = con.createStatement();                rs = stmt.executeQuery(SQL);                    // Iterate through the data in the result set and display it.                while (rs.next()) {                    System.out.println(rs.getString(4) + " " + rs.getString(6));                }            }                // Handle any errors that may have occurred.            catch (Exception e) {                e.printStackTrace();            }                finally {                if (rs != null)                    try {                        rs.close();                    } catch (Exception e) {                    }                if (stmt != null)                    try {                        stmt.close();                    } catch (Exception e) {                    }                if (con != null)                    try {                        con.close();                    } catch (Exception e) {                    }            }        }    }   
package pkg;import java.sql.*;public class QueryDemo {    public static void main(String[] args){        String JDriver="com.microsoft.sqlserver.jdbc.SQLServerDriver";        String url="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=GpsDB";        String user="sa";        String password="123456";        try{            Class.forName(JDriver);// 動態匯入資料庫的驅動            Connection conn=DriverManager.getConnection(url, user, password);// 擷取資料庫連結             String query="SELECT TOP 50 * FROM dbo.DSC_User";// 創造SQL語句             Statement stmt=conn.createStatement();// 執行SQL語句            ResultSet rs=stmt.executeQuery(query);            while(rs.next()){                System.out.println(rs.getString("ID")+"\tName:"+rs.getString(2)+"\t\t[GroupID]:"+rs.getString(3)                +"\t\t[Password]:"+rs.getString(4));            }            System.out.println("查詢資料成功");            rs.close();            stmt.close();            conn.close();        }catch(Exception e){            e.printStackTrace();        }    }}
本人實踐代碼

第二種:混合身分識別驗證模式,用上邊java代碼的url2.
在整合模式下需要如下操作:
找到你剛才的解壓目錄:進入sqljdbc_3.0\chs\auth\x64,我的是64位系統,如果是32位就x86,將一個名為sqljdbc_auth.dll的檔案拷貝到:C:\Windows\System32下,就好了
最後就是sqlserver2008用的是動態連接埠,需要你配置一下:
開啟組態工具->SQLServer組態管理員->SQLServer網路設定->MSSQLSERVER的協議->TCP/IP啟用,把TCP動態連接埠中的0都刪掉,留空;然後把列表拉到最下邊(IPALL),配置一個固定連接埠,以後你串連資料庫就用這個連接埠就可以了:如

這裡我用的是1368,資料庫重啟後,就可以用上面的程式串連了.

========================拓展:

Java串連資料庫(mysql,sqlserver) - 霽源童 - 部落格園
http://www.cnblogs.com/huangxinyuan650/p/5991441.html

java開發相關資料 - keenweiwei的專欄 - 部落格頻道 - CSDN.NET
http://blog.csdn.net/keenweiwei/article/details/7328611

Java串連並操作SQLServer資料庫 - zhangxiaoshuang - 部落格園
http://www.cnblogs.com/zhangxiaoshuang/p/6793561.html

 

mysql-connector-java-5.0.8-bin.jar
sqljdbc.jar
sqljdbc4.jar

Java串連並操作SQLServer資料庫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.