【发布时间】:2021-11-19 15:08:47
【问题描述】:
我花了很多时间试图弄清楚这一点,并遵循了许多指示,但无济于事。
我正在尝试将我的程序与我的 MySQL 数据库连接起来,我已经安装了 MySQL 驱动程序,或者至少我认为我已经安装了,我已将其添加到类路径中,然后我还将它安装为用户库。
然后当我通过命令行编译它时,我得到一个错误: 错误:com.mysql.jdbc 包不存在
我什至下载了驱动程序 jar 的早期版本,但没有成功,我可能花了整整 8 个小时来解决这个问题
我的代码:
String[] settings = new String[10];
try {
FileReader fr = new FileReader("settingsFile.txt");
BufferedReader textReader = new BufferedReader(fr);
for(int i=0; i <=9; i++){
settings[i] = textReader.readLine();
}
textReader.close();
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Unable to connect to database.\nSettings file Not Found");
}
String host = settings[2];
String uName = settings[3];
String uPass = settings[4];
try {
String driver = "com.mysql.jdbc.Driver";
Class.forName(driver).newInstance();
Connection con = DriverManager.getConnection(host, uName, uPass);
JOptionPane.showMessageDialog(null, "Connection to database established");
}
catch ( SQLException | ClassNotFoundException | IllegalAccessException | InstantiationException err ) {
JOptionPane.showMessageDialog(null, err.getMessage( ) + "\n\nDid not connect.");
}
}
请帮忙,我想我会对此感到沮丧。
【问题讨论】:
-
我正在尝试连接我的脚本 - 什么脚本?
-
您将 MySQL 连接器 JAR 放在哪里?你是如何构建和运行你的程序的?
-
请分享您的异常的完整堆栈跟踪,这会很有帮助。
-
jar文件的名称是mysql-connector-java-5.0.8-bin.jar,我用的是Eclipse,jar在项目文件夹内的一个文件夹中。
-
我是自学成才,对 Java 还是很陌生,很抱歉我不知道那是什么。