2
我開始使用Java學習SQL,並試圖使用Swing
和SQLite
進行註冊/登錄系統。如何使用JDBC基於多個條件過濾數據庫記錄?
所以,我做了幾乎整個基本的登錄系統,但我堅持。我有名爲user
的表格,我有兩個帶有按鈕的文本字段,如果點擊了按鈕,它只是表示hello +'username'。所以在我的表,例如,我有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,我的密碼
所以基本上我想弄清楚如何檢查用戶輸入例如只是 管理員密碼 比打招呼管理等
因此,這裏是我的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();
}
}
任何幫助將是巨大的!
請發佈您的表的模式並描述您確切的問題。 – 2014-09-05 02:39:52
你只需要找到一個記錄,其中一個字段等於你的參數,並從這個記錄中檢索另一個字段的值? '從用戶的WHERE密碼= <輸入的值>'SELECT用戶名? – 2014-09-05 03:06:37
MHM,如果我在表有2個記錄,就像我上面貼的,我希望能夠檢查是否輸入任何這些,如果它是不是過程,並說登錄等等等等等等... – Filip785 2014-09-05 03:10:10