Java – a schema that uses JDBC to connect to different databases
I am writing an application that must be configurable to connect to Oracle, SQL server and MySQL according to the idea of the client
So far, I have been planning to use JDBC - ODBC bridge to connect to the database using only different connection strings
I was told it wasn't very efficient
>Are there patterns or best practices for connecting multiple database systems? Or choose which driver to use? > Should I configure it? But include all three drivers or build three separate customers?
I didn't do anything complicated, just pump (insert) data from the event flow into the database
Solution
I recommend that you configure it to include three drivers You can use this pattern: create a superclass (let's call it Dao) that provides the ability to connect to the database This may be abstract
Create a specific subclass for each type of database you want to connect to So you may eventually get mysqldao, mssqldao and oracledao Each will load the corresponding driver and use its own connection string
Use the method getDao (DB) to create another class (let's call it daofactory), which will create an instance of Dao according to the value of DB
So, for example (in pseudo code):
if(DB.equals("MysqL")){ DAO = new MysqLDAO(); } return DAO;
Therefore, any code that needs to connect to the database will call daofactory and request Dao instances You can store DB values in external files, such as property files, so that you do not have to modify the code to change the type of database
In this way, your code does not need to know which type of database it is connected to. If you decide to support the fourth type of database later, you must add another class and modify daofactory instead of the rest of the code