2014-09-05 62 views
2

我开始使用Java学习SQL,并试图使用SwingSQLite进行注册/登录系统。如何使用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(); 
    } 

    } 

任何帮助将是巨大的!

+0

请发布您的表的模式并描述您确切的问题。 – 2014-09-05 02:39:52

+1

你只需要找到一个记录,其中一个字段等于你的参数,并从这个记录中检索另一个字段的值? '从用户的WHERE密码= <输入的值>'SELECT用户名? – 2014-09-05 03:06:37

+0

MHM,如果我在表有2个记录,就像我上面贴的,我希望能够检查是否输入任何这些,如果它是不是过程,并说登录等等等等等等... – Filip785 2014-09-05 03:10:10

回答

0

您应该将ConnectDB调用到ActionPerformed方法中,因为在您的代码中,数据库中的数据是在加载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 + "'"; 
相关问题