实例讲解连接sqlserver2000与2005 - sqljdbc.jar 驱动

一、文件准备与创建,其中sqljdbc.jar 的下载:http://download.csdn.net/source/2474822

实例讲解连接sqlserver2000与2005 - sqljdbc.jar 驱动_第1张图片

 

二、测试sql server 200 所有代码及执行,按创建顺序

1、sqljdbc.jar

2、testSqlServer2000.java

 import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.sql.*; public class testSqlServer2000 { //driver string static String JDBCDriver="com.microsoft.sqlserver.jdbc.SQLServerDriver"; //connection string String JDBCConnection="jdbc:sqlserver://data:1433;DatabaseName=master"; String userName = "sa"; String userPwd = "sa"; Connection conn=null; ResultSet rs=null; public testSqlServer2000() { try{ Class.forName(JDBCDriver);//load Driver } catch(java.lang.ClassNotFoundException e) { System.err.println("Dbconnection ():"+e.getMessage());//catch exception } } public Connection connect() { try{//get connection conn=DriverManager.getConnection(JDBCConnection, userName, userPwd); System.out.println("connect successful"); } catch(SQLException ex) { System.err.println("connection():"+ex.getMessage()); } return conn; } public void close() { try { if(!this.conn.isClosed()) { this.conn.close(); System.out.println("close successful"); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub testSqlServer2000 cdb=new testSqlServer2000(); cdb.connect(); Statement stmt; ResultSet rs; try { stmt = cdb.conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); rs = stmt.executeQuery("select * from spt_monitor"); while(rs.next()) { String s=rs.getString(1); System.out.println(s); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } cdb.close(); } }

3、javac2000.bat

set path="C:/Program Files/Java/jdk1.6.0_11/bin; C:/Windows;C:/Windows/Command" set classpath=". ;C:/Program Files/Java/jdk1.6.0_11/lib/tools.jar" "C:/Program Files/Java/jdk1.6.0_11/bin/javac" testSqlServer2000.java pause

执行结果

I:/workspace/java/testSqlServer/sqljdbc>set path="C:/Program Files/Java/jdk1.6.0 _11/bin; C:/Windows;C:/Windows/Command" I:/workspace/java/testSqlServer/sqljdbc>set classpath=". ;C:/Program Files/Java/ jdk1.6.0_11/lib/tools.jar" I:/workspace/java/testSqlServer/sqljdbc>"C:/Program Files/Java/jdk1.6.0_11/bin/j avac" testSqlServer2000.java I:/workspace/java/testSqlServer/sqljdbc>pause 请按任意键继续. . .

4、java2000.bat

@set path="C:/Program Files/Java/jdk1.6.0_11/bin; C:/Windows;C:/Windows/Command" @set classpath=". ;C:/Program Files/Java/jdk1.6.0_11/lib/tools.jar;sqljdbc.jar;" "C:/Program Files/Java/jdk1.6.0_11/bin/java" testSqlServer2000 pause

执行结果

I:/workspace/java/testSqlServer/sqljdbc>"C:/Program Files/Java/jdk1.6.0_11/bin/j ava" testSqlServer2000 connect successful 2000-08-06 01:30:31.687 close successful I:/workspace/java/testSqlServer/sqljdbc>pause 请按任意键继续. . .

 

三、测试sql server 2005

1、testSqlServer2005.java

import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.sql.*; public class testSqlServer2005 { //driver string static String JDBCDriver="com.microsoft.sqlserver.jdbc.SQLServerDriver"; //connection string String JDBCConnection="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=master"; String userName = "sa"; String userPwd = "000000"; Connection conn=null; ResultSet rs=null; public testSqlServer2005() { try{ Class.forName(JDBCDriver);//load Driver } catch(java.lang.ClassNotFoundException e) { System.err.println("Dbconnection ():"+e.getMessage());//catch exception } } public Connection connect() { try{//get connection conn=DriverManager.getConnection(JDBCConnection, userName, userPwd); System.out.println("connect successful"); } catch(SQLException ex) { System.err.println("connection():"+ex.getMessage()); } return conn; } public void close() { try { if(!this.conn.isClosed()) { this.conn.close(); System.out.println("close successful"); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub testSqlServer2005 cdb=new testSqlServer2005(); cdb.connect(); Statement stmt; ResultSet rs; try { stmt = cdb.conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); rs = stmt.executeQuery("select * from spt_monitor"); while(rs.next()) { String s=rs.getString(1); System.out.println(s); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } cdb.close(); } }

2、javac2005

set path="C:/Program Files/Java/jdk1.6.0_11/bin; C:/Windows;C:/Windows/Command" set classpath=". ;C:/Program Files/Java/jdk1.6.0_11/lib/tools.jar" "C:/Program Files/Java/jdk1.6.0_11/bin/javac" testSqlServer2005.java pause

3、java2005

@set path="C:/Program Files/Java/jdk1.6.0_11/bin; C:/Windows;C:/Windows/Command" @set classpath=". ;C:/Program Files/Java/jdk1.6.0_11/lib/tools.jar;sqljdbc.jar" "C:/Program Files/Java/jdk1.6.0_11/bin/java" testSqlServer2000 pause

你可能感兴趣的:(sql,exception,String,server,sqlserver,Path,scroll)