[英]How do I avoid an 'unknown database' error when using a custom database directory?
I'm working on a project and I have put my database folder in project folder.我正在做一个项目,我已将我的数据库文件夹放在项目文件夹中。 How can I make a database connection to any directory rather than just default MySQL dir in Java?如何与任何目录建立数据库连接,而不仅仅是 Java 中的默认 MySQL 目录?
String MySQLURL = "jdbc:mysql://localhost:3306/C:\\Program Files\\SnakeGame";
String UserName = "root";
String Password = "admin";
Connection con = null;
try {
con = DriverManager.getConnection(MySQLURL,UserName,Password);
if (con != null) {
System.out.println("Database connection is successful !!!!");
}
} catch (Exception e) {
e.printStackTrace();
}
When doing this, I get this error:执行此操作时,我收到此错误:
java.sql.SQLSyntaxErrorException: Unknown database 'c:\program files\snakegame' java.sql.SQLSyntaxErrorException: 未知数据库 'c:\program files\snakegame'
Your connection URL is wrong您的连接 URL 错误
String MySQLURL = "jdbc:mysql://localhost:3306/C:\\Program Files\\SnakeGame";
I am not sure why your MySQLURL contains C:\Program Files\SnakeGame我不确定为什么您的 MySQLURL 包含 C:\Program Files\SnakeGame
The connection URL for the mysql database is jdbc:mysql://localhost:3306/[DatabaseName] mysql 数据库的连接 URL 是jdbc:Z81C3B080DAD537DE7E10E032EZ]
Where jdbc is the API, mysql is the database, localhost is the server name on which mysql is running (we may also use the server's IP address here), 3306 is the port number, and [DatabaseName] is the name of the database created on the MySQL server. Where jdbc is the API, mysql is the database, localhost is the server name on which mysql is running (we may also use the server's IP address here), 3306 is the port number, and [DatabaseName] is the name of the database created在 MySQL 服务器上。
Replace the [DatabaseName] name accordingly after creating the database in MySQL server在 MySQL 服务器中创建数据库后,相应地替换 [DatabaseName] 名称
Combining localhost:3306/
with C:\\Program Files\\SnakeGame
makes little sense for any database - either you're trying to connect to a file-based database (in which case the localhost...
part makes no sense) or you're working with a server-based one (in which case the C:\...
part makes no sense.将localhost:3306/
与C:\\Program Files\\SnakeGame
结合起来对任何数据库都没有意义——要么您尝试连接到基于文件的数据库(在这种情况下localhost...
部分没有意义),要么您正在使用基于服务器的服务器(在这种情况下, C:\...
部分没有意义。
Also, this connection string would make little sense for a file-based database either because you didn't specify a specific file, just a path.此外,此连接字符串对于基于文件的数据库几乎没有意义,因为您没有指定特定文件,而只是指定了路径。
Incidentally, MySQL is server-based, not file-based.顺便说一句,MySQL 是基于服务器的,而不是基于文件的。 It's expecting a database name after the localhost:3306/
part, not a path (hence the error).它期望在localhost:3306/
部分之后有一个数据库名称,而不是路径(因此出现错误)。 The physical location of the actual database program is an installation/configuration issue - it has nothing to do with how you actually connect to the database server once it's already running.实际数据库程序的物理位置是一个安装/配置问题 - 一旦它已经运行,它与您如何实际连接到数据库服务器无关。
Think about it this way: when you call an external database, web service, or web site, do you need to know which physical folder it's deployed to?这样想:当您调用外部数据库、web 服务或 web 站点时,您需要知道它部署到哪个物理文件夹? Obviously not.明显不是。 The physical folders involved are completely irrelevant when calling MySQL or another database like this.调用 MySQL 或其他类似的数据库时,所涉及的物理文件夹完全无关紧要。
One of the comments pointed this out, but did you intend to use SQlite or some other file-based database here instead?其中一条评论指出了这一点,但您是否打算在这里使用 SQlite 或其他一些基于文件的数据库?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.