我开始使用Java学习SQL,我正在尝试使用Swing
和SQLite
创建注册/登录系统。
所以,我几乎完整的基本登录系统,但我被卡住了。我有一个名为user
的表,我有2个带按钮的文本字段,如果点击了按钮,它只是说你好+'用户名'。所以在我的表中,例如,我有2个值:
例如,我有表'用户':
+----------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+--------------+------+-----+---------+-------+ | admin | varchar(255) | YES | | NULL | | | password | varchar(255) | YES | | NULL | | +----------+--------------+------+-----+---------+-------+
有数据:
+----------+-------------+ | admin | password | +----------+-------------+ | myLogin | myPassword | | myLogin1 | myPassword1 | +----------+-------------+
当我输入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 password1而不是它会验证但是我有太多的管理员密码我无法验证。
这是我的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();
}
}
任何帮助都会很棒!
答案 0 :(得分:0)
你应该将ConnectDB调用到ActionPerformed方法,因为在你加载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 + "'";