我已经导入了sqlserver驱动类,程序的部分代码如下所示
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String url ="jdbc:microsoft:sqlserver://192.168.1.122:1433;DatabaseName=student"; con = DriverManager.getConnection(url, "sa", "");
好像都没什么错啊,为什么会出现下列的提示语句
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing 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 Source)
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String url ="jdbc:microsoft:sqlserver://192.168.1.122:1433;DatabaseName=student"; con = DriverManager.getConnection(url, "sa", "");
好像都没什么错啊,为什么会出现下列的提示语句
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing 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 Source)
如果你的代码没写错的话(譬如IP,JDBC驱动名),那应该是你SQL2000没有更新到SP3以上造成的。
请确认你的SQL2000的版本,最好升级到SP3或者SP4.补丁在微软官方网站应该有下。
试下我这个代码
package com.shop.db;
import java.sql.*;
import java.io.*;/**************************************************
* author:East(张栋芳)
* date:2008-6-13
* note:打开数据库的连接,和关闭连接
**************************************************/public final class DataBaseOperator {
public DataBaseOperator() {
}
/***
*连接数据库用户指定driver,url,user,pwd
*/
public static Connection createConnection(String driver,String url,String user,String pwd){
Connection con = null;
try{
Class.forName(driver);
con = DriverManager.getConnection(url,user,pwd);
}catch(ClassNotFoundException ce){
ce.printStackTrace();
}catch(SQLException se){
se.printStackTrace();
}
return con;
}
/***
*连接数据库用户用默认的SQL server 2000的纯驱动
*/
public static Connection createConnection(){
String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url = "jdbc:microsoft:sqlserver://127.0.0.1:1433;databaseName=pubs";
String user = "sa";
String pwd = "";
return createConnection(driver,url,user,pwd);
}
/***
*创建一个连接通过配制文件
*/
public static Connection createConnection(String fileName){
String driver="";
String url="";
String user="";
String pwd="";
BufferedReader in = null;
try{
in = new BufferedReader(new FileReader(fileName));
driver = in.readLine();
url = in.readLine();
user = in.readLine();
pwd = in.readLine();
in.close();
}catch(IOException ioe){
ioe.printStackTrace();
}
return createConnection(driver,url,user,pwd);
}
/***
*创建一个连接桥驱动
*/
public static Connection createConnection(String driver,String url){
driver = "sun.jdbc.odbc.JdbcOdbcDriver";
url = "jdbc:odbc:myodbc";
String user = "sa";
String pwd = "";
return createConnection(driver,url,user,pwd);
}
/***
*关闭连接。con,pstmt,rs;
*/
public static void closeAll(Connection con,PreparedStatement pstmt,ResultSet rs){
try{
if(rs != null)rs.close();
if(pstmt != null) pstmt.close();
if(con != null) con.close();
}catch(SQLException se){
se.printStackTrace();
}finally{
rs = null;
pstmt = null;
con = null; }
}
/***
*关闭连接。con,pstmt;
*/
public static void closeAll(Connection con,PreparedStatement pstmt){
try{
if(pstmt != null) pstmt.close();
if(con != null) con.close();
}catch(SQLException se){
se.printStackTrace();
}finally{
pstmt = null;
con = null;
}
}
/***
*关闭连接。con
*/
public static void closeAll(Connection con){
try{
if(con != null) con.close();
}catch(SQLException se){
se.printStackTrace();
}finally{
con = null;
}
}
/**
*关闭一个连接con,stmt,rs
*/
public static void closeAll(Connection con,Statement stmt,ResultSet rs){
try{
if(con != null) con.close();
if(stmt != null) stmt.close();
if(rs != null) rs.close();
}catch(SQLException se){
se.printStackTrace();
}
}
/**
*关闭一个连接con,stmt
*/
public static void closeAll(Connection con,Statement stmt){
try{
if(con != null) con.close();
if(stmt != null) stmt.close();
}catch(SQLException se){
se.printStackTrace();
}
}}