wood burning stoves
The moose likes JDBC and Relational Databases and the fly likes How does JDBC work? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "How does JDBC work?" Watch "How does JDBC work?" New topic
Author

How does JDBC work?

James Leong
Greenhorn

Joined: Dec 18, 2002
Posts: 3
Hi,
I was wondering how JDBC works? Currently, I do 2 things to get a Connection:
a. load the driver, i.e. Class.forName(driverName)
b. get a connection through a static method in DriverManager, i.e. DriverManager.getConnection(url, user, passwd);
In the case, when a few different jdbc drivers are loaded, i.e. we need access through different databases, how does the program arbitrate between which driver class to use? Is it through the url?
If so, is it true that when the driver class is loaded it registers the url that it will listen to the system?
Thanks!
vishwas bansal
Greenhorn

Joined: Dec 19, 2002
Posts: 6
you are very correct ,class.forname actually register and instantiate the driver class.you can have as much driver as you want.consider it to be a hashtable of drivers.when we give url it goes to check every driver class instance if it identifies the url and on success results the connection.
Rahul Pandya
Greenhorn

Joined: Jun 10, 2015
Posts: 9
My question is how JDBC works internally?

for example take JDBC MySQL driver
how does it communicate with the database.. Is there a mysql java driver on which JDBC driver is built?

JDBC is an api
I create an implementation,... how does that implementation send query to database internally


Please provide an example
Dave Tolls
Ranch Hand

Joined: Sep 04, 2013
Posts: 762
    
    2
Rahul Pandya wrote:My question is how JDBC works internally?

for example take JDBC MySQL driver
how does it communicate with the database.. Is there a mysql java driver on which JDBC driver is built?

JDBC is an api
I create an implementation,... how does that implementation send query to database internally


Please provide an example


It's however that particular database expects connections. They each have their own protocols, which is why there's different JDBC drivers.
You'd have to dig out the protocols for the DB in question. Or trawl through the JDBC code for open source drivers.
Rahul Pandya
Greenhorn

Joined: Jun 10, 2015
Posts: 9
That is what exactly I am doing right now

Suppose I run a query sql="select * from person"
behind the JDBC function executeQuery(sql)

how will the query be passed to mysql database


Also is there a mysql java driver(not jdbc ) in between??
Dave Tolls
Ranch Hand

Joined: Sep 04, 2013
Posts: 762
    
    2
What I mean is, you'll have to look at the source code for the MySQL JDBC driver.
Or look up the protocol MySQL expects, but I don't know where that is to be found.

So there's your Java code, which calls the MySQL JDBC code, which makes calls to the DB itself.
Paul Clapham
Sheriff

Joined: Oct 14, 2005
Posts: 19728
    
  10

Rahul Pandya wrote:Also is there a mysql java driver(not jdbc ) in between??


No -- I don't even know what you mean by that, but your code talks to the JDBC driver and the JDBC driver talks to the database. Could you explain why you need to know in detail how the latter part works? The whole point of a JDBC driver is that it encapsulates that communication.
Rahul Pandya
Greenhorn

Joined: Jun 10, 2015
Posts: 9
I wanted to see how it functions from inside
Because I wanted to use JDBC for a database which does not have a JDBC driver available(it is kind of experiment for a project)

Could anyone suggest a better method than JDBC ?

Also what part of JDBC actually runs the query I passed using executeQuery(sql).... I mean you said it is encapsulated but it has to be there in the mysql source code
Dave Tolls
Ranch Hand

Joined: Sep 04, 2013
Posts: 762
    
    2
In which case you need to study how the database you want to write a driver for accepts connections, and the protocol it uses for queries.
Each one is different.

If you've found the source for the MySQL driver then you can open it up in an IDE and trace the call through, wither following the calls by hand or using a debugger to see what happens.

As I say, though, that may not actually help you with the database in question.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How does JDBC work?
 
It's not a secret anymore!