MySQL 和 Java - 对多个数据库重复操作
MySQL and Java - Repeat operations for multiple databases
我在 Java 中有一个程序可以对数据库进行一些搜索操作,这是代码:
public class DBSearch {
public static void SearchDatabase() throws Exception{
ArrayList<String> names = new ArrayList<String>();
Connection c = null;
PreparedStatement ps = null;
ResultSet rs = null;
String host = "localhost";
String db = "mydatabase";
String user = "root";
String password = "password";
Class.forName("com.mysql.jdbc.Driver");
c = DriverManager.getConnection("jdbc:mysql://"+host+"/" + db +"?user="+user+"&password="+password);
ps = c.prepareStatement("SELECT NAME FROM NAMES");
rs = ps.executeQuery();
names.clear()
while ( rs.next() ) {
names.add(rs.getString("NAME"));
}
for(i = 1; i < names.size(); i++){
//Do some database operations
现在我想要做的是,在这些操作结束时,进程重新开始在另一个数据库中进行相同的操作。我的想法是创建一个名为 dblist
的 ArrayList,其中包含所有数据库名称,然后执行如下操作:
...
for(i=1, i < dblist.size(); i++{
Class.forName("com.mysql.jdbc.Driver");
c = DriverManager.getConnection("jdbc:mysql://"+host+"/" + dblist.get(i) +"?user="+user+"&password="+password);
但是这个过程似乎并没有针对每个数据库循环,它只对数组中的第一个数据库起作用,然后就停止了。
您收到什么消息?根据您发布的内容,没有人可以提供帮助。
这是丑陋、低效的代码。
我会提供一些建议:
- 外部数据库配置。它会更容易重用。您还可以利用连接池。
- 将操作封装在您可以轻松测试的基于接口的 POJO 中。
- 这个问题很容易并行化。一旦您的 POJO 是 运行 并经过测试,您就可以使用 Callable 使用 Executable 一次查询许多数据库。
听起来是个糟糕的设计。为什么要将冗余数据分散在多个数据库中?
与其将变量放在 for 循环的外部,不如尝试将其放在内部。因此,它每次都会创建一个新的连接。
同时检查您从 dblist
获取的数据库信息是否正确。事实上,请确保您甚至获得了数据库信息,而不是 运行 for 循环中的相同连接。
例如。
请注意,我在 for 循环中使用 0,因为数组列表中的第一个索引是 0。
for(i=0, i < dblist.size(); i++
{
dblist.get(i);
//Separate the string you got from the ArrayList into host, user, pass and assign correct variables
Class.forName("com.mysql.jdbc.Driver");
//Variables you got from the list go in the next statement
Connection c = DriverManager.getConnection("jdbc:mysql://"+host+"/" + db +"?user="+user+"&password="+password);
}
并不是说我在连接中没有 get 语句(我假设您的 ArrayList 包含主机、数据库、用户和密码)
或者,您可以将整个连接字符串存储在数组列表中,它只是:
Connection c = DriverManager.getConnection(dblist.get(i));
我在 Java 中有一个程序可以对数据库进行一些搜索操作,这是代码:
public class DBSearch {
public static void SearchDatabase() throws Exception{
ArrayList<String> names = new ArrayList<String>();
Connection c = null;
PreparedStatement ps = null;
ResultSet rs = null;
String host = "localhost";
String db = "mydatabase";
String user = "root";
String password = "password";
Class.forName("com.mysql.jdbc.Driver");
c = DriverManager.getConnection("jdbc:mysql://"+host+"/" + db +"?user="+user+"&password="+password);
ps = c.prepareStatement("SELECT NAME FROM NAMES");
rs = ps.executeQuery();
names.clear()
while ( rs.next() ) {
names.add(rs.getString("NAME"));
}
for(i = 1; i < names.size(); i++){
//Do some database operations
现在我想要做的是,在这些操作结束时,进程重新开始在另一个数据库中进行相同的操作。我的想法是创建一个名为 dblist
的 ArrayList,其中包含所有数据库名称,然后执行如下操作:
...
for(i=1, i < dblist.size(); i++{
Class.forName("com.mysql.jdbc.Driver");
c = DriverManager.getConnection("jdbc:mysql://"+host+"/" + dblist.get(i) +"?user="+user+"&password="+password);
但是这个过程似乎并没有针对每个数据库循环,它只对数组中的第一个数据库起作用,然后就停止了。
您收到什么消息?根据您发布的内容,没有人可以提供帮助。
这是丑陋、低效的代码。
我会提供一些建议:
- 外部数据库配置。它会更容易重用。您还可以利用连接池。
- 将操作封装在您可以轻松测试的基于接口的 POJO 中。
- 这个问题很容易并行化。一旦您的 POJO 是 运行 并经过测试,您就可以使用 Callable 使用 Executable 一次查询许多数据库。
听起来是个糟糕的设计。为什么要将冗余数据分散在多个数据库中?
与其将变量放在 for 循环的外部,不如尝试将其放在内部。因此,它每次都会创建一个新的连接。
同时检查您从 dblist
获取的数据库信息是否正确。事实上,请确保您甚至获得了数据库信息,而不是 运行 for 循环中的相同连接。
例如。 请注意,我在 for 循环中使用 0,因为数组列表中的第一个索引是 0。
for(i=0, i < dblist.size(); i++
{
dblist.get(i);
//Separate the string you got from the ArrayList into host, user, pass and assign correct variables
Class.forName("com.mysql.jdbc.Driver");
//Variables you got from the list go in the next statement
Connection c = DriverManager.getConnection("jdbc:mysql://"+host+"/" + db +"?user="+user+"&password="+password);
}
并不是说我在连接中没有 get 语句(我假设您的 ArrayList 包含主机、数据库、用户和密码)
或者,您可以将整个连接字符串存储在数组列表中,它只是:
Connection c = DriverManager.getConnection(dblist.get(i));