开发者

Java Connecting to remote MySQL database

开发者 https://www.devze.com 2023-04-12 10:48 出处:网络
Hey guys im trying to connect to my remote mysql database and my code gives me this error: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

Hey guys im trying to connect to my remote mysql database and my code gives me this error:

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.processMouseE开发者_开发百科vent(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)

This is my code:

          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 */ }
           }
       }


You need to add the JDBC Connector J driver for Mysql database at mysql-Downloads to your classpath. You can follow the instruction on adding the external jars to Eclipse here by going to project properties->Java Build Path -> Libraries->Add External JARs


This post should answer your questions. http://www.vogella.de/articles/MySQLJava/article.html You can obtain the needed mysql jar files from http://www.mysql.com/products/connector/


As Dave points it out, the first thing is to check if a jar containing the com.mysql.jdbc.Driver class is amongst your other libraries.

If not, add it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号