我正在尝试使用sqlite在Java中创建一个登录应用程序,但是...... [英] I'm trying to create a login application in Java using sqlite, but...

查看:68
本文介绍了我正在尝试使用sqlite在Java中创建一个登录应用程序,但是......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我一直收到用户名和密码不正确消息对话框。



在我的员工表中有:

EID,FirstName,Surname,用户名,密码,电子邮件







Why do i keep getting the "Username and Password Incorrect" message dialog.

In my Employee table there are :
EID, FirstName, Surname, Username, Password , email



import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;

class frame2 extends JFrame implements ActionListener{
    JTextField username ;
    JTextField password ;
    JButton login ;
    JLabel UserLabel ;
    JLabel PassLabel ;

    Connection connection = null;
    ResultSet rs = null;
    PreparedStatement pst = null;

public frame2(){
    super("Login:");
    
   
    setDefaultCloseOperation(JFrame.EXIT_ON_­CLOSE);
    setSize(500,500);
    GridBagLayout g = new GridBagLayout();
    GridBagConstraints c = new GridBagConstraints();
    setLayout(g);
    
    c.gridx = 0;
    c.gridy = 0;
    UserLabel = new JLabel("Username");
    add(UserLabel,c); 
    
    c.gridx = 2; //for username txtfield
    c.gridy = 0;
    username = new JTextField(15); //name of textfield 
    add(username,c); 
    
    c.gridx = 0; //for password label
    c.gridy = 1;
    PassLabel = new JLabel("Password");
    add(PassLabel,c); 
    
    c.gridx = 2; //for username txtfield
    c.gridy = 1;
    password = new JTextField(15);
    add(password,c);
    
    c.gridx = 2;//login button 
    c.gridy = 3;
    c.fill = GridBagConstraints.HORIZONTAL;
    c.insets = new Insets(10,0,0,0);  //top padding
    login = new JButton("Login");
    add(login,c);

    
    login.addActionListener(this);
    setVisible(true);
}

public void actionPerformed(ActionEvent e) {
    if(e.getSource() == login){
 
        
        String sql = "select from Employee where Username=? and Password=?";
        try{
            pst = connection.prepareStatement(sql) ;
            pst.setString(1, username.getText());
            pst.setString(2, password.getText());
            
            rs = pst.executeQuery();
            if(rs.next()){
                 JOptionPane.showMessageDialog(null, "Username and Password Correct");
                 setVisible(false); //closes login page
            }
            
        }
        catch(Exception ex){
            setVisible(true); //frame is still visible
            JOptionPane.showMessageDialog(null, "Username and Password Incorrect");
        }
    }
   
}

    public static void main(String[] args){
        frame2 l = new frame2();
}

    
}

推荐答案

请简要介绍一下你的完整问题可以解决它。
Please, brief your complete issues than i can solve it.


你初始化
connection



从您的代码中,我只看到

?
From your code, I just see

connection = null;



这就是为什么发生异常,从而执行该行


That's why exception happened, and thus executed the line

JOptionPane.showMessageDialog(null, "Username and Password Incorrect");


这篇关于我正在尝试使用sqlite在Java中创建一个登录应用程序,但是......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆