1、首先要记得sql2000 要打上sp3以上的补丁.(建议用sp4)
2、装上SQL Server 2000 Driver for JDBC Service Pack 3
下载地址:http://www.microsoft.com/downloads/details.aspx?FamilyId=07287B11-0502-461A-B138-2AA54BFDC03A&displaylang=en
3、安装完SQL Server 2000 Driver for JDBC Service Pack 3,它默认安装在C:\Program Files\Microsoft SQL Server 2000 Driver for JDBC 里,在C:\Program Files\Microsoft SQL Server 2000 Driver for JDBC\lib 这个目录里有三个jar文件:
msbase.jar,mssqlserver.jar,msutil.jar,一定要记得把这三个文件copy到你所安装的jdk下的jre目里,这里我以的我的安装配置进行解说:
我的jdk是jdk1.5.0_09,默认安装在:
C:\Program Files\Java 里,在这个C:\Program Files\Java目录下有两个文件夹:
jdk1.5.0_09 目录 和 jre1.5.0_09目录,
前面说的的那三个jar文件一定要copy到jdk1.5.0_09目录里下的jre目录下的lib目录下的ext目录下,我这里的完整路径为:
C:\Program Files\Java\jdk1.5.0_09\jre\lib\ext
4、设置classpath,把这个三个jar文件,加进去
5、sql2000要设置为混合登录模式(windows 和 sql)
-----------------------------------------------------------------------------------------------------------
调用示例JAVA代码 字串1
----------------------------------------------------------------------------------------------------------
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.sql.ResultSet;
//import java.*;
字串4
class CourseAppl {
private Connection conn;
private String url;
private String serverName;
private String portNumber;
private String databaseName;
private String userName;
private String password;
private String sql;
CourseAppl() {
url = "jdbc:microsoft:sqlserver://";
serverName = "localhost";
portNumber = "1433";
databaseName = "test";
userName = "sa";
password = "";
}
private String getConnectionUrl() {
//System.out.println (url + serverName + ":"
// + portNumber + ";databaseName="
//+ databaseName + ";");
return url + serverName + ":"
+ portNumber + ";databaseName="
+ databaseName + ";";
}
private Connection getConnection() {
try {
System.out.println (getConnectionUrl()); 字串4
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
//Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDrive");
conn =
DriverManager.getConnection(getConnectionUrl(),userName,password);
if(conn != null) {
System.out.println ("数据库连接成功!");
}
}catch(Exception ce) {
ce.printStackTrace();
System.out.println ("getConnection() 内部错误跟踪:" +
ce.getMessage());
}
return conn;
}
public void display() {
try {
conn = getConnection();
PreparedStatement pstmt =
conn.prepareStatement("update friends set "+
"salary = ? where name like ?");
pstmt.setInt(1,10000);
pstmt.setString(2,"李四");
pstmt.executeUpdate();
System.out.println ("李四的记录已经被更新!!");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from friends");
if(!rs.next()) {
System.out.println ("空表,没有数据!!");
}else {
System.out.print(rs.getString(1) + "\t");
System.out.print(rs.getString(2) + "\t");
System.out.print(rs.getInt(3) + "\t"); 字串9
System.out.print(rs.getDate(4) + "\t");
System.out.print(rs.getInt(5) + "\t");
System.out.println ();
while(rs.next()) {
System.out.print(rs.getString(1) + "\t");
System.out.print(rs.getString(2) + "\t"); 字串6
System.out.print(rs.getInt(3) + "\t");
System.out.print(rs.getDate(4) + "\t");
System.out.print(rs.getInt(5) + "\t");
System.out.println ();
}
}
}catch(SQLException ce) {
System.out.println (ce);
}
}
public static void main(String[] args) {
CourseAppl retObj = new CourseAppl();
retObj.getConnection();
retObj.display();
}
}