Java,如何将当前数据库更改为另一个?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13433326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 12:41:25  来源:igfitidea点击:

Java, how to change current database to another?

javamysqljdbc

提问by Krzysiek

I have a Java program connection to a MySQL database, how can I change the current database to a different one on the same connection?

我有一个到 MySQL 数据库的 Java 程序连接,如何在同一连接上将当前数据库更改为不同的数据库?

I connect to MySQL like this:

我像这样连接到 MySQL:

DriverManager.getConnection("jdbc:mysql://"+server+"/",log,pass);

After some operations I want to connect to a different mysql database on the same connection. How can I do that?

经过一些操作后,我想在同一连接上连接到不同的 mysql 数据库。我怎样才能做到这一点?

I tried to use:

我尝试使用:

Statement stat= con.createStatement();
ResultSet r=stat.executeQuery("use mysql"); 

But that does not change the database to be used.

但这不会改变要使用的数据库。

回答by Mark Rotteveel

As described in the MySQL documentationyou need to use Connection.setCatalog()to switch to another database. It also explicitly says that you should notexecute a USE <databasename>to switch.

MySQL 文档中所述,您需要使用它Connection.setCatalog()来切换到另一个数据库。这也明确地说,你应该执行USE <databasename>切换。

The reason for this warning is that JDBC is a generic interface to databases and therefor provides methods for most common tasks, including switching catalogs(or databasesas they are in MySQL). The JDBC specification/javadoc also explicitly says that people should use the API over database specific commands (if both are available). There are several reasons for this: 1) it promotes database-independent code, and 2) the driver might do additional things internally in response to one of the API methods. Using database specific commands might cause the driver to misbehave because its internal state does not match the database state.

出现此警告的原因是 JDBC 是数据库的通用接口,因此为大多数常见任务提供了方法,包括切换catalogs(或databases在 MySQL 中的情况)。JDBC 规范/javadoc 还明确指出,人们应该使用 API 而非数据库特定命令(如果两者都可用)。这有几个原因:1) 它促进了与数据库无关的代码,以及 2) 驱动程序可能会在内部执行其他操作以响应 API 方法之一。使用特定于数据库的命令可能会导致驱动程序行为异常,因为其内部状态与数据库状态不匹配。

A call to setCatalog(String)will not affect existing statements, as specified in the JDBC API documentation:

调用setCatalog(String)不会影响现有语句,如 JDBC API 文档中所述:

Calling setCataloghas no effect on previously created or prepared Statementobjects. It is implementation defined whether a DBMS prepare operation takes place immediately when the Connectionmethod prepareStatementor prepareCallis invoked. For maximum portability, setCatalogshould be called before a Statementis created or prepared.

调用setCatalog对先前创建或准备好的Statement对象没有影响。DBMS 准备操作是否在Connection方法prepareStatement或被prepareCall调用时立即发生是实现定义的。为了最大的可移植性,setCatalog应该在Statement创建或准备a 之前调用。

回答by Subin Sebastian

You can prefix the database name on your table names like this.

您可以像这样在表名上添加数据库名称的前缀。

For example: db1 has table1and db2 has table2

例如:db1 has table1db2 has table2

select * from db1.table1, db2.table2;

This will allow you to do cross database queries

这将允许您进行跨数据库查询