当前位置: 代码迷 >> Java Web开发 >> 简单的数据库连接有关问题
  详细解决方案

简单的数据库连接有关问题

热度:32   发布时间:2016-04-17 14:42:49.0
简单的数据库连接问题
源码:
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端口已经打开了,再次执行程序,顺利执行完毕。
  相关解决方案