日期:2014-05-20  浏览次数:20935 次

java连接数据库问题


import   java.sql.Connection;
import   java.sql.DriverManager;
import   java.sql.ResultSet;
import   java.sql.SQLException;
import   java.sql.Statement;

public   class   DBmanager   {
private   Connection   conn=null;
private   Statement   stat=null;

public   DBmanager(){
try   {
openDB();
}   catch   (ClassNotFoundException   e)   {

e.printStackTrace();
}

}
public   void   openDB()   throws   ClassNotFoundException{
Class.forName( "com.microsoft.jdbc.sqlserver.SQLServerDriver ");
try   {
conn=DriverManager.getConnection( "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test ", "sa ", " ");
stat=conn.createStatement();
System.out.println( "****************** ");
}   catch   (SQLException   e)   {
System.out.println( "1111111111111111 ");
e.printStackTrace();
}

}
public   ResultSet   excuteQuery(String   sql){
ResultSet   rs=null;
try{
rs=stat.executeQuery(sql);

}catch(SQLException   e){
e.printStackTrace();
return   null;
}
return   rs;

}

}


错误提示:
1111111111111111
java.sql.SQLException:   [Microsoft][SQLServer   2000   Driver   for   JDBC]ソケットの確立時にエラーが発生しました。
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   DBmanager.openDB(DBmanager.java:25)
at   DBmanager. <init> (DBmanager.java:15)
at   test.main(test.java:9)
Exception   in   thread   "main "   java.lang.NullPointerException
at   DBmanager.excuteQuery(DBmanager.java:38)
at   test.main(test.java:11)

------解决方案--------------------
1433 端口没开还要打sql server 2000 SP4补丁

我也遇到这样的问题。


------解决方案--------------------
你sql用的哪个端口你( "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test ", "sa ", " ");这句端口就写哪个
补丁肯定要打到sql的安装目录里的
------解决方案--------------------
XP下一定要打SP4补丁,不然JDBC无法使用。