Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How JDBC implementation works

Tags:

java

jdbc

In JDBC, I see that Connection is an interface, which defines methods for interacting with Database.

I also know that interface contains only abstract methods, and cannot be instantiated

But In JDBC code, how does the following work,

Connection connection = DriverManager.getConnection("URL String");

Statement statement=connection.createStatement();

As per my doubt createStatement() should be a abstract method, i..e, without any body

But every thing works fine... with this

Can anyone explain?

like image 983
Harsha Avatar asked Oct 27 '14 08:10

Harsha


People also ask

What is JDBC and its steps?

JDBC drivers JDBC-ODBC bridge driver: A thin Java layer that uses an ODBC driver under the hood. Native API driver: Provides an interface from Java to the native database client. Middleware driver: A universal interface (“middleware”) between Java and the RDBMS's vendor-specific protocol.

What is JDBC API explain with its work?

The Java Database Connectivity (JDBC) API provides universal data access from the Java programming language. Using the JDBC API, you can access virtually any data source, from relational databases to spreadsheets and flat files.


2 Answers

DriverManager.getConnection returns an object which implements the Connection interface - behind the scenes there is a real object.

like image 178
codebox Avatar answered Nov 02 '22 02:11

codebox


JDBC is like any driver mechanism - ahead of time the folks and Sun (now Oracle) defined the interface (or contract) that Java would use to interact with a database. It was then the responibility of the database vendors to provide implementations of that interface in order for their particular database to be used with Java.

The take away here is that the JDBC API defines the standard interface through which Java will interact with a database, the obvious benefit being that, providing your code only uses the JDBC API, it will be reasonably uniform regardless of the database you use, meaning you can therefore swap one implementation with another (although it's not always that simple in practice).

With regards to how implementations of the JDBC API are registered with the java.sql.DriverManager, what used to happen was that your code would need to explicitly load the class(es) implementing java.sql.Driver; the act of loading the class caused it to be registered with the java.sql.DriverManager. This mechanism has now be replaced with the service provider but the end result is still the same:

One or more implementations of java.sql.Driver are registered with the java.sql.DriverManager.

If you then look at the source code for java.sql.DriverManager.getConnection(String) you will see that it simply loops through the registered drivers until it finds one that accepts the connection url at which point the java.sql.DriverManager calls the java.sql.Driver.connect(String, Properties) method, returning a specific implementation of java.sql.Connection.

like image 28
Nick Holt Avatar answered Nov 02 '22 03:11

Nick Holt