• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How does JDBC work?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic