SQL 2005 和 eclipse 的问题

publicclassmySQL{privatefinalStringDRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriv... public class mySQL {

private final String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private final String URL = "jdbc:sqlserver://localhost:1433;DatabaseName=Grade1";
private String sql = "select name,age from student";

public Connection con;
public Statement stmt;
public ResultSet rs;

public mySQL() {
try {
Class.forName(DRIVER); // 加载驱动器
con = DriverManager.getConnection(URL, "sa", "123456"); // 连接数据库
stmt = con.createStatement();
rs = stmt.executeQuery(sql);
while (rs.next()) {
System.out.println("用户名为" + rs.getString(1) + "密码为"
+ rs.getString(2));
}
rs.close();
stmt.close();
con.close();
} catch (ClassNotFoundException ex) { // 因类不存在,加载失败
ex.printStackTrace();
} catch (SQLException ex) {
ex.printStackTrace();
ex.printStackTrace();
}
}

public static void main(String[] args) {
// TODO 自动生成方法存根
new mySQL();

}
}

报错 是

com.microsoft.sqlserver.jdbc.SQLServerException: Unrecognized Windows Sockets error: 997: recv failed
at com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(Unknown Source)
at com.microsoft.sqlserver.jdbc.TDSChannel.read(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.Prelogin(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.loginWithoutFailover(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at JDBC.mySQL.<init>(mySQL.java:18)
at JDBC.mySQL.main(mySQL.java:38)
com.microsoft.sqlserver.jdbc.SQLServerException: Unrecognized Windows Sockets error: 997: recv failed
at com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(Unknown Source)
at com.microsoft.sqlserver.jdbc.TDSChannel.read(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.Prelogin(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.loginWithoutFailover(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at JDBC.mySQL.<init>(mySQL.java:18)
at JDBC.mySQL.main(mySQL.java:38)
展开
 我来答
sky70868047
2009-06-17 · 超过15用户采纳过TA的回答
知道答主
回答量:46
采纳率:0%
帮助的人:41.1万
展开全部
public class mySQL {

private final String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private final String URL = "jdbc:sqlserver://localhost:1433;DatabaseName=Grade1";
private String sql = "select name,age from student";

public Connection con;
public Statement stmt;
public ResultSet rs;

try {
Class.forName(DRIVER); // 加载驱动器
con = DriverManager.getConnection(URL, "sa", "123456"); // 连接数据库
stmt = con.createStatement();
rs = stmt.executeQuery(sql);
while (rs.next()) {
System.out.println("用户名为" + rs.getString(1) + "密码为"
+ rs.getString(2));
}
rs.close();
stmt.close();
con.close();
} catch (ClassNotFoundException ex) { // 因类不存在,加载失败
ex.printStackTrace();
}
}

把它改为这样 直接运行试试!
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式