Java 如何使用JDBC基于多个条件筛选数据库记录?

Java 如何使用JDBC基于多个条件筛选数据库记录?,java,sql,sqlite,jdbc,Java,Sql,Sqlite,Jdbc,我开始用Java学习SQL,并尝试使用Swing和SQLite创建注册/登录系统 所以,我做了几乎整个基本的登录系统,但我被卡住了。我有一个名为users的表,我有两个带有按钮的文本字段,如果单击按钮,它只会显示hello+“username”。例如,在我的表中,我有两个值: 例如,我有表“user”: +----------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key

我开始用Java学习SQL,并尝试使用
Swing
SQLite
创建注册/登录系统

所以,我做了几乎整个基本的登录系统,但我被卡住了。我有一个名为
user
s的表,我有两个带有按钮的文本字段,如果单击按钮,它只会显示hello+“username”。例如,在我的表中,我有两个值:

例如,我有表“user”:

+----------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+--------------+------+-----+---------+-------+ | admin | varchar(255) | YES | | NULL | | | password | varchar(255) | YES | | NULL | | +----------+--------------+------+-----+---------+-------+ 例如,现在我可以只输入admin1密码1,然后进行验证,但我有太多无法验证的admin密码

这是我的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();
    }

    }

任何帮助都会很好

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

重写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 + "'";

请发布表的架构并描述具体问题。是否只需查找其中一个字段等于参数的记录并从此记录中检索另一个字段的值<代码>从用户中选择用户名,其中password=?嗯,如果我在表中有2条记录,就像我在上面发布的一样,我希望能够检查是否输入了其中任何一条记录,如果是,然后处理它,说登录了等等等等等等…与参数一起使用。不要存储未保存的密码!。您应该存储一个经过哈希和盐渍处理的密码,通过用户名检索该密码,然后使用存储的密码中的盐渍对用户的密码进行哈希处理。如果存储的哈希和新哈希相同,则密码正确。至于剩下的问题:请看一下关于
PreparedStatement
的JDBC教程,如
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();
    }
}
query = "SELECT admin,password FROM user WHERE admin = '" + GuiLogin.user + "' AND password = '" + GuiLogin.pass + "'";