2

right now I'm writing a program in Java that will be plugged into an existing application as a plugin. I want to use JDBI in my plugin (as it is very comfortable and I'm used to it) and need to connect to a MySQL database.

Usually that works fine after including the driver, but there is a problem with the existing application. Obviously it already has a mysql-driver, but an obsolete one.

That causes errors and makes it impossible to send a query to the database. (The error is known: The old driver has deprecated methods that cannot be used with the new MySQL versions)

I thought shading my jar would help (I'm using maven) but the error still occurs. But I know the shaded name of my driver, I only need to know how to load it so JDBI will make use of it. Right now I have something like this:

Class.forName("myapp.mysql.jdbc.Driver").newInstance();

DBI dbi = new DBI(String.format("jdbc:mysql://localhost/test", username, password);

What do I have to do to tell jdbi it must use myapp.mysql.jdbc.Driver?

Caleryn
  • 1,084
  • 3
  • 18
  • 23
user3060845
  • 61
  • 1
  • 7
  • It is the protocol (`jdbc:mysql:`) that determines which driver gets a chance. Maybe you can patch "mysql" with "xxxxx" to use your own connection URLs. How does **MariaDB** sound, the MySQL fork? – Joop Eggen Mar 09 '15 at 16:16
  • Could you give an example? I can't really imagine what you mean... – user3060845 Mar 09 '15 at 17:08
  • 1
    Look at [Driver.acceptsURL](http://stackoverflow.com/a/28169947/984823). By patching your driver (for instance as in that answer), you keep both drivers jdbc:mysql and jdbc:xxxxx separate. (Of course a DriverManager.unregister from the MySQL driver would be possible too.) – Joop Eggen Mar 09 '15 at 18:45

1 Answers1

1

You can probably use the DataSource api instead. You don't have to do Class.forName (which I find icky). The MySQL DataSource implementation has a setUrl method that takes jdbc:mysql://.... Then you can just pass the DataSource to the DBI. Here's an example:

MysqlDataSource ds = new MysqlDataSource();
ds.setUrl(dbUrl);
DBI dbi = new DBI(ds);
toadzky
  • 3,806
  • 1
  • 15
  • 26