下面显示了我在尝试运行代码时收到的错误消息:
sun.jdbc.odbc.JdbcOdbcStatement cannot be cast to java.sql.ResultSet
我不明白为什么会出现这种错误,所以有人可能会告诉我原因,甚至可以更好地解决问题。以下是整个代码
public void actionlogin()
{
btnLogin.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
String username = txtUserName.getText();
String password = txtPassword.getText();
String databaseUsername = "";
String databasePassword = "";
String dataSourceName = "securitySystem";
String dbUrl = "jdbc:odbc:" + dataSourceName;
try{
//Type of connection driver used
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//Connection variable or object param: dbPath, userName, password
Connection con = DriverManager.getConnection(dbUrl, "", "");
Statement statement = con.createStatement();
statement.execute("select username, password from securitySystem.employees");
while (((ResultSet) statement).next()) {
databaseUsername = ((ResultSet) statement).getString("username"); //resultSet.getString("username");
databasePassword = ((ResultSet) statement).getString("password");
}
statement.close();
con.close();
}catch (Exception e){
System.out.print("Out!" +e);
}
答案 0 :(得分:0)
您需要使用statement.executeQuery,这将返回ResultSet。
所以你需要写这样的东西:
ResultSet rs = statement.executeQuery("select....");
while (rs.next()) {
databaseUsername = rs.getString("username");
databasePassword = rs.getString("password";
}