日期:2014-05-18  浏览次数:20631 次

痛苦的一天 完全没有连接上 数据库 望高手解答 谢谢了
package myclass;

import java.sql.*;

public class Jdbc{ 
  public Connection getConnection() 
  { 
  String driver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
  String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=stuDB";
  Connection con=null;
  Statement st;
  ResultSet rs;
  try {  
  Class.forName(driver); 
  con=DriverManager.getConnection(url,"sa","");
  }
  catch(ClassNotFoundException ex){  
  System.out.println("DriverError");  
  ex.printStackTrace();  
  }  
  catch(SQLException ex){  
  System.out.println( "Connection error");  
  ex.printStackTrace();  
  }  

  return con ;  

  }
 
}
-----
package myclass;

import java.sql.*;

public class Jdbctest{
Jdbc con=new Jdbc();
  void dispaly(){

try{
Connection cn = con.getConnection();
String sql="Select * from SC";
Statement s = cn.createStatement();
ResultSet rs = s.executeQuery(sql);
while(rs.next()){
System.out.print(rs.getString(1));
System.out.print(rs.getString(2));
System.out.print(rs.getString(3));

}
}
catch(SQLException ce){
System.out.print(ce);
}

}


public static void main(String [] args){
Jdbctest jt = new Jdbctest();
jt.dispaly();
}
  }
------
报错
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)
Connection error 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 myclass.Jdbc.getConnection(Jdbc.java:15)
at myclass.Jdbctest.dispaly(Jdbctest.java:10)
at myclass.Jdbctest.main(Jdbctest.java:30)
Exception in thread "main" java.lang.NullPointerException
at myclass.Jdbctest.dispaly(Jdbctest.java:12)
at myclass.Jdbctest.main(Jdbctest.java:30)
----
我是在 eclipse 下写的代码 3个驱动也装在了D:\apache-tomcat-5.5.23\common\lib下 但是就是连接不上去 找了一天了 没找到 我用的是SQL2000 我基本上都是用最新的补丁了
请问大家 这个问题是出在那里啊 谢谢了

------解决方案--------------------
我也遇到同样的错误,关注中,希望高手来解答!
------解决方案--------------------
装了三个驱动,还要打sp3或sp4补丁~
JAVA菜鸟,如何才能学好呢