SQL Server 2000 是当今网络编程中使用的比较多的一个数据库系统,而java则是流行的网络编程语言,在网络编程中涉及的比较多也比较重要的就是数据库问题,Java自身提供了对各类主流数据库系统的支持,通过提供Java.SQL 库,提供了一个统一的接口,使得可以在Java环境下不必对程序作大规模的修改,只要更改相应的驱动程序,即可实现对各类数据库的操作,从而提高软件的生存周期和降低软件的开发成本和维护费用,在这种情况下SQL Server 2000 和Java 的融合问题就显得比较重要了,本文简要地阐述一下SQL Server 2000 和jdk的融合问题。
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我们便可进行数据库编程,事实则不然,首先我们看下边的代码:
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...
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...