I am developing an application which simulates datacubes and their operation. For this application I need to provide support for various database engines such as PostgreSQL, MySQL, etc.
I was planning on providing an interface where the user can choose the engine and specify its details.
I have been able to connect to PostgreSQL using the JDBC4 driver available. For this connection I had to compile my program code by including the .jar driver in the classpath during compilation.
But the application as a product should run on JRE and there should be no need to recompile the code. Is this possible?
The application should also be customizable to the newer versions of driver and engines available for the database. How can this be done?
You should not need to do this. That’s actually the whole point of JDBC. You should provide the driver class name (and the connection URL, username and/or password) as a string which can be retrieved from some external configuration (properties?) file. E.g.
Even more, since JDBC4 the
Class#forName()is not necessary at all. Just put the JDBC driver in the runtime classpath and let theServiceLoaderAPI do its automatic job of loading the driver.You only need to rewrite your code to only use
java.sqlinterfaces/classes likejava.sql.Connectionand so on instead oforg.postgresqlinterfaces/classes so that you do not need the JDBC driver during compile. In other words, you should not have any single line of code which imports/references the JDBC driver specific interfaces/classes.