这是我的第一篇文章,所以如果我有任何错误,我深表歉意!
我在 Eclipse 中使用 Java 在 MySQL 数据库中创建表。一切正常,直到前几天莫名其妙地停止工作。我的代码运行了,但是当我尝试初始化连接时,由于错误,数据库没有任何反应。代码实际上运行到最后,错误只有在我单步执行代码时才能看到。
这是我的 TableCreator 类的构造函数:
public TableCreator() throws SQLException {
host = "jdbc:mysql://localhost:3306/test";
dbName = "root";
dbPass = "pass";
conn = DriverManager.getConnection(host, dbName, dbPass);
query = conn.createStatement();
initialise();
System.out.println("Success.");
}
错误发生在“conn = DriverManager.getConnection(host, dbName, dbPass);”这一行。
在我尝试执行这一行之后,堆栈看起来像这样:
TableCreator (1) [Java Application]
biz.cogitare.gpsperformancetool.TableCreator at localhost:53389
Thread [main] (Suspended)
Driver(NonRegisteringDriver).connect(String, Properties) line: 306
DriverManager.getConnection(String, Properties, Class<?>) line: not available
DriverManager.getConnection(String, String, String) line: not available
TableCreator.<init>() line: 23
TableCreator.main(String[]) line: 49
Daemon Thread [Abandoned connection cleanup thread] (Running)
C:\Program Files\Java\jre1.8.0_45\bin\javaw.exe (9 Jul 2015 12:27:27)
我花了很多时间在互联网上搜索有关如何解决此问题的帮助,但到目前为止我还没有成功。
如有任何帮助,我们将不胜感激。
谢谢!
最佳答案
有几种情况会导致这种行为。
检查驱动是否加载。
如果您的 JDBC 驱动程序早于 4.0,您需要确保驱动程序已加载
Class.forName("com.mysql.jdbc.Driver")
或类似的字符串,具体取决于您使用的特定驱动程序。
也许您在执行此代码之前正在执行另一个加载驱动程序的操作,这就是它起作用的原因。也许您没有执行该操作,这就是为什么这段代码尽管是相同的代码但仍然失败的原因。
检查驱动文件是否在类路径中。
如果您收到 ClassNotFoundException,则表示 JVM 无法找到您的类。如果您收到 LinkageError,则表示存在版本冲突。获取最新的驱动程序并重新安装。
检查驱动是否正确导入eclipse。
按照以下说明导入它:How to import a jar in Eclipse
如果一切都失败了,请重新安装。
卸载java,删除eclipse,重新安装一切。
以正确的方式插入驱动程序并开始一个新项目并将您的类复制过来。
关于java - MySQL 非注册驱动程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31316184/