在利用纯java jdbc驱动程序实现数据路的连接.我已经把那三个数据包放到了jdk中的bin/ext文件夹中了.
给出程序是这样的:
import java.sql.*;
import javax.swing.*;
class ConnectServer2 {
static Connection con=null;
public static boolean conn(String url,String username,String password){
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
}catch(Exception e){ e.printStackTrace();return false;}
try{
con = DriverManager.getConnection(url,username,password);
}catch(SQLException e){ e.printStackTrace();return false;}
return true;
}
public static boolean close(){
try{
con.close();
con = null;
}catch(SQLException e){ return false;}
return true;
}
public static void main(String args[]){
if(conn("jdbc:microsoft:sqlserver://localhost:3606;DatabaseName=xsgl","sa","")){
JOptionPane.showMessageDialog(null, "数据库连接成功");
close();
}
else
JOptionPane.showMessageDialog(null, "数据库连接失败");
}
}
错误提示是这样的:
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)
at com.microsoft.jdbc.base.BaseConnection.getNewImplConnection(Unknown Source)
at com.microsoft.jdbc.base.BaseConnection.open(Unknown Source)
at com.microsoft.jdbc.base.BaseDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(DriverManager.java:525)
at java.sql.DriverManager.getConnection(DriverManager.java:171)
at ConnectServer2.conn(ConnectServer2.java:19)
at ConnectServer2.main(ConnectServer2.java:44)
什么是正在建立socket错误,端口不是可以在一定范围内可以任意定的吗?