[英]java.sql.SQLSyntaxErrorException: Unknown database 'userinfo'
I clearly have a database named userinfo with a table named userName.我显然有一个名为 userinfo 的数据库,其中有一个名为 userName 的表。 I am using XAMPP
我正在使用 XAMPP
public class DatabaseHelper {
private static final String dbName = "userinfo";
Connection connection;
Statement stmt = null;
Timestamp date;
public Connection getConnection(){
String dbName = "userinfo";
String userName="root";
String password="12345678";
try {
Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
connection= DriverManager.getConnection("jdbc:mysql://localhost/"+dbName,userName,password);
} catch (Exception e) {
e.printStackTrace();
System.err.println(e.getClass().getName() + ": " + e.getMessage());
System.exit(0);
}
createUsersTable();
return connection;
}
public void createUsersTable() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost/"+dbName);
stmt = connection.createStatement();
String sql = "CREATE TABLE IF NOT EXISTS Users"
+ "(Id INTEGER PRIMARY KEY AUTOINCREMENT,"
+ " Firstname TEXT NOT NULL,"
+ " Lastname TEXT NOT NULL,"
+ " Username TEXT NOT NULL,"
+ " Password TEXT NOT NULL,"
+ " TotalAmount DOUBLE NOT NULL,"
+ " StockAmount DOUBLE NOT NULL,"
+ " Email TEXT NOT NULL" + ");";
stmt.executeUpdate(sql);
stmt.close();
connection.close();
} catch (Exception e) {
System.err.println(e.getClass().getName() + ": " + e.getMessage());
System.exit(0);
}
System.out.println("User table creation successful");
}
The error I am getting : WARNING: Loading FXML document with JavaFX API of version 8.0.171 by JavaFX runtime of version 8.0.131 java.sql.SQLSyntaxErrorException: Unknown database 'userinfo'我收到的错误:警告:使用 8.0.171 版本的 JavaFX API 加载 FXML 文档,JavaFX 运行时版本为 8.0.131 java.sql.SQLSyntaxErrorException:未知数据库'userinfo'
undate: this is the error i am getting now Mar 07, 2020 2:58:56 PM javafx.fxml.FXMLLoader$ValueElement processValue WARNING: Loading FXML document with JavaFX API of version 8.0.171 by JavaFX runtime of version 8.0.131 java.sql.SQLException: Access denied for user ''@'localhost' (using password: NO)日期:这是我现在遇到的错误 2020 年 3 月 7 日下午 2:58:56 javafx.fxml.FXMLLoader$ValueElement processValue 警告:通过 8.0.131 版 JavaFX 运行时使用 8.0.171 版 JavaFX API 加载 FXML 文档.sql.SQLException:拒绝用户“@”本地主机的访问(使用密码:否)
This question is incomplete, so we can't give you a definitive answer.这个问题是不完整的,所以我们不能给你一个明确的答案。 I have commented to point out the extra information that you need to provide .... if you want a complete answer / solution.
我已经评论指出您需要提供的额外信息......如果您想要完整的答案/解决方案。
Here are some initial problems that you need to fix.以下是您需要解决的一些初始问题。
These statements are unnecessary and should be removed:这些语句是不必要的,应该删除:
Class.forName("com.mysql.cj.jdbc.Driver").newInstance(); Class.forName("com.mysql.cj.jdbc.Driver");
It has not been necessary since Java 6 and JDBC 4.0 compliant drivers for MySQL were released.自从 Java 6 和 JDBC 4.0 兼容 MySQL 驱动程序发布以来,就没有必要了。 Roughly 2007!
大约 2007 年! You only need to use
DriverManager.getConnection
.您只需要使用
DriverManager.getConnection
。
Why does this matter?为什么这很重要? Well, the driver class name is database and driver version dependent.
好吧,驱动程序类名称取决于数据库和驱动程序版本。 If you hardwire a driver classname into your code, it is liable to break 1 .
如果您将驱动程序类名硬连接到您的代码中,它很可能会破坏1 。 (Also, an unnecessary
Class.forName
call is inefficient.) (此外,不必要的
Class.forName
调用效率低下。)
Your code is not using the account name and password.您的代码未使用帐户名和密码。 You will be connecting to the database with no account name and no password.
您将在没有帐户名和密码的情况下连接到数据库。 Depending on the way that the MySQL access control has been configured, this might cause it to say that a particular database does not exist.
根据 MySQL 访问控制的配置方式,这可能会导致它说特定的数据库不存在。 (Possibly ... though I doubt it.)
(可能……虽然我怀疑。)
Your code is managing the connection incorrectly:您的代码错误地管理连接:
getConnection
calls DriverManager.getConnection
and assigns the result to connection
.getConnection
调用DriverManager.getConnection
并将结果分配给connection
。createUserTable
... which calls DriverManager.getConnection
again, and assigns it to the same connection
variable.createUserTable
... ,后者再次调用DriverManager.getConnection
,并将其分配给相同的connection
变量。createUserTable
executes some SQL and closes connection
.createUserTable
执行一些 SQL 并关闭connection
。getConnection
will ultimately return a Connection
that has been closed.getConnection
最终将返回一个已关闭的Connection
。 The createUserTable
should not be calling DriverManager.getConnection
, and it should not be closing connection
. createUserTable
不应调用DriverManager.getConnection
,也不应关闭connection
。
Calling System.exit
deep in your code is generally a bad idea.在代码深处调用
System.exit
通常是一个坏主意。 It is better to allow the exception to propagate so that something further up can deal with it.最好允许异常传播,以便进一步处理它。
1 - This is not a theoretical problem. 1 - 这不是理论问题。 The driver class name did change between MySQL Connector/J 5.x and MySQL Connector/J 8.0.
MySQL Connector/J 5.x 和 MySQL Connector/J 8.0 之间的驱动程序类名称确实发生了变化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.