日期:2014-05-16 浏览次数:20443 次
首先下载JDBC:下载地址:http://www.microsoft.com/zh-cn/download/details.aspx?id=21599
下载 完成后,是个exe文件,点击运行,会提示你选择解压目录:如下图:
点击Browse,随便选择一个文件夹就好
解压完成后,进入 <你解压到得目录>\sqljdbc_3.0\chs,里边有两个我们需要的东东
一个是:sqljdbc.jar,另外一个是sqljdbc4.jar
这两个有什么区别我也没研究,当我在eclipse中新建了一个项目,适用第一个包时,报错了,说是什么该包不支持jre1.7,让我用带4的那个,我用了就没报错了。
接下来是在java代码中用两种方式连接sqlserver2008数据库,一种是sa身份验证模式,另外一种是混合身份验证模式:
第一种:sa身份验证模式,用下边java代码的url
Java代码:
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 connectionUrl = "jdbc:sqlserver://localhost:1433;" + "databaseName=AdventureWorks;integratedSecurity=true;"; 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(url2); 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) { } } } }
代码很简单我就不多说了,这个代码的来源是刚才你解压的文件的 sqljdbc_3.0\chs\help\samples\connections 目录下connectURL.java文件,整体基本没动,就是改了改url和表名。
如果用集成模式可能会出现的问题:
如下:
警告: Failed to load the sqljdbc_auth.dll cause :- no sqljdbc_auth in java.library.path com.microsoft.sqlserver.jdbc.SQLServerException: 没有为集成身份验证配置驱动程序。 at com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(SQLServerConnection.java:1352) at com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerConnection.java:2329) at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:1905) at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$000(SQLServerConnection.java:41) at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:1893) at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:4575) at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1400) at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1045) at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:817) at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:700) at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:842) at java.sql.DriverManager.getConnection(Unknown Source) at java.sql.DriverManager.getConnection(Unknown Source) at Test.main(Test.java:26) Caused by: java.lang.UnsatisfiedLinkError: no sqljdbc_auth in java.library.path at java.