package demo;
//import java.util.*;
import java.sql.*;
public class Database {
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
String driverName = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=master";
String userName = "***";
String userPwd = "****";
Connection dbConn;
try {
Class.forName(driverName);
dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
Statement stmt=dbConn.createStatement();
ResultSet rs=stmt.executeQuery("select sno,sname from master.dbo.student");
String sno;
String sname;
while(rs.next())
{
sno=rs.getString("sno");
sname=rs.getString("sname");
System.out.println(sno);
System.out.println(sname);
}
System.out.println("Connection Successful!");
} catch (Exception e) {
e.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:525)
at java.sql.DriverManager.getConnection(DriverManager.java:171)
at demo.Database.main(Database.java:21)
我的sqlserver2000驱动包已加到/jdk1.5/jre/lib/ext中
[此贴子已经被作者于2007-8-24 23:00:58编辑过]
----------------解决方案--------------------------------------------------------
楼上的可能是连接出问题了 没用过那种连接方法
----------------解决方案--------------------------------------------------------
是sqlserver的驱动包的路径不对吗?
----------------解决方案--------------------------------------------------------
很明显是驱动的问题,端口有问题!!
----------------解决方案--------------------------------------------------------
如果不是导包问题
导包正确在字符串外面可以 点 出来com.microsoft.jdbc.sqlserver.SQLServerDriver
那有可能是用户名密码不正确
另外 Connection conn; --> Connection conn=null;
会在程序中省去很多麻烦
----------------解决方案--------------------------------------------------------
"jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs"
----------------解决方案--------------------------------------------------------
是sql server2000自身的问题,我下了一个补丁,现在连上啦...
谢谢各位的回答!
----------------解决方案--------------------------------------------------------
安装sql sp3补丁ok
----------------解决方案--------------------------------------------------------