1、引言本文介绍如何利用SQLServer2000的JDBC驱动程序连接SQLServer2000,应用在JSP页面时数据库链接有问题从源头排查。2、现象描述JSP页面里面,连接数据库就有问题,驱动程序初始化就有问题。3、处理过程1)SQLServer2000数据库服务器(1)启动SQLServer2000数据库服务器(2)设置用户“sa”的密码输入密码为“6407”。2)连接方式(1)、命令行方式a、将SQLServer2000的JDBC驱动程序拷贝到某一路径下(如:C:\j2sdk1.4.0\lib)msbase.jar、mssqlserver.jar、msutil.jarb、在系统环境变量中加人对上述三个文件的路径说明path=c:\j2sdk1.4.0\bin;setclasspath=.;C:\j2sdk1.4.0\lib\msbase.jar;C:\j2sdk1.4.0\lib\mssqlserver.jar;C:\j2sdk1.4.0\lib\msutil.jar;c、编写连接数据库的URL地址Stringurl=jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs;StringDBDriver=com.microsoft.jdbc.sqlserver.SQLServerDriver;d、程序代码为:importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;importjava.sql.*;publicclassSqlServerJdbcextendsJFrameimplementsWindowListener{String[]columnNames={};Object[][]dataRows;/*连接oracle数据库时Stringurl=jdbc:oracle:thin:@10.172.0.107:1521:DBName;StringDBDriver=oracle.jdbc.driver.OracleDriver;*//*连接SqlServer数据库时,数据库在远程主机时Stringurl=jdbc:microsoft:sqlserver://10.172.0.105:1433;DatabaseName=pubs;数据库在本地主机时(中文版的SQLServer的端口号为1433,而英文版的SQLServer的端口号为1036)Stringurl=jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=pubs;*/Stringurl=jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs;StringDBDriver=com.microsoft.jdbc.sqlserver.SQLServerDriver;Connectioncon=null;Statementstmt=null;PreparedStatementpstmt=null;ResultSetrs=null;ResultSetMetaDatametaData;publicSqlServerJdbc(StringwindowTitle){super(windowTitle);intFrameWidth=600,FrameHeight=400;this.setSize(FrameWidth,FrameHeight);this.setLocation((Toolkit.getDefaultToolkit().getScreenSize().width-FrameWidth)/2,(Toolkit.getDefaultToolkit().getScreenSize().height-FrameHeight)/2);this.addWindowListener(this);JPanelcontentPanel=newJPanel();contentPanel.setLayout(newBorderLayout());this.setContentPane(contentPanel);try{Class.forName(DBDriver);}catch(ClassNotFoundExceptione){System.out.println(不能正确地加载驱动程序);}try{con=DriverManager.getConnection(url,sa,);}catch(SQLExceptione){}try{stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);rs=stmt.executeQuery(select*fromauthors);metaData=rs.getMetaData();intnumberOfColumns=metaData.getColumnCount();columnNames=newString[numberOfColumns];for(intcolumn=0;columnnumberOfColumns;column++){columnNames[column]=metaData.getColumnLabel(column+1);}introwCounter=0;if(rs.last()){rowCounter=rs.getRow();}rs.beforeFirst();//将记录指针回退到最开始处,此时必须将Cursor设置为可回滚dataRows=newObject[rowCounter][numberOfColumns];intloopRowCounter=0;while(rs.next()){for(intcolumn=0;columnnumberOfColumns;column++){dataRows[loopRowCounter][column]=rs.getObject(column+1);}loopRowCounter++;}rs.close();}catch(SQLExceptione){System.out.println(SQLState:+e.getSQLState());System.out.println(Messager:+e.getMessage());System.out.println(Vendor:+e.getErrorCode());}JTabletable=newJTable(dataRows,columnNames);JScrollPanejsp=newJScrollPane(table,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);contentPanel.add(jsp,BorderLayout.CENTER);}publicstaticvoidmain(Stringargs[]){SqlServerJdbcmainframe=newSqlServerJdbc(利用JDBCSQLServer2000的驱动程序连接SQLServer2000数据库);mainframe.show();}publicvoidwindowOpened(WindowEventparm1){//TODO:Addyourcodehere}publicvoidwindowClosing(WindowEventparm1){try{con.close();}catch(SQLExceptione){}this.dispose();System.exit(0);}publicvoidwindowClosed(WindowEventparm1){//TODO:Addyourcodehere}publicvoidwindowIconified(WindowEventparm1){//TODO:Addyourcodehere}publicvoidwindowDeiconified(WindowEventparm1){//TODO:Addyourcodehere}publicvoidwindowActivated(WindowEventparm1){//TODO:Addyourcodehere}publicvoidwindowDeactivated(WindowEventparm1){//TODO:Addyourcodehere}}e、执行该程序注意:执行过程中如果出现如下错误,应该修改注册表中的版本号,将1.3修改为1.4。(2)、在JCreator中实现a.编程访问数据库的程序b.将SQLServer2000的JDBC驱动程序加入到JCreator的环境变量中(选择Configuration菜单,然后再Option子菜单)c.选择“编辑”按钮,然后d.再点击“Add”按钮,并选择AddPackage项目。然后将将SQLServer2000的JDBC驱动程序的三个*.jar文件分别加入到JCreator环境中。e.再点击“打开”按钮f.再点击“OK”按钮g.再编译和执行该程序