我在使用mysql时遇到了问题。看来,当程序运行到这个部分,它不会继续[解决]Class.forName在尝试连接到MySQL数据库时不起作用
try{
//Load the Driver Class
Class.forName("com.mysql.jdbc.Driver");
我认为在这部分代码存在问题。因为我想这一点,在JOptionPane的弹出:
try{
JOptionPane.showMessageDialog(null, "Ok!");
//Load the Driver Class
Class.forName("com.mysql.jdbc.Driver");
,但是,当我写这样的代码,没有任何反应:
try{
//Load the Driver Class
Class.forName("com.mysql.jdbc.Driver");
JOptionPane.showMessageDialog(null, "Ok!");
有什么建议?谢谢。
顺便说一句,这是我的整个程序
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String user = userName.getText();
final String URL = "jdbc:mysql://localhost:3306/videorental";
if(user.equals("")){
JOptionPane.showMessageDialog(null, "Fields should not be empty", "Error", JOptionPane.ERROR_MESSAGE);
}
else{
try{
//Load the Driver Class
Class.forName("com.mysql.jdbc.Driver");
//Create the connection using the static getConnection method
JOptionPane.showMessageDialog(null, "Connecting to database");
Connection con = DriverManager.getConnection(URL, "root", "");
//Create statement to execute the SQL statement
JOptionPane.showMessageDialog(null, "Creating a statement");
Statement stmt = con.createStatement();
//Check the username
ResultSet rs = stmt.executeQuery("select * from 'user_tbl' where user_id = '"+user+"' ");
if(rs.next()){
String uname = rs.getString("user_id");
if(user.equals(uname)){
JOptionPane.showMessageDialog(null, "Thank you!");
}
else{
JOptionPane.showMessageDialog (null, "You are not registered.");
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
所以我试图把e.printStackTrace(),对不起,我真的在使用MySQL中的新手。所以这里是结果... [解决]
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:190)
at user.loginFrame.jButton1ActionPerformed(loginFrame.java:113)
at user.loginFrame.access$000(loginFrame.java:14)
at user.loginFrame$1.actionPerformed(loginFrame.java:48)
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:3321)
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)
而且我不知道这些错误是什么。谁能帮我?谢谢。
顺便说一句,我使用netbeans作为IDE。
谢谢你们的帮助,但一个新的问题出现了,它说,我有MYSQLSYntaxError
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''user_tbl' where user_id = '12345678'' at line 1
'catch(Exception e){}' - ** AAAARRRRGGGGHHH **。不要吞噬异常,否则你不知道发生了什么......就像这里。 – Mat
严重的是,把'e.printStackTrace();'到catch块,并张贴堆栈跟踪也看到有什么问题.. –
@KubaSpatny还是放弃了追赶完全..我非常罕见的有抓“打印材料”。有用地处理它或GTEO(得到例外)。 – user2864740