我使用mysql制作了一个简单的java登录表单,安装了所有驱动程序等,并且我使用了另一个j2e项目中的连接代码,该代码有效,但登录不会在这里传递我的代码和错误日志,管理测试工作正常
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String login = user.getText();
String pass = password.getText();
if(login.equals("admin")&&pass.equals("admin")) {
JOptionPane.showMessageDialog(null,"hi admin ");
} else {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("driver MySQL JDBC charged");
} catch (ClassNotFoundException e) {
/*e.printStackTrace();*/
System.out.println("Error charging driver");
}
}
try {
DriverManager.getConnection("jdbc:odbc:bd");
rs = st.executeQuery("select * from users where nom='" + login + "' and passwd='" + pass+ "' ");
if(rs.next())
JOptionPane.showMessageDialog(null,"correct login ");
else
JOptionPane.showMessageDialog(null, "incorect login");
} catch (SQLException e) {
System.out.println("Error etablishing connexion");
}
}
错误:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javaapplication1.NewJFrame.jButton1ActionPerformed(NewJFrame.java:141)
at javaapplication1.NewJFrame.access$000(NewJFrame.java:18)
at javaapplication1.NewJFrame$1.actionPerformed(NewJFrame.java:49)
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)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
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: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)
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)
最佳答案
您的连接出错..
DriverManager.getConnection("jdbc:odbc:bd");
rs = st.executeQuery("select * from users where nom='" + login + "' and passwd='" + pass + "' ");
rs 结果集未定义 语句未定义 连接也未正确存储..
检查这个..
Class.forName("com.mysql.jdbc.Driver");
Connection conn = null;
conn = DriverManager.getConnection("jdbc:mysql://hostname:port/dbname","username", "password");
System.out.println("-------- MySQL JDBC Connection Testing ------------");
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("Where is your MySQL JDBC Driver?");
e.printStackTrace();
return;
}
System.out.println("MySQL JDBC Driver Registered!");
Connection connection = null;
try {
connection = DriverManager
.getConnection("jdbc:mysql://localhost:3306/mkyongcom","root", "password");
} catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
e.printStackTrace();
return;
}
if (connection != null) {
System.out.println("You made it, take control your database now!");
} else {
System.out.println("Failed to make connection!");
}
}
关于java - 登录表单 java mysql 在 netbeans 上失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22909490/