1
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

    if (evt.getSource() == jButton1) {

        String ab = jTextField1.getText();
        String bc = jPasswordField1.getText().toString();
        String cd = jTextField2.getText();
        String de = jTextField3.getText();
        PreparedStatement ps1 = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            Connection c = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "system", "hr");
            ps = c.prepareStatement("Select User_Name from AdminLogin where Password =?");
            ps.setString(1, bc);

            rs = ps.executeQuery();
            rs.next();

            if (ab.equals(rs.getString(1))) {

                ps1 = c.prepareStatement("Update AdminLogin SET Date1=?,Time=? WHERE Password=?");
                ps1.setString(1, cd);
                ps1.setString(2, de);
                ps1.setString(3, bc);
                int e = ps1.executeUpdate();
                JOptionPane.showMessageDialog(this, "Welcome", "Logged In", JOptionPane.INFORMATION_MESSAGE);
                //MainMenuAAI mainMenuAAI = new MainMenuAAI();
                //setVisible(false);}
            } else if (!(ab.equals(rs.getString(1)))) {
                JOptionPane.showMessageDialog(this, "<html>YOU ARE NOT A<br>ADMIN</br></html>", "ERROR", JOptionPane.ERROR_MESSAGE);
                //AdminLogin admin=new AdminLogin();
                //setVisible(false);
            }

            c.close();
        } catch (Exception e) {

            System.out.println(e);
        }
    }// TODO add your handling code here:
}

代码中的一切工作正常。它正在执行里面的代码

if(ab.equals(rs.getString(1)))

并显示“欢迎”,但里面没有显示

if(!(ab.equals(rs.getString(1))))

每当我输入错误的用户名或密码时,它都会显示错误

java.sql.SQLException:用尽的结果集

4

1 回答 1

3

这是正确的,因为如果您输入错误的用户名或密码,将不会返回任何记录。因此,当您rs.next();在这种情况下使用时,它会尝试访问空结果集的第一行!这就是它抛出异常的地方。

您可以像这样修复您的代码:

rs = ps.executeQuery();
//rs.next();
int counter=0;
while (rs.next()) {
    counter++;
    if (ab.equals(rs.getString(1))) {
        ps1 = c.prepareStatement("Update AdminLogin SET Date1=?,Time=? WHERE Password=?");
        ps1.setString(1, cd);
        ps1.setString(2, de);
        ps1.setString(3, bc);
        int e = ps1.executeUpdate();
        JOptionPane.showMessageDialog(this, "Welcome", "Logged In", JOptionPane.INFORMATION_MESSAGE);
        //MainMenuAAI mainMenuAAI = new MainMenuAAI();
        //setVisible(false);}
    } 
}
if(counter==0){
    JOptionPane.showMessageDialog(this, "<html>YOU ARE NOT A<br>ADMIN</br></html>", "ERROR", JOptionPane.ERROR_MESSAGE);
}
于 2013-07-18T06:07:43.047 回答