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编辑过]