我有一个Manager类,可以将数据保存在SQL表中,还可以从SQL表中获取结果并测试这些数据。当我运行程序时,将显示一个获取ID和密码的框架,如果它们正确,则另一个框架将但是我不知道为什么它只是测试SQL表的最后一行?我的意思是如果我用除最后一行以外的其他ID和密码设置那些文本字段,它将显示数据错误(我之前为错误的数据设置了它)
经理班:
public static boolean Test(String userName, String password) { boolean bool = false; Statement stmt = null; try { stmt = conn.createStatement(); ResultSet rst = null; rst = stmt.executeQuery("SELECT yahooId , password FROM clienttable"); while (rst.next()) { if (rst.getString(1).equalsIgnoreCase(userName) && rst.getString(2).equalsIgnoreCase(password)) { bool = true; } else { bool = false; } } } catch (SQLException ex) { Logger.getLogger(Manager.class.getName()).log(Level.SEVERE, null, ex); } return bool; }
我的按钮在获取ID和密码并对其进行测试的框架中执行操作:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { try { submit(); } catch (ConnectException ex) { JOptionPane.showMessageDialog(this, "You coudn't connect to the server successfully,try it again", "Sign_In Problem", JOptionPane.OK_OPTION); } clear(); } private void submit() throws ConnectException { String id = idField.getText(); char[] pass1 = passField.getPassword(); String pass = new String(pass1); if (id.equals("") || pass.equals("")) { Toolkit.getDefaultToolkit().beep(); JOptionPane.showMessageDialog(this, "You should enter an ID and password", "Sign_In Problem", JOptionPane.OK_OPTION); return; } else { boolean b = Manager.Test(id, pass); client.setCurrentName(id); if (b == true) { this.setVisible(false); ListFrame frame = new ListFrame(client); frame.setVisible(true); } else { JOptionPane.showMessageDialog(this, "You have entered wrong datas,try it again", "Sign_In Problem", JOptionPane.OK_OPTION); return; } } }
编辑:
我已经编辑了我的经理班级(测试方法),但它仍然像过去一样工作!
public static boolean Test(String userName, String password) { boolean bool = false; PreparedStatement stmt = null; ResultSet resultSet = null; try { stmt = conn.prepareStatement("SELECT id FROM clienttable WHERE yahooId = ? AND password = ?"); stmt.setString(1, userName); stmt.setString(2, password); resultSet = stmt.executeQuery(); bool = resultSet.next(); } catch (SQLException ex) { Logger.getLogger(Manager.class.getName()).log(Level.SEVERE, null, ex); } finally { try { resultSet.close(); stmt.close(); conn.close(); } catch (SQLException ex) { Logger.getLogger(Manager.class.getName()).log(Level.SEVERE, null, ex); } } return bool; }
因为您要将 整个 数据库表拖到Java内存中,并在while循环中测试 每一 行。如果找到匹配项,则不会中断循环,这样它将继续覆盖布尔结果,直到最后一行为止。
也就是说,您真的不想在Java中进行比较。只需使用SQL WHERE子句即可。这 要 高效 得多 ,实际上是数据库应该完成的任务。不要试图用Java接管数据库的工作,那样只会效率低下。
WHERE
public boolean exists(String username, String password) throws SQLException { Connection connection = null; PreparedStatement preparedStatement = null; ResultSet resultSet = null; boolean exists = false; try { connection = database.getConnection(); preparedStatement = connection.prepareStatement("SELECT id FROM client WHERE username = ? AND password = ?"); preparedStatement.setString(1, username); preparedStatement.setString(2, password); resultSet = preparedStatement.executeQuery(); exists = resultSet.next(); } finally { close(resultSet); close(preparedStatement); close(connection); } return exists; }
您会看到我做了一些增强:
要了解有关以适当方式使用JDBC的更多信息,您会发现本基础教程很有用。