我使用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)
我不知道这些错误是什么。谁能帮我?感谢。
BTW,我使用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
答案 0 :(得分:3)
如果你有一段表格的代码:
try {
A();
B();
} catch (Exception e) {}
并且B
没有被执行,这是一个非常安全的选择,A
正在引发某种类型的异常,尤其是,如果B
做了什么意味着如果订单被撤销。
在你的情况下:
catch (Exception e) {}
正在捕获异常,但绝对没有任何东西可以修复或报告它。 最低限度,您应该使用System.out.println (e);
或e.printStackTrace()
报告,以便了解正在发生的事情。
答案 1 :(得分:2)
确保将mysql.jar
添加到您的项目中。同样如评论所述,不要吞下你的例外。如果你这样做,你不知道错误是什么或。它可以是ClassNotFoundException
或SQLException
,它们完全不同。将ex.printStackTrace()
添加到catch
区块后,如果您获得ClassNotFoundException
,则可能忘记将mysql.jar
导入到项目中。如果您得到SQLExcpetion
,则SQL语法或连接语法错误
编辑以包含jar
.zip
.jar
添加到您的库中mysqul-connector.java...jar
并选择它Libraries
文件夹图标Add Jar/Folder
.jar
,然后选择它。.jar
即可。<强>更新强>
我认为MySQL.jar
随Netbeans IDE一起提供。
Libraries
文件夹图标答案 2 :(得分:0)
你应该添加Netbeans IDE附带的MySQL.jar。
您创建的项目中有一个Libraries文件夹。
右键单击该文件夹,然后单击“添加库”。
从可用的库中,单击MySQL JDBC Driver(如果使用MySQL)。