首页 > 解决方案 > 为什么在尝试验证用户名和密码时出现“java.awt.event.ActionEvent”错误?

问题描述

我正在创建一个连接到 SQLite 数据库的简单登录屏幕。连接工作 100%。

但是,当我尝试在登录屏幕上验证用户名和密码时,出现“java.awt.event.ActionEvent”错误。

我修改了好几次代码,都没有发现问题。

package Tutorial3;

import java.awt.EventQueue;
import java.sql.*;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Login {

    private JFrame frame;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Login window = new Login();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    Connection connection = null;
    private JTextField txtlogin;
    private JPasswordField pswsenha;

    /**
     * Create the application.
     */
    public Login() {
        initialize();
        connection = sqliteConnection.dbConnector();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 247, 210);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JLabel lbllogin = new JLabel("LOGIN");
        lbllogin.setBounds(10, 20, 48, 14);
        frame.getContentPane().add(lbllogin);

        JLabel lblsenha = new JLabel("SENHA");
        lblsenha.setBounds(10, 68, 48, 14);
        frame.getContentPane().add(lblsenha);

        txtlogin = new JTextField();
        txtlogin.setBounds(82, 11, 142, 33);
        frame.getContentPane().add(txtlogin);
        txtlogin.setColumns(10);

        pswsenha = new JPasswordField();
        pswsenha.setEchoChar('•');
        pswsenha.setBounds(82, 65, 142, 33);
        frame.getContentPane().add(pswsenha);

        JButton btnconfirmar = new JButton("Confirmar");
        btnconfirmar.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                 try {
                     String query="select * from employeeinfo where username=? and password=? ";
                     PreparedStatement pst=connection.prepareStatement(query);
                     pst.setString(1,txtlogin.getText() );
                     pst.setString(2,pswsenha.getText() );

                     ResultSet rs=pst.executeQuery();
                     int count=0;

                     while(rs.next()) {
                         count=count+1;

                     }

                     if(count==1)
                     {
                         JOptionPane.showMessageDialog(null, "Username and Password is correct");
                     }

                     else if(count>1) {
                         JOptionPane.showMessageDialog(null, "Duplicate Username and Password");
                     }

                     else {
                         JOptionPane.showMessageDialog(null, "Username and Password is not correct. Try again.");
                     }

                      rs.close();
                      pst.close();

                 }catch(Exception ex)
                    {
                        ex.printStackTrace();

                    }


            }
        });
        btnconfirmar.setBounds(10, 120, 214, 38);
        frame.getContentPane().add(btnconfirmar);
    }
}

org.sqlite.SQLiteException: [SQLITE_ERROR] SQL error or missing database (no such table: employeeinfo)
at sqlite.jdbc@3.30.1/org.sqlite.core.DB.newSQLException(DB.java:941)
at sqlite.jdbc@3.30.1/org.sqlite.core.DB.newSQLException(DB.java:953)
at sqlite.jdbc@3.30.1/org.sqlite.core.DB.throwex(DB.java:918)
at sqlite.jdbc@3.30.1/org.sqlite.core.NativeDB.prepare_utf8(Native Method)
at sqlite.jdbc@3.30.1/org.sqlite.core.NativeDB.prepare(NativeDB.java:134)
at sqlite.jdbc@3.30.1/org.sqlite.core.DB.prepare(DB.java:257)
at sqlite.jdbc@3.30.1/org.sqlite.core.CorePreparedStatement.<init>(CorePreparedStatement.java:47)
at sqlite.jdbc@3.30.1/org.sqlite.jdbc3.JDBC3PreparedStatement.<init>(JDBC3PreparedStatement.java:30)
at sqlite.jdbc@3.30.1/org.sqlite.jdbc4.JDBC4PreparedStatement.<init>(JDBC4PreparedStatement.java:19)
at sqlite.jdbc@3.30.1/org.sqlite.jdbc4.JDBC4Connection.prepareStatement(JDBC4Connection.java:35)
at sqlite.jdbc@3.30.1/org.sqlite.jdbc3.JDBC3Connection.prepareStatement(JDBC3Connection.java:241)
at sqlite.jdbc@3.30.1/org.sqlite.jdbc3.JDBC3Connection.prepareStatement(JDBC3Connection.java:205)
at Login$2.actionPerformed(Login.java:73)
at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308)
at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
at java.desktop/java.awt.Component.processMouseEvent(Component.java:6632)
at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
at java.desktop/java.awt.Component.processEvent(Component.java:6397)
at java.desktop/java.awt.Container.processEvent(Container.java:2263)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5008)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4840)
at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4918)
at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4547)
at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4488)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2772)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4840)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

在此处输入图像描述

标签: javaeclipse

解决方案


推荐阅读