好吧所以我有一个java fx应用程序,它将显示来自在同一台计算机上本地运行的mysql数据库的信息。我一直找不到司机。这是代码:
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();
}
}
这是堆栈跟踪:
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
非常感谢任何帮助
答案 0 :(得分: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)
这:Class.forName("com.mysql.jdbc.Driver");
还不够。
实际上,如果无法从运行时类路径中检索类,它会上升ClassNotFoundException
。
您只需要在运行时类路径中添加与已安装的MySQL版本匹配的MySQL JDBC驱动程序。
答案 1 :(得分:0)
问题在于Class.forName(“com.mysql.jdbc.Driver”); 获取java.lang.ClassNotFoundException的原因可能是加载驱动程序时出现问题。如果尚未添加,请将* .jar驱动程序添加到项目中。