I have web service which I need to expose through JDBC due some BI tools software restrictions.
Very limited support required few defined select queries.
What I understand that I need to implement all the classes under the interface java.sql to achieve that. Has anyone done similar things?
Do we have some custom implementations where we need to implement the bare minimum code.
I wrote a "csv-jdbc-driver" just for fun. It is far from production quality code, my goal was only to demonstrate (for myself) how to write a jdbc driver.
Here are my experiences:
As others wrote, it is enough for implementing 4 interfaces from java.sql: Driver, Statement, Connection, ResultSet.
To know, which method should be implemented with "real code", I have to know, how I want to use the csv driver. This is my example code:
try ( Connection conn = DriverManager.getConnection("jdbc:csv:/home/peter/csvdir"); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM test.csv") ){ while (rs.next()) System.out.println(rs.getString(1) + " - " + rs.getString(2)); }
I give a jdbc url to DriverManager.getConnection. In the url I have to specify a directory, where different csv files are. The DriverManager getConnection method calls Driver.connect(url, info). This connect method should check the jdbc url and decides if it can deal with the url or not. If yes, it returns with a Connection object, if not, returns with null.
In the stms.executeQuery I have to give an SQL select, where the "table name" is the name of the csv file. I do not want to implement an sql parser, so this jdbc driver takes into account only the table name.
I also have to use the ResultSet.next() and ResultSet.getString(int) methodes as a bare minimum.
So what I have to implement:
- Driver.connect(String, Properties). Because this method returns with a Connection class
- Connection constructor
- Connection.createStatement method. Because this returns with a Statement class
- Statement constructor
- Statement.executeQuery. Because this returns with a ResultSet class
- ResultSet constructor
- ResultSet.getString(int)
- ResultSet.next()
I also created a service provider file in src/main/resources/MET-INF/services/java.sql.Driver with the contents of org.example.CsvDriver (my Driver implementation class name)
I thought, that these would be enough, but not. My driver loaded, but the DriverManager did not find it.
I also had to call DriverManager.registerDriver(INSTANCE) from my Driver implementation's static initializer, where INSTANCE is an object from my Driver implementation. It seems to me superfluous (because I wrote a java service for avoiding this).
Here are the sources:
I was in a similar situation. It would be nice to see a minimalistic example, but I didn't find such a sample at the time.
My experience is, that, thinking of any API, it's extremely useful to study existing "reference-like" implementations. In the case of JDBC, H2 is a good example to study.
The main interface to implement is java.sql.Driver
. That's how H2 implements it:
https://github.com/h2database/h2database/ . . . /Driver.java
And in this package are the other classes:
https://github.com/h2database/h2database/ . . . /jdbc
H2 is a mature and complex piece of software, but the code is still readable and studyable.
If you are curious, here is a much simpler implementation from the miniconnect project (maintained by me):
For a good walk-through, please see this Java World tutorial.
Basically you need to create 4 classes:
- Driver
- Connection
- Statement
- ResultSet
But as you will see in that tutorial, it's not that simple.
Yes, I've written custom drivers.
You have the right idea, but implement the interfaces
in the java.sql
package. Study the API documentation to learn the purpose for each method so you can implement it in a meaningful way.
Throw UnsupportedOperationException
from any methods your simple driver doesn't support.
public class CsvConnection implements java.sql.Connection
and the driver will practically write itself because of the interface dependencies. –
Histogen java.sql.Connection
interface defines 54 methods. And like you said, you can probably skip most of them by simply returning null
or throwing UnsupportedOperationException
. –
Distich © 2022 - 2024 — McMap. All rights reserved.