-4

我正在创建一个简单的 java 应用程序,表单中有两个文本字段用于在按下按钮后传递用户名和密码,它应该是错误或正确的用户名和密码。它给出了例外

捕捉到异常

在执行查询中;

package myproject;

import java.sql.*;

public class connectionDB {

    String loadDriver = null;
    String url = null;
    String uName = null;
    String uPass = null;
    Connection con = null;
    Statement st = null;
    ResultSet rst = null;
    PreparedStatement pstmt;

    public connectionDB() {
        loadDriver = "com.mysql.jdbc.Driver";
        url = "jdbc:mysql://localhost/mydatabase";
        uName = "root";
        uPass = "water";
    }

    void Connect() {
        try {
            try {
                Class.forName(loadDriver);
                System.out.println("Successfully load");
            } catch (Exception e) {
                System.out.println("Fail to load");
            }
            con = DriverManager.getConnection(url, uName, uPass);

            System.out.println("Connect to mysql sucessfull");
            st = con.createStatement();
        } catch (SQLException e) {
            System.out.println("Something goes worng in Connection the mysql\n");

        }
    }

    boolean validateUserNamePassword(String uName, String uPass) {
        boolean isThere = false;
        int count = 0;
        try {


            pstmt = con.prepareStatement("select * from admin where "
                    + "Admin_Name= ? "
                   + "'and Admin_pass= ? ");


            pstmt.setString(1, uName);
            pstmt.setString(2, uPass);
            rst = pstmt.executeQuery();

            while (rst != null) {
                count = 1;
                isThere = true;
            }

        } catch (Exception e) {
            System.out.println("Exception caught in execution query");
        }
        if (isThere) {
            return isThere;
        } else {
            return isThere;
        }
    }

    void closeConnection() {
        try {
            st.close();
            con.close();
            System.out.println("Connection Sucessfully close with Database");
        } catch (Exception e) {
            System.out.println("Excepiotn caught");
        }
    }
}

编辑:这是错误的堆栈跟踪

运行: java.lang.NullPointerException else at myproject.connectionDB.validateUserNamePassword(connectionDB.java:48) at myproject.loginPage$Handler.actionPerformed(loginPage.java:96) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018 ) 在 javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341) 在 javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) 在 javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) 在 javax .swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252) 在 java.awt.Component.processMouseEvent(Component.java:6505) 在 javax.swing.JComponent.processMouseEvent(JComponent.java:3321) 在 java. awt.Component.processEvent(Component.java:6270) 在 java.awt.Container.processEvent(Container.java:2229) 在 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:4492) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422) at java.awt.Container.dispatchEventImpl(Container.java:2273) at java .awt.Window.dispatchEventImpl(Window.java:2719) 在 java.awt.Component.dispatchEvent(Component.java:4687) 在 java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723) 在 java.awt.EventQueue。访问 $200(EventQueue.java:103) 在 java.awt.EventQueue$3.run(EventQueue.java:682) 在 java.awt.EventQueue$3.run(EventQueue.java:680) 在 java.security.AccessController.doPrivileged(本机方法)在 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:696) at java.awt.EventQueue$4 .run(EventQueue.java:694) 在 java.security.AccessController.doPrivileged(Native Method) 在 java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) 在 java.awt.EventQueue.dispatchEvent(EventQueue.java: 693) 在 java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244) 在 java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163) 在 java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151) 在 java. awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147) 在 java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139) 在 java.awt.EventDispatchThread.run(EventDispatchThread.java:97) 建立成功(总时间:6 秒)

4

2 回答 2

5

查看您的查询:问题出在'. 它应该是这样的:

pstmt = con.prepareStatement("select * from admin where "
                + "Admin_Name= ? "
               + "and Admin_pass= ?");

加上你的validateUserNamePassword()方法:

if (isThere) {
        return isThere;
    } else {
        return isThere;
    }
}

这没有任何意义。

于 2013-09-16T04:19:26.203 回答
-1

你也应该输入mysql的端口号

尝试这个

url = "jdbc:mysql://localhost:3306/mydatabase";

3306是mysql的默认端口号。如果您有不同的端口号,请相应地更改它。

此外,您尝试检索的方式将是一个无限循环

 while (rst != null) {
                count = 1;
                isThere = true;
            }

使用这种方式

 while (rst.next()) {
                count = 1;
                isThere = true;
            }

你有一个'Vimal bera指出的,相应地改变它

于 2013-09-16T04:09:57.840 回答