Java连接到远程MySQL数据库

时间:2011-10-12 23:04:35

标签: java mysql eclipse jdbc classpath

嘿伙计们我试图连接到我的远程mysql数据库,我的代码给了我这个错误:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at Login.login(Login.java:116)
at Login.actionPerformed(Login.java:97)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)Logging in with credintials (wqdq, dqww)

at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

这是我的代码:

          Connection conn = null;

       try
       {

           String url = "jdbc:mysql://myhost:3306/mydb";
           Class.forName ("com.mysql.jdbc.Driver");
           conn = DriverManager.getConnection (url,"myuser","mypass");
           System.out.println ("Database connection established");
       }
       catch (Exception e)
       {
           e.printStackTrace();

       }
       finally
       {
           if (conn != null)
           {
               try
               {
                   conn.close ();
                   System.out.println ("Database connection terminated");
               }
               catch (Exception e) { /* ignore close errors */ }
           }
       }

3 个答案:

答案 0 :(得分:1)

您需要将mysql-Downloads的Mysql数据库的JDBC Connector J驱动程序添加到类路径中。您可以通过转到project properties->Java Build Path -> Libraries->Add External JARs

按照有关将外部jar添加到Eclipse here的说明进行操作

答案 1 :(得分:0)

这篇文章应该回答你的问题。 http://www.vogella.de/articles/MySQLJava/article.html 您可以从http://www.mysql.com/products/connector/

获取所需的mysql jar文件

答案 2 :(得分:0)

正如Dave指出的那样,首先要检查包含com.mysql.jdbc.Driver类的jar是否在您的其他库中。

如果没有,请添加它。