我正在创建一个简单的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在myproject.connectionDB.validateUserNamePassword(connectionDB.java:48)的myproject.loginPage $ Handler.actionPerformed(loginPage.java:96)at javax.swing.AbstractButton.fireActionPerformed(AbstractButton。 java:2018)at javax.swing.AbstractButton $ Handler.actionPerformed(AbstractButton.java:2341)at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259 )javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)at java.awt.Component.processMouseEvent(Component.java:6505)at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)在java.awt.Component.processEvent(Component.java:6270)的java.awt.Container.processEvent(Container.java:2229),位于java.awt的java.awt.Component.dispatchEventImpl(Component.java:4861)。 java.awt.Component.dispatchEvent中的Container.dispatchEventImpl(Container.java:2287)(Component.java:46 87)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)位于java.awt.Eind上的java.awt.Window.dispatchEventImpl(Window.java:2719),java.awt.EventQueue.dispatchEventImpl,java.awt.Component.dispatchEvent(Component.java:4687) (EventQueue.java:723)java.awt.EventQueue.access $ 200(EventQueue.java:103)java.awt.EventQueue $ 3.run(EventQueue.java:682)at java.awt.EventQueue $ 3.run(EventQueue) .java:680)java.security.ProtectionDomain上的java.security.AccessController.doPrivileged(Native Method),java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:87)中的$ 1.doIntersectionPrivilege(ProtectionDomain.java:76)at Java.awt.EventQueue $ 4.run(EventQueue.java:696)java.awt.EventQueue $ 4.run(EventQueue.java:694)at java.security.AccessController.doPrivileged(Native Me) java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:76)at java.awt.EventQueue.dispatchEvent(EventQueue.java:693)at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)at java在java.awt.EventDispatchThread的java.awt.EventDispatchThread。 java.awt.EventDispatchThread.run中的pumpEvents(EventDispatchThread.java:139)(EventDispatchThread.java:97)BUILD SUCCESSFUL(总时间:6秒)
答案 0 :(得分:5)
查看您的查询:问题出在'
。它应该是这样的:
pstmt = con.prepareStatement("select * from admin where "
+ "Admin_Name= ? "
+ "and Admin_pass= ?");
另外还有validateUserNamePassword()
方法:
if (isThere) {
return isThere;
} else {
return isThere;
}
}
这没有任何意义。
答案 1 :(得分:-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 指出,相应地改变