API如下: getConnection public static Connection getConnection(String url) throws SQLExceptionAttempts to establish a connection to the given database URL. The DriverManager attempts to select an appropriate driver from the set of registered JDBC drivers. Parameters: url - a database url of the form jdbc:subprotocol:subname Returns: a connection to the URL Throws: SQLException - if a database access error occurs
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Invalid operation for the current cursor position.
控制台有没有打印出数据?? 你把while语句改成rs.next()试试
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Invalid operation for the current cursor position. 说明是你的JDBC连接数据库出了问题,和Swing没有关系了 JDBC连接MSSQLserverClass.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance(); String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb"; //mydb为数据库 String user="sa"; String password=""; Connection conn= DriverManager.getConnection(url,user,password);
2 admin admin null 4 ceshi test [email protected] 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 exa1.db2$1.actionPerformed(db2.java:58)
这句不对,要三个参数,你只传了一个,虽然你把三个写一起变成一个了,但这样不行。
分开写
getConnection
public static Connection getConnection(String url)
throws SQLExceptionAttempts to establish a connection to the given database URL. The DriverManager attempts to select an appropriate driver from the set of registered JDBC drivers. Parameters:
url - a database url of the form jdbc:subprotocol:subname
Returns:
a connection to the URL
Throws:
SQLException - if a database access error occurs
你把while语句改成rs.next()试试
JDBC连接MSSQLserverClass.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb";
//mydb为数据库
String user="sa";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);
这样
try {
Class.forName(drivername);
Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
rs.next();
jta_data.setText(rs.getString(1)+"");
jta_data.setText(rs.getString(2)+"");
jta_data.setText(rs.getString(3)+"");
jta_data.setText(rs.getString(4)+"");
} catch (Exception e1) {
// TODO: handle exception
e1.printStackTrace();
}
控制台有数据输出,但是窗体中textarea内没有数据输出。
截图如下
2 admin admin null
4 ceshi test [email protected]
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 exa1.db2$1.actionPerformed(db2.java:58)
Class.forName(drivername);
Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
String text="";
while (rs.next()) {
text+=rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3)+" "+rs.getString(4)+"\n";
}
jta_data.setText(text);
} catch (Exception e1) {
// TODO: handle exception
e1.printStackTrace();
}