0

ok so i have a java fx app that will display info from a mysql database runing locally on the same computer. i keep getting driver not found. here is the code:

  String databaseURL = "jdbc:mysql://localhost:3307/mysql?zeroDateTimeBehavior=convertToNull";
            String user = "user";
            String password = "password";
            Connection conn = null;
            try {
                Class.forName("com.mysql.jdbc.Driver");
                conn = DriverManager.getConnection(databaseURL, user, password);
                if (conn != null) {
                    System.out.println("Connected to the database");
                }
            } catch (ClassNotFoundException ex) {
                System.out.println("Could not find database driver class");
                ex.printStackTrace();
            } catch (SQLException ex) {
                System.out.println("An error occurred. Maybe user/password is invalid");
                ex.printStackTrace();
            } finally {
                if (conn != null) {
                    try {
                        conn.close();
                    } catch (SQLException ex) {
                        ex.printStackTrace();
                    }
                }

and here is the stack trace:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
    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:259)
    at got_map.GOT_Map.start(GOT_Map.java:130)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(LauncherImpl.java:821)
    at com.sun.javafx.application.LauncherImpl$$Lambda$51/1468396900.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(PlatformImpl.java:323)
    at com.sun.javafx.application.PlatformImpl$$Lambda$44/584634336.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$164(PlatformImpl.java:292)
    at com.sun.javafx.application.PlatformImpl$$Lambda$47/1960854798.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(PlatformImpl.java:291)
    at com.sun.javafx.application.PlatformImpl$$Lambda$45/501263526.run(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$141(WinApplication.java:102)
    at com.sun.glass.ui.win.WinApplication$$Lambda$37/96639997.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)
Apr 21, 2017 6:29:06 PM javafx.fxml.FXMLLoader$ValueElement processValue
WARNING: Loading FXML document with JavaFX API of version 8.0.111 by JavaFX runtime of version 8.0.20

Any help would be much appreciated

JavaScrub
  • 133
  • 9
  • Are you using a build tool like maven? If not, you need to add the MySQL JDBC driver to the build path. – kevcodez Apr 21 '17 at 17:49

2 Answers2

2
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    ...    
    at java.lang.Class.forName(Class.java:259)
    at got_map.GOT_Map.start(GOT_Map.java:130)

This : Class.forName("com.mysql.jdbc.Driver"); is not enough.
Indeed, it rises a ClassNotFoundException if the class cannot be retrieved from the runtime classpath.

You need just to add in the runtime classpath the MySQL JDBC driver matching with the installed MySQL version.

davidxxx
  • 125,838
  • 23
  • 214
  • 215
0

The problem is in the line, Class.forName("com.mysql.jdbc.Driver"); The reason for getting java.lang.ClassNotFoundException can be an issue in loading the driver.Add the *.jar driver to your project if you haven't added it.

Shav_I
  • 126
  • 1
  • 7