sql server 2000 jdbc driver访问sql server2000出错(50分)

  • 主题发起人 主题发起人 youweibin
  • 开始时间 开始时间
Y

youweibin

Unregistered / Unconfirmed
GUEST, unregistred user!
代码如下:
import java.sql.*;
public class PrintEmployees
{
public static void main(String[] args)
{
String driverName = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String connURL = "jdbc:microsoft:sqlserver://taxi:1433;User=sa;Password=;DatabaseName=Northwind";

try
{
Class.forName(driverName);
Connection conn = DriverManager.getConnection(connURL);
PreparedStatement stmt = conn.prepareStatement("SELECT FirstName, LastName, BirthDate FROM Employees");
ResultSet rs = stmt.executeQuery();

while (rs.next());
{
System.out.println("FirstName:" + rs.getString(1));
System.out.println(" LastName:" + rs.getString(2));
System.out.println("BirthDate:" + rs.getDate(3));
System.out.println("");
}

rs.close();
stmt.close();
conn.close();
}
catch (ClassNotFoundException ex)
{
ex.printStackTrace();
}
catch (SQLException ex)
{
ex.printStackTrace();
}
}
}
错误信息如下:
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Invalid operation for the current cursor position.
at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.base.BaseResultSet.validateCursorPosition(Unknown Source)
at com.microsoft.jdbc.base.BaseResultSet.getString(Unknown Source)
at PrintEmployees.main(PrintEmployees.java:19)
第19行的代码为:System.out.println("FirstName:" + rs.getString(1));
请问是什么原因?
 
没有经过测试:
String connURL = "jdbc:microsoft:sqlserver://taxi:1433;User=sa;Password=;DatabaseName=Northwind";
改成:
String connURL = "jdbc:microsoft:sqlserver://taxi:1433;DatabaseName=Northwind";
然后:
Connection conn = DriverManager.getConnection(connURL);
改成:Connection conn = DriverManager.getConnection(connURL,"sa","");
 
原因知道了,是在while循环后面多了一个分号,所以访问数据的时候光标已经到表尾了。
 
接受答案了.
 
后退
顶部