【发布时间】:2019-05-15 11:03:49
【问题描述】:
我使用 NetBeans 8 做了一个项目,我必须连接到 mysql 数据库并在表格中显示内容。 当我使用 IDE 时,连接工作得很好!但是当我清理并构建以创建可执行 jar 时,我的程序无法连接到数据库...... 谁能帮我解决这个问题? 谢谢!
这是我正在使用的代码:
@FXML
private void loadDB() {
//cleaning all fields from table View and removing previous elements from studentList before show new content;
tableView.getItems().clear();
studentList.removeAll();
String stg = dataBaseField.getText();
//Expected string format:
//jdbc:mysql://mymysql.senecacollege.ca/eden_burton?user=jac444_183a01&password=eqXE@4464
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("Error loading the driver" + e);
}
try {
//connecting to the database
Connection conexao = DriverManager.getConnection(dataBaseField.getText());
//creating a statement and query to be executed
Statement myStmt = conexao.createStatement();
ResultSet result = myStmt.executeQuery("select * from Students");
//look at the result set
while (result.next()) {
Student student_ = new Student(result.getInt("id"), result.getString("name"), result.getString("course"), result.getInt("grade"));
studentList.addNaLista(student_);
tableView.setItems(getStudent(studentList.getList()));
}
myStmt.close();//closing statement
result.close();//closing results
conexao.close();//closing connection
} //if a connection could not be set.
catch (SQLException exc) {
alert("CONNECTION ERROR!", "Please verify your connection string.", "String should be in the following format: " + "\n\njdbc:mysql://mymysql.senecacollege.ca/DATABASENAME?user=USERNAME&password=PASSWORD");
}
}
【问题讨论】:
-
错误是什么?
-
我们很乐意为您提供帮助,但我们需要更多信息。你如何打包你的 Jar 文件,你通常如何连接到你的数据库,你如何配置你的连接?请在您的问题中包含此信息
-
如果您的代码在 IDE 中执行时运行良好,我猜您没有正确构建您的 jar。你在使用像 maven/gradle/... 这样的构建系统吗?
-
SQLException: 没有找到适合...的驱动程序
-
这似乎是一个类路径问题。我们需要知道您是如何创建 exe 文件的。