英文:
ClassNotFound & No Suitable Driver, where did I go wrong?
问题
我已经创建了一个在NetBeans的IDE中完美运行的程序,但是每次构建.jar文件时,数据库连接就会停止工作。我已经将MySQL连接器的CLASSPATH添加到了项目中,并且使用了Class.forName("com.mysql.jdbc.Driver")来定义驱动程序。但仍然没有任何作用。
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/gearheads";
Connection conn = DriverManager.getConnection(url, "root", "HellOnEarth202021");
Statement stmt = conn.createStatement();
ResultSet rs;
String pid = txt_staffID.getText();
rs = stmt.executeQuery("SELECT name,timeraccess FROM staff WHERE staffid = '" + pid + "'");
while (rs.next()) {
String timeraccess = rs.getString("timeraccess");
String staffName = rs.getString("name");
getLogin = staffName;
System.out.println(staffName);
System.out.println("Users Admin Level: " + timeraccess);
if (timeraccess.equals("1")) {
this.dispose();
new menu().setVisible(true);
System.out.println("Access Granted");
} else
System.out.println("Access Restricted.");
}
conn.close();
} catch (Exception e) {
System.err.println("出现异常!");
txt_staffID.setText(e.toString());
System.err.println(e.getMessage());
}
目前的输出如下:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
如果去掉Class.forName部分的代码,则输出为:
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/gearheads
请帮我看看。这个链接是我的库房截图。
我的库房:
英文:
I have created a program that works perfectly in the IDE on NetBeans, but anytime I build the .jar file, the database connection stops working. I've already added the CLASSPATH to the MySQL Connector, as well as defined Class.forName("com.mysql.jdbc.Driver"); Still nothing..
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/gearheads";
Connection conn = DriverManager.getConnection(url,"root","HellOnEarth202021");
Statement stmt = conn.createStatement();
ResultSet rs;
String pid = txt_staffID.getText();
rs = stmt.executeQuery("SELECT name,timeraccess FROM staff WHERE staffid = '"+pid+"'");
while ( rs.next() ) {
String timeraccess = rs.getString("timeraccess");
String staffName = rs.getString("name");
getLogin = staffName;
System.out.println(staffName);
System.out.println("Users Admin Level: " + timeraccess);
if ( timeraccess.equals("1")) {
this.dispose();
new menu().setVisible(true);
System.out.println("Access Granted");
}else
System.out.println("Access Restricted.");
}
conn.close();
} catch (Exception e) {
System.err.println("Got an exception! ");
txt_staffID.setText(e.toString());
System.err.println(e.getMessage());
}
This, as it stands, outputs:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
Where taking out the Class.forName outputs:
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/gearheads
Please help me. This link is a screenshot of my libraries.
My Libraries:
答案1
得分: 1
如果在NetBeans中运行正确,则MySQL驱动程序是正确的。
您需要搜索清单文件(MANIFEST.MF),可能需要将驱动程序的内容解压缩到您自己的JAR文件中。
<del>目前我会暂时离开这个回答,但我会努力收集细节,将其带回这里。</del>
我也使用NetBeans,当我生成JAR文件时,它已经复制了相关的库并生成了正确的MANIFEST.MF。
我最终得到了以下的结构
<pre>MyProject\dist\lib\JdbcDriver.jar
和
MyProject\dist\MyProject.jar</pre>
在MyProject.jar中,我有以下的MANIFEST.MF:
<pre>Manifest-Version: 1.0
Ant-Version: Apache Ant 1.9.7
Created-By: 1.8.0_201-b09 (Oracle Corporation)
Class-Path: lib/JdbcDriver.jar
X-COMMENT: Main-Class将会被构建自动添加
Main-Class: myproject.MyMainClass</pre>
请确保您将lib文件夹与您的JAR文件一起复制。
只是为了在这里留下最终的解决方案(@Nick Media的最终评论):您需要在“Build> Packaging Project Properties”中勾选“Copy Dependent Libraries”。
英文:
If its runnning correctly inside Netbeans, then the MySQL driver is correct.
You have to search for Manifest file (MANIFEST.MF) and probably will have to unjar the contents of the driver inside your own JAR file.
<del>I'll leave this answer by now, but will try to gather the details to bring it back here.</del>
I also use NetBeans and when I generate the JAR file, its already copying the dependant libraries and generating the correct MANIFEST.MF.
I ended up with the following structure
<pre>MyProject\dist\lib\JdbcDriver.jar
and
MyProject\dist\MyProject.jar</pre>
Inside MyProject.jar, I have the following MANIFEST.MF:
<pre>Manifest-Version: 1.0
Ant-Version: Apache Ant 1.9.7
Created-By: 1.8.0_201-b09 (Oracle Corporation)
Class-Path: lib/JdbcDriver.jar
X-COMMENT: Main-Class will be added automatically by build
Main-Class: myproject.MyMainClass</pre>
Please, verify with you are copying the lib folder altogether with your JAR file.
Just to leave here the final solution (@Nick Media final comment): you have to check "Copy Dependent Libraries" in Build>Packaging Project Properties.
答案2
得分: 0
并不清楚你到底做错了什么,但显然你某些地方做错了:
-
通常最好使用
DriverManager.getConnection
而不是Class.forName
以及特定的驱动程序类名。这是Oracle Java教程推荐的方法;参见https://docs.oracle.com/javase/tutorial/jdbc/basics/connecting.html -
如果你通过类名加载驱动程序,对于MySQL Connector/J 5.x,请使用
com.mysql.jdbc.Driver
,对于MySQL Connector/J 8.x,请使用com.mysql.cj.jdbc.Driver
。 -
如果这是一个Web应用程序,请确保相关的驱动程序JAR文件实际上在你的WAR文件中,和/或者确保它被正确地部署。
-
如果这是一个命令行应用程序,请确保你在运行时类路径中包含驱动程序JAR文件。(特别是如果你尝试在IDE之外运行它。)或者,考虑创建一个包含所有应用程序依赖项的Shaded JAR。(很难给出准确建议,因为你没有告诉我们你是否在使用构建工具,如Ant、Maven、Gradle等。)
-
驱动程序实际上不需要成为编译时依赖,尽管这样做没有什么坏处。(坏处是你可能会不小心将MySQL实现类的导入添加到你的应用程序中...并遇到问题。)
英文:
It is not clear exactly what you are doing wrong, but you are clearly doing something incorrectly:
-
It is often better to use
DriverManager.getConnection
rather thanClass.forName
and a specific driver class name. This is the approach recommended by the Oracle Java Tutorial; see https://docs.oracle.com/javase/tutorial/jdbc/basics/connecting.html -
If you are loading the driver via its classname, use
com.mysql.jdbc.Driver
with MySQL Connector/J 5.x andcom.mysql.cj.jdbc.Driver
with MySQL Connector/J 8.x. -
If this is a webapp, make sure that the relevant driver JAR file is actually in your WAR file, and/or that it is being deployed correctly.
-
If this is a command line app, make sure that you include the driver JAR on the runtime classpath. (Especially if you are trying to run it outside of your IDE.) Alternatively, consider creating a shaded JAR that included all of the apps dependencies. (It is a bit hard to advise since you haven't told us if you are using a build tool like Ant, Maven, Gradle, etc.)
-
The driver doesn't actually need to be a compile time dependency, though there is little harm in doing that. (The harm is that you might accidentally add imports to the MySQL implementation classes to your app ... and run into problems.)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论