简体   繁体   中英

How to switch Databases using Java-JDBC

I have 2 databases, Derby DB and Oracle DB. My logic is to check if the Derby DB is active. If yes, I will send SQL queries to it. If the Derby DB is not active I want to create a connection pool to Oracle and perform the SQL updates there.

Is there any way to do this?

You can used as many database connection as you want. You just need to create the connection provider that will serve the logic. In that connection provider you first create a connection to Derby if fail then try to Oracle.

What you need to assure is that you have proper drivers to both database and proper connection string.

TO assure that you have class

Class.forName("oracle.jdbc.driver.OracleDriver"); 

Class.forName("org.apache.derby.jdbc.EmbeddedDriver"); 

In case then a class is not found above code will throw Exception

About Derby Connection String

About Oracle Connection String

I also advise you to read JDBC Tutorial , and when your project will evolve you might want to use some ORM .

Good luck!

You can do it in the following way. 1)Load the Driver for Derby DB and try to connect to the database, if it throws the Exception then u can handle it in catch block. 2) Load the Driver for Oracle DB and connect to the database and do your transactions.

In this way you can do it...

you could try something like this i suppose:

 getDerbyConnection();
   if(derbyConnectionActive) {
        //execute queries on derbyDb
      }
     else {
       getOracleDBConnection();
          //execute queries on Oracle 
      } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM