日期:2014-05-16 浏览次数:20611 次
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.
?
?
转载;
【系统】
winxpsp3
【工具】
MyEclipse6.5
SQLServer2000个人版(升级到sp4? SQL版本8.00.2039)
SQLServer 2000 Driver for JDBC
【注意事项】
1.Test.java必须要处理SQLException和ClassNotFoundException两个异常,否则MyEclipse提示这两个异常不可控制;
2.遍历结果集的时候用While语句,用if(rs.next())只会输出结果集的第一条记录。
【步骤】
1.SQLServer2000个人版升级到SP4,没有装过SP3直接到SP4(SQL版本8.00.2039)
2.安装SQLServer 2000 Driver for JDBC,从它的安装目录下的lib目录下拷贝3个jar:mssqlserver.jar;msutil.jar;msbase.jar
到工程目录WebRoot\WEB-INF\lib\下(否则报错是Error Establish Socket)或者直接从MyEclipse下构建路径。
3.MyConnection.java
view plaincopy to clipboardprint?
package tff.messagebook.Database;?? 
? 
import java.sql.Connection;?? 
import java.sql.DriverManager;?? 
import java.sql.SQLException;?? 
? 
public class MyConnection {?? 
??? private String Driver;?? 
??? private String Url;?? 
??? private String UserID;?? 
??? private String Password;?? 
??? private Connection conn=null;?? 
?????? 
?????? 
??? public MyConnection(){?? 
??????? this.Driver="com.microsoft.jdbc.sqlserver.SQLServerDriver";?? 
??????? this.Url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=guestbook";?? 
??????? this.UserID="sa";?? 
??????? this.Password="";?? 
??? }?? 
??? public Connection getConnection() throws SQLException,ClassNotFoundException{?? 
??????? try{?? 
??????? Class.forName(this.Driver);?? 
??????? this.conn=DriverManager.getConnection(this.Url, this.UserID, this.Password);?? 
??????? }catch(SQLException e1){?? 
??????????? e1.printStackTrace();?? 
??????? }catch(ClassNotFoundException e2){?? 
??????????? e2.printStackTrace();?? 
??????? }?? 
??????? return this.conn;?? 
??? }?? 
?????? 
??? public void close(){?? 
??????? try{?? 
??????????? this.conn.close();?? 
??????? }catch(SQLException e){?? 
??????????? e.printStackTrace();?? 
??????? }?? 
??? }?? 
}? 
package tff.messagebook.Database;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class MyConnection {
?private String Driver;
?private String Url;
?private String UserID;
?private String Password;
?private Connection conn=null;
?
?
?public MyConnection(){
??this.Driver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
??this.Url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=guestbook";
??this.UserID="sa";
??this.Password="";
?}
?public Connection getConnection() throws SQLException,ClassNotFoundException{
??try{
??Class.forName(this.Driver);
??this.conn=DriverManager.getConnection(this.Url, this.UserID, this.Password);
??}catch(SQLException e1){
???e1.printStackTrace();
??}catch(ClassNotFoundException e2){
???e2.printStackTrace();
??}
??return this.conn;
?}
?
?public void close(){
??try{
???this.conn.close();
??}catch(SQLException e){
???e.printStackTrace();
??}
?}
} 
4.Test.java
view plaincopy to clipboardprint?
package tff.messagebook.Database;?? 
? 
import java.sql.ResultSet;?? 
import java.sql.SQLException;?? 
import java.sql.Statement;?? 
import java.sql.Connection;?? 
? 
public class Test {?? 
??? public static void main(String[] args)throws SQLException,ClassNotFoundException {?? 
??????? MyConnection myc=new MyConnection();?? 
??????? Connection conn=myc.getConnection();?? 
??????? Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);?? 
??????? String sql="select * from guestbook";?? 
?????????? 
??????? ResultSet rs=stmt.executeQuery(sql);?? 
?????????? 
??????? while(rs.next()){?? 
?