Java project with Oracle and MySQL at the same time?

1

Given an Oracle Database and a MySQL DB, and the impossibility of integrating both databases:

Would it be possible, for example, to class my application 'look' at the mysql table and update an oracle table?

    
asked by anonymous 04.05.2015 / 19:15

1 answer

1

As I researched you can open two connections to different databases and then insert the data read from one database in the other more or less like this:

String urlMySql = "jdbc:mysql://localhost:3306/seu_database_mysql";
String urlDb2 = "jdbc:db2://localhost:50000/seu_database_db2";

Connection connMySql = DriverManager.getConnection(urlMySql);
Connection connDb2 = DriverManager.getConnection(urlDb2);

PreparedStatement selectDb2 = connDb2.prepareStatement("SELECT * FROM TABELA");
ResultSet rsDb2 = selectDb2.executeQuery();

while (rsDb2.next()) {
 PreparedStatement insertMySql = connMySql.prepareStatement("INSERT INTO OUTRA_TABELA VALUES...");
insertMySql.setXXX(rsDb2.getXXX(...));
insertMySql.executeUpdate();
}
So, all you have to do is manage the Connections, PreparedStatements, and ResultSets as usual (including closing them in a finally block or using try-with-resources).

It is also valid to encapsulate the Connections in DAOs, put the connections into pools or separate any operation with the database into several classes and / or various methods. Just keep in mind that there may be more than one connection to the active database at the same time (along with their PreparedStatements and ResultSets).

    
04.05.2015 / 19:25