通过结果集从数据库获取require用户和密码

时间:2016-03-19 14:03:31

标签: java sql sql-injection resultset

String user=usertx.getText();
    char[] password=pswdtx.getPassword();
    String pswd=String.valueOf(password);   \\here is important to put a char password in string.

1-我们通过以下方式呼叫require用户及其密码:

Resultset rs= stmt.excuteQuery("SELECT * FROM USERS WHERE NAME='"+user+"' AND PASSWORD='"+pswd+"'");

此代码将仅获取所选用户的行及其在行上的信息,并将其设置为(rs)。

2。rs.next()代码:

while(rs.next()){
     rsuser=rs.getString("NAME");      //get user and set it in rsuser.
     rspswd=rs.getString("PASSWORD");} //get password and set it in rspswd.

if ((user.equals(rsuser)) && (pswd.equals(rspswd))){  //the equevlant statement.
        JOptionPane.showMessageDialog(null, "Username and Password exist");}
        else {
            JOptionPane.showMessageDialog(null, "Please Check Username and Password ");}
        }

这里是完整的代码:

private void submitActionPerformed(java.awt.event.ActionEvent evt) {                                       
    try {
        String user=usertx.getText();
        char[] password=pswdtx.getPassword();
        String pswd=String.valueOf(password);

        String rsuser=null;
        String rspswd=null;
        String Q="SELECT * FROM USERS WHERE NAME='"+user+"' AND PASSWORD='"+pswd+"'";
        rs=stmt.executeQuery(Q);

        while(rs.next()){
        rsuser=rs.getString("NAME");
        rspswd=rs.getString("PASSWORD");}
        if ((user.equals(rsuser)) && (pswd.equals(rspswd))){
        JOptionPane.showMessageDialog(null, "Username and Password exist");}
        else {
            JOptionPane.showMessageDialog(null, "Please Check Username and Password ");}
        } 
    catch (SQLException ex) {ex.printStackTrace();}`

}

带着我的祝福。

0 个答案:

没有答案