簡體   English   中英

如何從 mysql netbeans 驗證用戶名和密碼

[英]How to validate username and password from mysql netbeans

大家好,我想使用 netbeans Jform 從我的 phpmyadmin 數據庫驗證用戶名和密碼

這是我的代碼

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

    String a = username.getText();
    String b = password.getText();
    try {
        //configDB is my connnection file to mysql
        java.sql.Connection conn = (Connection) config.configDB();
        String username = "select username from akun where username='" + a + "';";
        String password = "select password from akun where password='" + b + "';";

        java.sql.PreparedStatement us = conn.prepareStatement(username);
        java.sql.PreparedStatement pw = conn.prepareStatement(password);
        us.execute();
        pw.execute();

        if (a.equals(username.toString()) && b.equals(password.toString())) {
            JOptionPane.showMessageDialog(this, "Benar");
        } else {
            JOptionPane.showMessageDialog(this, "Username or Password is incorrect");
        }

    } catch (Exception e) {
        JOptionPane.showMessageDialog(this, e.getMessage());
    }        // TODO add your handling code here:
}       

試試這個。

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

String a = username.getText();
String b = password.getText();

try {
    
    java.sql.Connection conn = (Connection) config.configDB();
    String query = "select username, password  from akun where username='" + a + "';";

    java.sql.PreparedStatement ps = conn.prepareStatement(query);
    java.sql.ResultSet resultSet = ps.executeQuery(query);
    String un = "";
    String pw = "";
    while (resultSet.next()) {
            un = resultSet.getString("username");
            pw = resultSet.getString("password");
     }
    

    if (a.equals(us) && b.equals(pw)) {
        JOptionPane.showMessageDialog(this, "Benar");
    } else {
        JOptionPane.showMessageDialog(this, "Username or Password is incorrect");
    }

} catch (Exception e) {
    JOptionPane.showMessageDialog(this, e.getMessage());
}        // TODO add your handling code here:

}

您需要確保您的資源已關閉並正確使用PreparedStatement

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

        String a = username.getText();
        String b = password.getText();
        try {
            // configDB is my connnection file to mysql
            try (java.sql.Connection conn = (Connection) config.configDB()) {
                String username = "select username from akun where username = ? and password = ?";

                java.sql.PreparedStatement ps = conn.prepareStatement(username);
                java.sql.Result rs = ps.executeQuery();

                if (rs.next()) {
                    JOptionPane.showMessageDialog(this, "Benar");
                } else {
                    JOptionPane.showMessageDialog(this, "Username or Password is incorrect");
                }
            }

        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, e.getMessage());
        } // TODO add your handling code here:
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM