有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

eclipse java数据库与OOP的连接

我在一个包用户和登录窗口中创建了两个独立的类。我正在使用MySQL数据库

用户名:库存\u 01
密码:g29egXRvFTGmYqf4

但是登录窗口中的按钮ActionListener得到0值,因为用户类中的方法调用userLogin返回0值为什么

//userLogin method in User class

    protected int userLogin(String user_name,String password)
    {
    try
    {
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/inventory_01","Inventory_01","g29egXRvFTGmYqf4");
    Statement stmt = con.createStatement();
    String sql = "Select * from user where User_name='"+user_name+"' and Password='"+password+"'";
    ResultSet rs = stmt.executeQuery(sql);
                if(rs.next())
                {
                    a = a + 1;
                }
                else
                {
                    a = 0;
                }
                con.close();
            }
            catch (SQLException e) 
            {
                System.out.print(e);
            }
            catch(Exception e)
            {
                System.out.print(e);
            }
            return a;
        }

//class Login_window button actionlistener
JButton btnLogin = new JButton("Login");
        btnLogin.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent arg0) 
            {
                u.userLogin(txt_user_name.getText(), passwordField.getText());
                if(a == 1)
                {
                    Inventory_system i = new Inventory_system();
                    JOptionPane.showMessageDialog(null, "Login done!");
                }
                else
                {
                    JOptionPane.showMessageDialog(null, "Worng Password/Username !");
                }
            }
        });
        btnLogin.setBounds(12, 79, 97, 25);
        frame.getContentPane().add(btnLogin);

共 (0) 个答案