SQLite登录检查用户名和;密码

时间:2015-02-05 16:32:43

标签: java sqlite login

我正在创建一个应用程序(使用Java& SQLite)(JFrame,使用Netbeans)我有我想要登录的用户。(我有所有正确的包JDBC,SQLite等)

我遇到的问题似乎是获取用户名/密码来检查我的users.db文件..我正在使用Java和SQLite。我也在使用JDBC。

我的一些代码作为示例(这向我的用户发送信息以建立帐户,工作正常),我的数据库是users.db,我想比较用户名/密码与USERNAME& PASSWORD

Connection dbconn = null;
Statement stmt = null;
String query = "insert into USERS(ID, FIRSTNAME, SECONDNAME, USERNAME, PASSWORD, JAVALESSON, CLESSON, PYTHONLESSON) values(?, ?, ?, ?, ?, ?, ?, ?)";

try {
    Class.forName("org.sqlite.JDBC");
    dbconn = DriverManager.getConnection("jdbc:sqlite:users.db");
    Statement statement = dbconn.createStatement();
    PreparedStatement pstmt = dbconn.prepareStatement(query);

我有一个UsernameLoginBox& PasswordLoginBox,我如何查看USERNAME& PASSWORD(来自SQLite数据库)针对文本框中用于登录的字符串?

TRIED CODE:

    Connection conn = null;
    ResultSet rs =null;
    PreparedStatement pst =null;
    String sql1 = "Select * from USERS where USERNAME=? and PASSWORD=?";
    try{
    pst = conn.prepareStatement(sql1);
    pst.setString(1, UsernameLogIn.toString());
    pst.setString(2, PasswordLogInField.getText());

        rs=pst.executeQuery();
        if(rs.next()){
        JOptionPane.showMessageDialog(null, "Username & Password are correct");

        } else {
            JOptionPane.showMessageDialog(null, "Username & Password are incorrect");
            System.out.println("Logged in");
        }
    }
    catch(Exception e){

        JOptionPane.showMessageDialog(null, e);
        System.out.println("Not Logged in");
    }

而且:

    Connection cbconn = null;
    Statement stmt2 = null;

    String upcheck  = "SELECT USERNAME, PASSWORD FROM USERS";
        ResultSet rs = Statement.executeQuery(upcheck);

        while (results.next()) {
        String staffname = results.getString("snameeee");
        String password =  results.getString("SPwd");

           if ((f.equals(staffname)) && (s.equals(password))) {

              JOptionPane.showMessageDialog(null, "Username and Password exist");  
        }else {

         //JOptionPane.showMessageDialog(null, "Please Check Username and Password ");
        }
        results.close();
    } catch (SQLException sql) {

        out.println(sql);

1 个答案:

答案 0 :(得分:0)

我猜查询应该是:

Select PASSWORD from USERS where USERNAME = 'theNameInTextBox';