Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing a custom jdbc driver in java a very basic one

Tags:

java

jdbc

driver

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.

like image 300
RockSolid Avatar asked Jan 23 '15 16:01

RockSolid


People also ask

How do you write a JDBC program in Java?

A JDBC program comprises the following FIVE steps: STEP 1: Allocate a Connection object, for connecting to the database server. STEP 2: Allocate a Statement object, under the Connection created earlier, for holding a SQL command. STEP 3: Write a SQL query and execute the query, via the Statement and Connection created.

What are the 4 types of JDBC drivers?

Type 1: JDBC-ODBC bridge. Type 2: partial Java driver. Type 3: pure Java driver for database middleware. Type 4: pure Java driver for direct-to-database.

Which JDBC driver fully written in Java?

Type-3 driver Type-3 drivers are fully written in Java, hence they are portable drivers. No client side library is required because of application server that can perform many tasks like auditing, load balancing, logging etc.


2 Answers

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:

  • https://github.com/peterborkuti/csv-jdbc-driver
  • https://github.com/peterborkuti/csv-jdbc-driver-example
like image 96
PeterB Avatar answered Oct 12 '22 23:10

PeterB


I was in a similar situation. It would be nice to see a minimalistic example, but I didn't find one either.

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.

like image 38
Dávid Horváth Avatar answered Oct 13 '22 01:10

Dávid Horváth