有人可以向我解释一下为什么当我在 Eclipse IDE 中运行应用程序时以下代码有效,但在服务器上编译和运行时它会生成异常吗? 我只是不明白....
public class Database {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
public Database() throws SQLException {
String host = "localhost:3306";
String schema = "stickmanDatabase";
String user = "richard";
String password = "xxxxxxxx";
String url = "jdbc:mysql://" + host + "/" + schema + "?user=" + user + "&password=" + password;
try {
conn = DriverManager.getConnection(url);
} catch (SQLException e) {
e.printStackTrace();
throw e;
}
}
}
这是我得到的堆栈跟踪:
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/stickmanDatabase?user=richard&password=xxxxxxxx
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at stickmanPyramidScheme.server.Database.<init>(Database.java:23)
at stickmanPyramidScheme.server.LoginVerification.verifyLogin(LoginVerification.java:10)
at stickmanPyramidScheme.server.StickmanServer.parseAndExecute(StickmanServer.java:55)
at stickmanPyramidScheme.server.StickmanServer.main(StickmanServer.java:32)
我的理解是,问题是我需要构建路径中的文件“mysql-connector-java-5.1.18-bin.jar,我这样做了。如果我删除它,代码在运行时不再有效在 Eclipse 中。
如有任何帮助,我们将不胜感激。谢谢。
...
根据 Andrew 的要求...我在 Ubuntu Linux 服务器上使用 Eclipse。 我可以看到“mysql-connector-java-5.1.18-bin.jar”包含“com.mysql.jdbc”,其中包含“Driver.class” jar 被添加到(应用程序目录)/WEB-INF/lib 目录中。仍然出现同样的错误。
最佳答案
您不仅需要在 IDE 中安装驱动程序 jar 文件,还需要将其安装在服务器上。你没有说这是什么类型的服务器,但 jar 需要在你的服务器进程的类路径上;即,在您的 Web 应用程序的 WEB-INF/lib
目录中,或者简单地在服务器 JVM 的 -classpath
参数中指示。
关于java - 运行时为 "No suitable driver",但不在 IDE 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9755756/