例外Occured com.mysql.jdbc.Driver

时间:2014-04-19 18:50:28

标签: java mysql jdbc

我很难与JBDC连接。我不知道我哪里做错了?请帮我指出我在哪里犯了错误以及异常错误背后的原因:发生了异常com.mysql.jdbc

private void btnInsertActionPerformed(java.awt.event.ActionEvent evt)            
{
  String code = txtCode.getText();
  String name = txtName.getText();
  String cell_num = txtCell.getText();
  String adrs = txtAdd.getText();

 Connection conn = null;
 try{
      Class.forName("com.sql.jdbc.Driver");
    }
  catch(ClassNotFoundException e)
    {
       JOptionPane.showMessageDialog(null,"Exception Occured"+e.getMessage());
    }

 try{conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/ManagementSystem"+","+ user);

    String query ="INSERT INTO EmployeeForm1(EmployeeCode,Name,Cell_No, Address) VALUES('"+code+","+name+","+cell_num+","+ "'" + adrs+"')";

        PreparedStatement preparedStatement = conn.prepareStatement(query);
        ResultSet resultSet = preparedStatement.executeQuery();
        resultSet.close();
        JOptionPane.showMessageDialog(null,"INSERTED SUCCESSFULLY");
     }catch(SQLException se){
  //Handle errors for JDBC
         se.printStackTrace();
     }catch(Exception e){
  //Handle errors for Class.forName
         e.printStackTrace();
     }finally{
  //finally block used to close resources
         try{
             if(conn!=null)
                 conn.close();
         }catch(SQLException se){
             se.printStackTrace();
         }//end finally try
}                                         
}

这是堆栈跟踪:

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/ManagementSystem,root
at java.sql.DriverManager.getConnection(DriverManager.java:596)
at java.sql.DriverManager.getConnection(DriverManager.java:233)
at managementsystem1.EmployeeForm.btnInsertActionPerformed(EmployeeForm.java:120)
at managementsystem1.EmployeeForm.access$000(EmployeeForm.java:12)
at managementsystem1.EmployeeForm$1.actionPerformed(EmployeeForm.java:53)
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)`

2 个答案:

答案 0 :(得分:0)

只是一个简单的错字:)你有:

Class.forName("com.sql.jdbc.Driver");

这应该是:

Class.forName("com.mysql.jdbc.Driver");< - 注意com。 mysql 而不是com。 sql

原因是因为当ClassLoader加载类时,MySQL驱动程序会将自己添加到静态块中的DriverManager注册表中。通过调用Class.forName(或通过导入等取决于类),您明确要求Java加载它,从而在DriverManager中注册mysql jdbc命名空间。

请参阅MySQL Connector/J Documentation。另外,请查看旧版本的MySQL Driver class source以查看使用DriverManager注册驱动程序的静态块。

答案 1 :(得分:-1)

添加" mysql-connector-5.1.6.jar"进入你的java的类路径。如果您遇到任何问题或将jar放入java的类路径dosnt中,请尝试在Eclipse IDE下构建项目

在" Build Path" - > "配置构建路径.." 添加外部jar,这将是" mysql-connector-5.1.6.jar"