【问题标题】:How to filter database records based on more than one condition using JDBC?如何使用 JDBC 根据多个条件过滤数据库记录?
【发布时间】:2014-10-29 22:24:33
【问题描述】:

我开始使用 Java 学习 SQL,我正在尝试使用 SwingSQLite 制作注册/登录系统。

所以,我制作了几乎整个基本的登录系统,但我被卡住了。我有一个名为users 的表,我有2个带有按钮的文本字段,如果单击按钮,它只会说你好+'用户名'。因此,例如,在我的表中,我有 2 个值:

例如,我有表'user':

+----------+--------------+------+-----+---------+ --------+ |领域 |类型 |空 |钥匙 |默认 |额外 | +----------+--------------+------+-----+---------+ --------+ |管理员 | varchar(255) |是 | |空 | | |密码 | varchar(255) |是 | |空 | | +----------+--------------+------+-----+---------+ --------+

有数据:

+------------+-------------+ |管理员 |密码 | +------------+-------------+ |我的登录 |我的密码 | |我的登录1 |我的密码1 | +------------+-------------+

当我输入 myUsername1 , myPassword1 等时它会识别。如果我输入 myUsername, myPassword 它将不会识别

所以基本上我想弄清楚如何检查用户是否输入了例如 管理员密码 而不是说你好管理员等。

这是我的 Connect.java

import java.sql.*;

import javax.swing.JOptionPane; 

public class Connect {

static String username;
static String password;
static String query;

public static Connection ConnectDB() {
    try {

        Class.forName("org.sqlite.JDBC");
        Connection conn = null;
        conn = DriverManager.getConnection("jdbc:sqlite:C:\\Users\\Alex\\workspace   \\RegisterAndLoginSystem\\System.sqlite");

        Statement state = null;
        state = conn.createStatement();

        query = "SELECT * FROM `user`";
        // This query does not work as well
        // query = "SELECT admin,password FROM user WHERE admin = '" + GuiLogin.user + "' AND password = '" + GuiLogin.pass + "'";


        ResultSet rs = state.executeQuery(query);

        while(rs.next()) {
            username = rs.getString("admin");
            System.out.println(username);
            password = rs.getString("password");
            System.out.println(password);
        }
        rs.close();
        state.close();

        return conn;

    } catch(Exception e) {
        JOptionPane.showMessageDialog(null, "Could not connect: " + e);
        return null;
    }
}

}

例如,现在我可以输入 admin1 密码 1 并且它会验证但我有太多无法验证的管理员密码。

这是我的 GUI 类:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

    import javax.swing.*;

    public class GuiLogin extends JFrame {

    public static final long serialVersionUID = 1L;

    private JPanel panel;
    private JButton button;
    private JTextField field1;
    private JPasswordField field2;
    private JLabel label1, label2, answer;

    static String user;
    static String pass;

    public GuiLogin() {

        try {
             UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch(Exception e) {
            e.printStackTrace();
        }

        panel = new JPanel();

        label1 = new JLabel("Username:");
        panel.add(label1);

        field1 = new JTextField("", 15);
        panel.add(field1);

        label2 = new JLabel("Password:");
        panel.add(label2);

        field2 = new JPasswordField("", 15);
        panel.add(field2);

        button = new JButton("Login");
        button.setFocusPainted(false);
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if(e.getSource() == button) {
                    user = field1.getText();
                    pass = field2.getText();

                    if(user.equals(Connect.username) && pass.equals(Connect.password)) {
                        answer.setText("Logged in");
                    } else {
                        answer.setText("Bad login data try again");
                    }
                }
            }
        });
        panel.add(button);

        answer = new JLabel("");
        panel.add(answer);

        this.add(panel);
        this.setSize(500, 400);
        this.setVisible(true);
        this.setResizable(false);
        this.setLocationRelativeTo(null);
        this.setTitle("Login");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        Connect.ConnectDB();
        new GuiLogin();
    }

    }

任何帮助都会很棒!

【问题讨论】:

  • 请发布您的表的架构并描述您的确切问题。
  • 您是否只需要找到其中一个字段等于您的参数的记录并从该记录中检索另一个字段的值? SELECT username from Users WHERE password=<entered value>?
  • 嗯,如果我在表中有 2 条记录,就像我在上面发布的那样,我希望能够检查是否输入了其中的任何一条,如果它不是处理它并说登录等等等等...
  • Prepared Statement 与参数一起使用。
  • 不要存储未经哈希处理的密码!。您应该存储一个散列和加盐的密码,通过用户名检索该密码,然后使用存储密码的盐对用户的密码进行散列。如果存储的哈希值和新的哈希值相同,则密码正确。至于您的其他问题:请查看PreparedStatement 上的 JDBC 教程,例如 this one

标签: java sql sqlite jdbc


【解决方案1】:

您应该在 ActionPerformed 方法中调用 ConnectDB,因为在您的代码中,在加载 GUI 之前从数据库中获取数据,而不是在用户输入其姓名和密码并单击“登录”按钮后从数据库中加载数据。

像这样重写 GUI 类:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class GuiLogin extends JFrame {

    public static final long serialVersionUID = 1L;

    private JPanel panel;
    private JButton button;
    private JTextField field1;
    private JPasswordField field2;
    private JLabel label1, label2, answer;

    static String user;
    static String pass;

    public GuiLogin() {

        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }

        panel = new JPanel();

        label1 = new JLabel("Username:");
        panel.add(label1);

        field1 = new JTextField("", 15);
        panel.add(field1);

        label2 = new JLabel("Password:");
        panel.add(label2);

        field2 = new JPasswordField("", 15);
        panel.add(field2);

        button = new JButton("Login");
        button.setFocusPainted(false);
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (e.getSource() == button) {
                    user = field1.getText();
                    pass = field2.getText();

                    Connect.ConnectDB();

                    if (user.equals(Connect.username)
                            && pass.equals(Connect.password)) {
                        answer.setText("Logged in");
                    } else {
                        answer.setText("Bad login data try again");
                    }
                }
            }
        });
        panel.add(button);

        answer = new JLabel("");
        panel.add(answer);

        this.add(panel);
        this.setSize(500, 400);
        this.setVisible(true);
        this.setResizable(false);
        this.setLocationRelativeTo(null);
        this.setTitle("Login");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new GuiLogin();
    }
}

现在已注释的 SQL 查询将起作用。所以使用查询:

query = "SELECT admin,password FROM user WHERE admin = '" + GuiLogin.user + "' AND password = '" + GuiLogin.pass + "'";

【讨论】:

    猜你喜欢
    • 2021-04-10
    • 2011-07-26
    • 2020-10-23
    • 1970-01-01
    • 1970-01-01
    • 2022-01-03
    • 1970-01-01
    • 2018-08-24
    相关资源
    最近更新 更多