Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JDBC: What is the concrete class that implements the jdbc interfaces

 
prasad regula
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a jdbc code, I am mentioning a sample code below for our reference, we come across
Connections, Statements, PreparedStatement, ResultSet. All these are interfaces. In which part of the jdbc code are we coming across a concrete class. We cant execute a code without a concrete class, by just using interfaces. I am not able to identify what class is the real concrete that is executing this jdbc code.


 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64827
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's the whole point of interfaces -- you don't want or need to know!
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Somewhere you have a jar file that contains the MySQL JDBC driver - the classes that implement those interfaces are in that jar file.
 
Peter Johnson
author
Bartender
Posts: 5852
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not add a line such as:

System.out.println("conn is a " + conn.getClass());

That should tell you the concrete class implementing that interface. You can do the same for the other interfaces.

The "magic" behind the way that JDBC works is that you ask the DriverManager for a connection using a URL. The DriverManager gives you a concrete class that implements the Connection interface. You then turn around and ask the concrete class that implements the Connection interface for a Statement, so that concrete class returns another concrete class that implements the Statement interface. Notice that in this whole scenario that you never "new" anything (that is, you never write "xxx = new XXX").
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic