回复 1# 的帖子
import java.sql.*;
public class MyDB {
public static void main(String[] args) {
//加载驱动程序
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
} catch (ClassNotFoundException ex) {
}
String str =
"jdbc:microsoft:sqlserver://localhost:1433;databasename=hyp";
try {
Connection con = DriverManager.getConnection(str,
"sa", "");
Statement sta = con.createStatement();
ResultSet res = sta.executeQuery("select * from hyp1");
while(res.next()) {
System.out.print(res.getString("Sno"));
System.out.print(res.getString("Sname"));
}
res.close();
sta.close();
con.close();
} catch (SQLException ex1) {
ex1.printStackTrace();
}
}
}
错误如下:
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(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at MyDB.main(MyDB.java:15)