/*********************************************** /* /*DbTest.Java /* /******************************************* */ import Java.SQL.*; public class DbTest { Connection con; Statement sta; ResultSet rs; String driver; String url; String user; String pwd; public DbTest() { driver = "sun.JDBC.odbc.JDBCOdbcDriver"; url = "JDBC:odbc:store_manager"; user = "share"; pwd = "share"; init(); } public void init() { try{ Class.forName(driver); System.out.println("driver is ok"); con = DriverManager.getConnection (url,user,pwd); System.out.println("conection is ok"); sta = con.createStatement(); rs = sta.executeQuery ("select * from room"); while(rs.next()) System.out.println (rs.getInt("roomNum")); }catch(Exception e) { e.printStackTrace(); } } public static void main (String args []) //自己替换[] { new DbTest(); } } |
运行结果如下:
driver is ok conection is ok 1001 1002 1003 1004 1005 1006 Press any key to continue... |
顺利通过测试
2、我们这次不通过odbc桥来操作数据库,我们采用SQL Server driver 来实现对SQLServer数据库的操作,这将是我们这篇文章的重点,因为JDBC-odbc桥是一种常见的操作windows系统数据库的常用方法,但它存在的缺点很多,所以现在很多开发者都侧重于使用SQLServer driver来操作,在这里我们通过一步步的调试,来加深读者对这种连接的理解。
在通常的理解下,只要我们装了SQLServer driver for JDBC我们便可进行数据库编程,事实则不然,首先我们看下边的代码:
/*********************************************** /* /*DbTest.Java /* /******************************************* */ import Java.SQL.*; public class DbTest { Connection con; Statement sta; ResultSet rs; String driver; String url; String user; String pwd; public DbTest() { driver = "com.microsoft.JDBC.SQLServer. SQLServerDriver";; url = "JDBC:microsoft:SQLServer: //localhost:1433;DatabaseName =StoreManager"; user = "sa"; pwd = "potsmart10"; init(); } public void init() { try{ Class.forName(driver); System.out.println("driver is ok"); con = DriverManager.getConnection (url,user,pwd); System.out.println("conection is ok"); sta = con.createStatement(); rs = sta.executeQuery ("select * from room"); while(rs.next()) System.out.println (rs.getInt("roomNum")); }catch(Exception e) { e.printStackTrace(); } } public static void main (String args []) //自己替换[] { new DbTest(); } } |
这段代码跟上变得代码是一样的,差别在于驱动,还有url,这是在使用SQLServer driver for JDBC 中遇到的困惑
按道理讲,上边这段代码应该没错,可首先我们来看一下,如果SQLser服务器没有升级到sp3(在使用JDBC时,如果系统是xp或者2003务必要把SQLServer 升级到sp3),我们看看运行结果:
driver is ok Java.SQL.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Error establis hing socket. at com.microsoft.JDBC.base. BaseExceptions.createException (Unknown Source ) at com.microsoft.JDBC.base. BaseExceptions.getException (Unknown Source) at com.microsoft.JDBC.base. BaseExceptions.getException (Unknown Source) at com.microsoft.JDBC.SQLServer. tds.TDSConnection.<init> (Unknown Source) at com.microsoft.JDBC.SQLServer. SQLServerImplConnection.open (Unknown Sou rce) at com.microsoft.JDBC.base.BaseConnection. getNewImplConnection(Unknown S ource) at com.microsoft.JDBC.base. BaseConnection.open(Unknown Source) at com.microsoft.JDBC.base. BaseDriver.connect(Unknown Source) at Java.SQL.DriverManager. getConnection(DriverManager.Java:523) at Java.SQL.DriverManager. getConnection(DriverManager.Java:171) at DbTest.init(DbTest.Java:32) at DbTest.<init>(DbTest.Java:25) at DbTest.main(DbTest.Java:46) Press any key to continue... |
出现上边错误的主要原因是默认的数据库服务器端口1433没有打开,无法直接连接。如果升级到sp3则这个问题可以结决,我们再来看看升级之后,程序运行的结果:
driver is ok conection is ok Java.SQL.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC] [SQLServer] 对象名 'room' 无效。 at com.microsoft.JDBC.base. BaseExceptions.createException (Unknown Source ) at com.microsoft.JDBC.base. BaseExceptions.getException (Unknown Source) at com.microsoft.JDBC.SQLServer. tds.TDSRequest.processErrorToken (Unknown Source) at com.microsoft.JDBC.SQLServer.tds. TDSRequest.processReplyToken(Unknown Source) at com.microsoft.JDBC.SQLServer.tds. TDSExecuteRequest.processReplyToken ( Unknown Source) at com.microsoft.JDBC.SQLServer.tds. TDSRequest.processReply(Unknown Sour ce) at com.microsoft.JDBC.SQLServer. SQLServerImplStatement.getNextResultType (Unknown Source) at com.microsoft.JDBC.base. BaseStatement.commonTransitionToState (Unknown Source) at com.microsoft.JDBC.base. BaseStatement.postImplExecu te(Unknown Source) at com.microsoft.JDBC.base. BaseStatement.commonExecute (Unknown Source) at com.microsoft.JDBC.base. BaseStatement.executeQueryInternal (Unknown So urce) at com.microsoft.JDBC.base. BaseStatement.executeQuery (Unknown Source) at DbTest.init(DbTest.Java:35) at DbTest.<init>(DbTest.Java:25) at DbTest.main(DbTest.Java:46) Press any key to continue... |
在这儿,用户已经登陆上去,但是却不能访问里边的数据表,出现这个问题的原因在于sa用户为系统用户,它虽然能够登陆数据库,但是storeManager数据库里边却没有这个用户的访问权限,所以,我们现在为这个数据库重新建立一个用户
share ,建立过程如下:在storeManager数据库中选重用户 ---〉新建用户 -- 〉名称选择(这一步中有两个关键点 1、身份验证选SQL身份验证,默认数据库选StoreManager)-〉建立新教色share ,此时更改程序,将用户登陆名和密码修改一下,重新运行程序:
driver is ok conection is ok 1001 1002 1003 1004 1005 1006 Press any key to continue... |
这次顺利通过测试
总结:SQL Serve 和JDBC 的融合问题,关键涉及到Sp3补丁(端口开放)还有用户问题,解决这两个问题之后,剩余的便是SQL Server操作问题了,还有一点在远程操作的时候,要把SQL Server 组设置一下,在安全性里边亦将身份验证更改为SQL Serve 验证即可。