Java应用程序不从本地数据库读取数据

时间:2014-12-07 16:55:29

标签: java mysql

我正在尝试使用NetBeans在Java中创建一个简单的登录表单。 插入用户并传递并按下Login按钮后,输出窗口显示:

java.lang.NullPointerException
at mms.Login.LoginLblMouseClicked(Login.java:85)
at mms.Login.access$000(Login.java:9)
at mms.Login$1.mouseClicked(Login.java:45)
at java.awt.Component.processMouseEvent(Component.java:6508)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4501)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

嗯,我知道这个错误意味着ResultSet变量等于null(因此在数据库中找不到特定用户),但我确定代码是正确的:

这是登录事件:

private void LoginLblMouseClicked(java.awt.event.MouseEvent evt) {                                      
    try
    {
        Database DB = new Database();

        String Username = UsernameFld.getText();
        String Password = PasswordFld.getText();

        if(DB.Connection())
        {
            String Query = "SELECT * FROM user WHERE username = '"+Username+"'" AND password = '"+Password+"';

            DB.setResultSet(DB.getStatement().executeQuery(Query));

            int nRecords = 0;

            if(DB.getResultSet().next())
            {
                nRecords++;
            }

            if(nRecords == 1)
            {
                System.out.println("User found.");
            }
            else
            {
                ErrorLbl.setText("User not found - Please check password field.");
            }
        }
        else
        {
            ErrorLbl.setText("Server Off.");
        }
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}     

N.B。 Connect()函数位于Database类中,它返回一个布尔类型。 这是功能:

public boolean Connection()
{
    try
    {
        Connection = DriverManager.getConnection(URL, dbUser, dbPass);
        return true;
    }
    catch(SQLException e)
    {
        System.out.println();
        return false;
    }
}

URL字符串的值为jdbc:mysql://localhost:3306/medical_suppliesdbUserdbPass“root”“”

嗯,medical_supplies数据库中的表有属性:

  1. 用户名
  2. 密码
  3. 并且唯一的记录是:

    username = 'admin' and password = 'admin'
    

    我已经在项目中包含了连接驱动程序,所以这不是问题。

    我希望有人会帮助我!谢谢大家!

0 个答案:

没有答案