Java project with Oracle and Mysql at the same time?

Asked

Viewed 110 times

1

In view of an Oracle BD and a Mysql BD, and the impossibility of integration of both bases:

It would be possible, for example, a class of my application to 'look' at the mysql table and update an oracle table?

  • 4

    Yes, it would be possible.

  • 1

    The above comment would already answer your question. Try to specify better what your problem is. Read [Ask].

1 answer

1


By searching you can open two connections with different databases and then insert the read data from one database into 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();
}

Hence, all you have to do is manage Connections, Preparedstatements and Resultsets as usual (including by closing them in a Finally block or using Try-with-Resources).

It is also valid to encapsulate the Connections in Daos, put the connections in pools or separate any operation with the database in 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 the respective Preparedstatements and Resultsets).

  • Very good! my doubt was precisely "Just keep in mind that there may be more than one connection to the active database at the same time". Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.