Writing a custom jdbc driver in java a very basic one
Asked Answered
F

4

19

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.

Frontiersman answered 23/1, 2015 at 16:49 Comment(1)
Good topic, but it is unclear what you're asking.Twopiece
C
16

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:

Caralie answered 10/4, 2021 at 17:1 Comment(0)
C
10

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):

https://github.com/miniconnect/miniconnect/ . . . /jdbc

Choochoo answered 14/6, 2020 at 15:7 Comment(0)
D
8

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.

Distich answered 12/7, 2018 at 8:10 Comment(1)
This article is from 2002, that's dated. There is now service provider declaration now.Castile
H
5

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.

Histogen answered 23/1, 2015 at 17:3 Comment(3)
Could you share some of you code to write the driver? <BR>It would be interesting to see something like a SQL support over a well defined simple csv file.Frontiersman
I could share what I've done, but honestly it's not going to be useful to you for a CSV driver. It's really easy though, believe me. Start by creating a public class CsvConnection implements java.sql.Connection and the driver will practically write itself because of the interface dependencies.Histogen
It would help if you could tell us which methods are the bare minimum to implement. After all the 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.