我想要做的是通过 JDBC 查询 N 个数据库并将结果存储在每个数据库特定的文件中。我正在考虑并行执行查询,并通过线程池进行思考,但是这样可以扩展吗?有更好的方法( Actor 模型)吗?
谢谢。
最佳答案
是的,它是可扩展的。总是有更好的方法,但您需要确保最简单的方法适合您的需求。如果没有,则寻求更好的方法。
替代方法根本不必复杂。
// initialize an executor service
ExecutorService executor = Executors.newCachedThreadPool();
// externalize the access to every database in a method declared in a class
// that implements Runnable
class Oracle implements Runnable {
@Override
public void run() {
// load the driver
// prepare the statement
// get the connection
// execute the statement and get the results
// save the results to a file
}
}
// execute every db access withing the executor
executor.execute(new Oracle());
executor.execute(new SqlServer());
executor.execute(new MySql());
关于java - JDBC查询多个数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9260848/