SQL Server 2000 Driver for JDBC Service Pack 3 安装测试方法
1.安装:sql server 2000 driver for jdbc service pack 3
下载安装jdbc sp3
里面的安装包
按照提示安装可以了.成功后有三个文件要使用:
c:/program files/microsoft sql server 2000 driver for jdbc/lib/msbase.jar
c:/program files/microsoft sql server 2000 driver for jdbc/lib/msutil.jar
c:/program files/microsoft sql server 2000 driver for jdbc/lib/mssqlserver.jar
2.测试代码
新建类文件connect.java.
package test; import java.*; import java.sql.driver; public class connect{ private java.sql.connection con = null; private final string url = "jdbc:microsoft:sqlserver://"; private final string servername= "localhost"; private final string portnumber = "1433"; private final string databasename= "dbtest"; private final string username = "sa"; private final string password = "123456"; // informs the driver to use server a side-cursor, // which permits more than one active statement // on a connection. private final string selectmethod = "cursor"; // constructor public connect(){} private string getconnectionurl(){ return url+servername+":"+portnumber+";databasename="+databasename+";selectmethod="+selectmethod+";"; } private java.sql.connection getconnection(){ try{ class.forname("com.microsoft.jdbc.sqlserver.sqlserverdriver"); con = java.sql.drivermanager.getconnection(getconnectionurl(),username,password); if(con!=null) system.out.println("connection successful!"); }catch(exception e){ e.printstacktrace(); system.out.println("error trace in getconnection() : " + e.getmessage()); } return con; } /* display the driver properties, database details */ public void displaydbproperties(){ java.sql.databasemetadata dm = null; java.sql.resultset rs = null; try{ con= this.getconnection(); if(con!=null){ dm = con.getmetadata(); system.out.println("driver information"); system.out.println("/tdriver name: "+ dm.getdrivername()); system.out.println("/tdriver version: "+ dm.getdriverversion ()); system.out.println("/ndatabase information "); system.out.println("/tdatabase name: "+ dm.getdatabaseproductname()); system.out.println("/tdatabase version: "+ dm.getdatabaseproductversion()); system.out.println("avalilable catalogs "); rs = dm.getcatalogs(); while(rs.next()){ system.out.println("/tcatalog: "+ rs.getstring(1)); } rs.close(); rs = null; closeconnection(); }else system.out.println("error: no active connection"); }catch(exception e){ e.printstacktrace(); } dm=null; } private void closeconnection(){ try{ if(con!=null) con.close(); con=null; }catch(exception e){ e.printstacktrace(); } } public static void main(string[] args) throws exception { connect mydbtest = new connect(); mydbtest.displaydbproperties(); } }
代码来源:
http://support.microsoft.com/default.aspx?scid=kb;zh-cn;313100
------------------------------------------
成功后控制台输出:
connection successful!
driver information
driver name: sqlserver
driver version: 2.2.0040
database information
database name: microsoft sql server
database version: microsoft sql server 2000 - 8.00.760 (intel x86)
dec 17 2002 14:22:05
copyright (c) 1988-2003 microsoft corporation
enterprise edition on windows nt 5.2 (build 3790: )
avalilable catalogs
catalog: dbtest
...........
3.问题:
在测试中控制台老输出下面的错误!
找资料找了很久.都说把jdbc安装后的三个jar文件的路径放进环境变量里可以了但我试了不行的!
java.lang.classnotfoundexception: com.microsoft.jdbc.sqlserver.sqlserverdriver
.........
error trace in getconnection() : com.microsoft.jdbc.sqlserver.sqlserverdriver
error: no active connection
请教了别人才找到办法:
包资源管理器-->包名右键"构建路径"-->配置构建路径-->java构建路径-->库-->添加外部jar
把那三个jar选择进去就可以了.
添加后三个jdbc文件就有了.
上一篇: Android实现摇一摇功能
下一篇: .NET中弹出对话框的方法汇总