2

JDBC in it's documentation states that it has 4 implementations to connect to databases. I don't quite get what the 4 implementations mean, I was wondering is JDBC truly database agnostic. That is, will I need "drivers" for each type of database, like for MYSQL (jConnector)?

I am writing an app to support Oracle, MySQL and MSSQL.

Reference: http://docs.oracle.com/javase/tutorial/jdbc/basics/gettingstarted.html

Thanks

Dane Balia
  • 5,271
  • 5
  • 32
  • 57

5 Answers5

5

Unfortunately, you will need drivers for all database types.

If you want to be truly database agnostic you need to use JPA:

http://en.wikipedia.org/wiki/Java_Persistence_API

With one of its implementations. One of the most popular is Hibernate:

http://en.wikipedia.org/wiki/Hibernate_%28Java%29

bjedrzejewski
  • 2,378
  • 2
  • 25
  • 46
  • I get this - but what I don't get, if it needs a driver, then how can it be database agnostic? I don't understand that part. Thanks for your answer - this is just meta-info – Dane Balia Sep 28 '12 at 10:41
  • 1
    JB Nizet explained that in quite a simple way. The API does not care what database you use- but you need to make your database understand the API- thats why you need the driver. Also there are differences between SQL for MySQL, Oracle and MS SQL. JPA helps you with those. You will still need specific dialects for your JPA (but you will not have to change the code). – bjedrzejewski Sep 28 '12 at 10:42
  • Thanks, appreciate the answer. – Dane Balia Sep 28 '12 at 10:46
2

When using JDBC, you use an API that is database-agnostic. The interfaces of this API (Connection, Statement, etc.) are implemented by the JDBC driver of the database that you target. So if you use Oracle, you'll need an oracle driver.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
0

Yes the JDBC API is database agnostic - you just need to provide an appropriate driver. Of course, the SQL you send won't be, unless you use JPA

Vala
  • 5,628
  • 1
  • 29
  • 55
0

JDBC in it's documentation states that it has 4 implementations to connect to databases

No it doesn't. It might state that there are (currently) 4 implementation levels. However the number of implementations depends on (at least) the number of target databases and the number of iterations of the implementation per database, both of which are considerably greater than 4.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • I kind of get you, but it seems silly to add little bits of code to do limited or neglible work (JDBC). In .NET, the connectivity and interaction with the DB rests solely in the "Driver". There is no layer with 'supposed' agnoticism. I don't get it. Just remove JDBC and tell me to use the 'Driver'. – Dane Balia Sep 28 '12 at 11:29
0

I've explained in this answer how difficult it is to be truly vendor agnostic using JDBC alone. There are two layers to this:

  • JDBC is a very good abstraction of the network protocol used to connect to the database. Though you'll need a few vendor-specific quirks here and there. These quirks are fine if you're binding to a single RDBMS vendor, but if you need to support multiple products, it gets very hairy
  • SQL is a very good standard for a query language. Though, again, you'll need quite a few vendor-specific quirks here and there. Same as above, it's OK to support 1 RDBMS and its quirks, but very hard to support multiple.

As others have mentioned, JPA/Hibernate help you abstract over some of the differences, mostly by removing access to more "advanced" SQL features (including derived tables, unions, etc. at least as of version 5). For a more SQL centric abstraction over dialects, jOOQ is a popular option.

Disclaimer: I work for the company behind jOOQ.

Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509