Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使JFrame登录表单工作?_Java_Mysql_Swing_Jdbc_Login - Fatal编程技术网

Java 如何使JFrame登录表单工作?

Java 如何使JFrame登录表单工作?,java,mysql,swing,jdbc,login,Java,Mysql,Swing,Jdbc,Login,我正在用eclipse上的swing做一个java库管理项目。当我点击登录按钮时,什么也没发生。mysql jdbc驱动程序已加载,我正在获取连接,但没有显示标签。请告诉我该怎么办。这是我的密码 import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.sql.*; public class Loginscreen implements ActionLis

我正在用eclipse上的swing做一个java库管理项目。当我点击登录按钮时,什么也没发生。mysql jdbc驱动程序已加载,我正在获取连接,但没有显示标签。请告诉我该怎么办。这是我的密码

    import java.awt.*;  
    import javax.swing.*;
    import java.awt.event.*;
    import java.sql.*;
    public class Loginscreen implements ActionListener {

    JFrame logframe;
    JButton bt;
    JLabel lid, lpass, lmsg;
    JTextField tid;
    JPasswordField pass;
    JPanel p1;
    Font ft, ft2;
    Connection conn;

    public Loginscreen() {

        logframe = new JFrame("Login");
        logframe.setSize(1366, 720);
        logframe.setVisible(true);
        lid = new JLabel("User ID");
        lpass = new JLabel("Password");
        lmsg = new JLabel();
        ft = new Font("Arial", Font.BOLD, 30);
        ft2 = new Font("Arial", Font.PLAIN, 20);
        tid = new JTextField();
        pass = new JPasswordField();
        p1 = new JPanel();
        bt = new JButton("Login");
        logframe.add(p1);
        p1.setLayout(null);
        p1.setBackground(new Color(160, 82, 45));//Background Color Set
        lid.setBounds(450, 200, 500, 30);
        p1.add(lid);
        lid.setFont(ft);
        tid.setBounds(650, 200, 200, 30);
        p1.add(tid);
        tid.setFont(ft2);
        lpass.setBounds(450, 300, 500, 30);
        p1.add(lpass);
        lpass.setFont(ft);
        pass.setBounds(650, 300, 200, 30);
        p1.add(pass);
        bt.setBounds(600, 430, 100, 50);
        p1.add(bt);
        bt.setFont(new Font("Segoe Script", Font.BOLD, 22));
        bt.setBackground(new Color(173, 216, 230));
        lmsg.setBounds(450, 510, 500, 30);
        p1.add(lmsg);
        bt.addActionListener(this);

    }

    public void actionPerformed(ActionEvent ae) {
        if (ae.getSource() == bt) {
            try {
                Class.forName("com.mysql.jdbc.Driver");
                conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_lib", "root", "admin");
                String i = tid.getText();//get the user id
                char[] pwd = pass.getPassword();//get the password
                String spwd = new String(pwd);//converting char to string
                String query = "select * from tbl_user where id=? and password=?";
                PreparedStatement p = conn.prepareStatement(query);
                p.setString(1, i);
                p.setString(2, spwd);
                ResultSet rs = p.executeQuery();
                int count = 0;
                while (rs.next()) {
                    count = count + 1;
                }
                if (count == 1) {
                    lmsg.setText("Invalid User ID or Password");
                } else {
                    lmsg.setText("Invalid User ID or Password");
                }
                rs.close();
                conn.close();
            } catch (Exception e) {
                System.out.println(e);
            }
        }
    }
}

我想当它成功时,你不会设置文本

还要确保

while(rs.next())
{
count=count+1;
}
终止。 还要针对SQL注入保护您的查询(刚刚意识到这一点)

对于这类调试问题,我建议进行单元测试(基本上只是测试程序运行的距离、需要多长时间、是否终止等)

另外,在您发布问题之前,请帮我们添加注释并正确设置GUI内容的格式……这看起来很可怕:/

编辑:


对我来说没有意义。。。无论发生什么情况,您都会返回一个错误

只是一个注释,在添加所有组件之前,您不应该使框架可见。请打印您的
spwd
i
好吗?您的程序与我配合得很好
  if(count==1)
        {
            lmsg.setText("Invalid User ID or Password");
        }
        else
        {
            lmsg.setText("Invalid User ID or Password");
        }