源码:
package dbconn;
import java.sql.*;
public class Getconn {
private static final String dbDriver= "com.microsoft.jdbc.sqlserver.SQLServerDriver ";
private static final String url= "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=message_board ";
private static final String userName= "sa ";
private static final String password= "sa ";
private static Connection conn=null;
public Getconn() {
try {
System.out.println( "dbDriver======== "+dbDriver);
Class.forName(dbDriver).newInstance();
System.out.println( "注册成功! ");
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
System.out.println( "url=============== "+url);
System.out.println( "userName========= "+userName);
System.out.println( "password======== "+password);
conn=DriverManager.getConnection(url, userName, password);
System.out.println( "连接成功! ");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Connection getconn(){
return conn;
}
public static void main(String args[]) throws IllegalAccessException, InstantiationException, ClassNotFoundException{
Getconn connnn=new Getconn();
Connection conn=connnn.getconn();
}
}
出现的问题:
dbDriver
注册成功!
url===============jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=message_board
userName=========sa
password========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)
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(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at dbconn.Getconn. <init> (Getconn.java:35)
at dbconn.Getconn.main(Getconn.java:52)
请高手指点!!!!20分赠送
------解决方案--------------------
出现上边错误的主要原因是默认的数据库服务器端口 1433没有打开,无法直接连接。这时你在命令提示行中输入命令:netstat –an 来查看有没有tcp:0.0.0.0:1433,如果没有那就肯定是因为数据库服务器端口 1433端口没开。这时你需要下载SQL_Serve20000的sp3补丁,下载安装后会发现1433端口已经打开了,再次执行程序,顺利执行完毕。