gpt4 book ai didi

java - 单线程写入具有不同连接参数的不同数据库

转载 作者:搜寻专家 更新时间:2023-11-01 03:08:22 27 4
gpt4 key购买 nike

我正在做一个项目,在这个项目中,我在不同的数据库中有三个表,它们具有不同的模式。所以这意味着我对这三个表使用 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 插入所有三个表或其中的任何一个。

以下是我的三个用例-

  1. 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.
  2. 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.
  3. 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 以及其他一些常量。

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;
}

任何人都可以提供一些关于我应该如何前进的想法吗?

注意:-

每个表中的列都会有很大的不同。就像在某些表中,列可以是 10,而在其他一些表中,列可以是 20。

最佳答案

创建 databases.properties 文件,内容如下:

# 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 文件中。

关于java - 单线程写入具有不同连接参数的不同数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14786183/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com