使用不同的连接参数将单线程写入不同的数据库 [英] Single thread writing to different database with different connection parameters
问题描述
我正在一个项目中,在一个具有不同模式的不同数据库中有三个表.因此,这意味着对于这三个要使用JDBC-
I am working on a project in which I have three tables in a different database with different schemas. So that means I have three different connection parameters for those three tables to connect using JDBC-
我们假设-
对于表1-
Username:- A
Password:- B
URL: C
Columns-
ID1 String
Account1 String
对于表2-
Username:- P
Password:- Q
URL:- R
Columns-
ID2 String
Account2 String
对于表3-
Username:- T
Password:- U
URL:- V
Columns-
ID3 String
Account3 String
我应该使用JDBC将所有三个表或其中任何一个插入.
And I am supposed to insert in all the three tables or any one of them using JDBC.
下面是我拥有的三个用例-
Below are the three use cases I have-
- 在命令提示符下,如果假设我仅传递Table1,则假设通过建立连接仅在Table1列中插入 表格1.
- 如果我从命令提示符传递Table1,Table2,那么我想通过以下方式在Table1和Table2列中插入 连接到Table1和Table2.
- 如果我要传递Table1,Table2和Table3,那么我想使用各自的连接输入所有三个表 参数
- From the command prompt if suppose I am passing Table1 only, then I am suppose to insert only in Table1 columns by making connection to Table1.
- And if I am passing Table1, Table2 from the command prompt then I am suppose to insert in both Table1 and Table2 columns by making connection to Table1 and Table2.
- And if I am passing Table1, Table2 and Table3 then I am suppose to enter in all the three tables using there respective connection parameter
我无法理解如何以一种更简洁的方式为上述特定场景编写代码,因此如果我提出了四个表,它也可以在不久的将来进行扩展.我可以有一个常量文件,该文件可以存储需要为这三个表中的任何一个执行的SQL,以及一些其他常量.
I am not able to understand how to write code for the above particular scenario in such a cleaner way so that it can be extended in near future as well if I come up with four tables. I can have a one constant file which can store the SQL that needs to be executed for any of the three tables and some other constant thing as well.
public static void main(String[] args) {
}
class Task implements Runnable {
private Connection dbConnection = null;
private PreparedStatement preparedStatement = null;
public Task() {
}
@Override
public void run() {
dbConnection = getDbConnection();
//prepare the statement and execute it
}
}
private Connection getDBConnection() {
Connection dbConnection = null;
Class.forName(Constants.DRIVER_NAME);
dbConnection = DriverManager.getConnection( , , );
return dbConnection;
}
任何人都可以对此提出一些想法吗?我应该如何继续前进?
Can anyone provide some thoughts on this how should I proceed forward?
注意:-
每个表中的列将有很大的不同.像在某些表中一样,列可以是10,而在另一些表中,列可以是20.
Column in each table will differ a lot. Like in some tables, column can be 10 and in some other table, column can be 20.
推荐答案
使用以下内容创建databases.properties
文件:
Create databases.properties
file with content like this:
# Table 1
table1.url: jdbc:mysql://localhost:3306/garden
table1.user: gardener
table1.password: shavel
table1.table: fruits
table1.column.id: fruitID
table1.column.color: fruitColor
table1.column.weight: fruitWeight
# ... More fruit columns here ...
# Table 2
table2.url: jdbc:mysql://otherhost:3306/forest
table2.user: forester
table2.password: axe
table2.table: trees
table2.column.id: treeID
table2.column.height: treeHeight
# ... More tree columns here ...
# ... More tables here ...
然后执行以下操作:
public static void main (String [] args)
{
Properties databasesProperties = new Properties ();
databasesProperties.load ("databases.properties");
for (String arg: args)
{
String url = databasesProperties.get (arg + ".url");
String user = databasesProperties.get (arg + ".user");
String password= databasesProperties.get (arg + ".password");
String table = databasesProperties.get (arg + ".table");
String columnPrefix = arg + ".column."
Map <String, String> columns = new HashMap <String, String> ();
for (String key: databasesProperties.stringPropertyNames ())
{
if (key.startsWith (columnPrefix))
columns.put (
key.substring (columnPrefix.length ()),
databasesProperties.get (key));
}
doInsert (url, user, password, table, columns);
}
}
以后,您总是可以将更多表添加到databases.properties
文件中.
Later you can always add more tables into your databases.properties
file.
这篇关于使用不同的连接参数将单线程写入不同的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!