Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes JDBC and the fly likes import java.sql Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "import java.sql" Watch "import java.sql" New topic
Author

import java.sql

santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
hi friends,
As we know java.sql package consists of interfaces and these interfaces are implemented by the corresponding data base vendors in thier own class for example connection is an interface in sql package and these interface are implemented by oracle in their class by name oracleConnection.but when we are connecting to a perticular data base we will put data base vendors jar files in class files and we are tyoping in our code as import java.sql."; why it is so like that even thoudh the implementation is in jar files we have not mentioned any where in our code.. but still we are using sql package only please tell me with example


Creativity is nothing but Breaking Rules
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874


Yes you are right. Its all interfaces that Java API defines and database vendor write implementation to it.

why it is so like that even thoudh the implementation is in jar files we have not mentioned any where in our code..


Not true in all case. We use Class.forName to load the Database specific driver class to load. Though recent JDBC 4.0 release has sophisticated itself to load class files from CLASSPATH.

but still we are using sql package only please tell me with example


By using JAVA sql API , we conform to standard , that each method returns this type of Class . So any database vendor when writing driver classes, has to conform to these standards( this is the beauty of interfaces). This way we code to interfaces , without worrying about implementation ( different Database drivers).
Imagine if there is no such thing like this , what would you do if you change your database vendor , would you rewrite entire code importing new database vendor classes ?


I guess this would better fit in JDBC forum
santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
ok balu but when we call createStatement() method from our class.is it so JVM will call the data base vendor class or while implementing the interfaces they had writtenn the code like that... tell me please
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874


ok balu but when we call createStatement() method from our class.is it so JVM will call the data base vendor class or while implementing the interfaces they had writtenn the code like that... tell me please


how do you call createStatement() ? using which object ? how did you get that object ?

If you answer the above questions , you get the answer to your question
santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
how do you call createStatement() ? using which object ?

first we will create reference of type connection and then we will call CreateStatement() method which will return statement
but that implementation is provided in data base vendors jar files na... who is going to take responsibility of calling createStatement() method in jar files is it JVM or who else?
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874



using which object ? how did you get that object ?


You dint answer this point.

first we will create reference of type connection and then we will call CreateStatement() method which will return statement


thats is more general. be specific of what happens exactly.
santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
which object ?

here we cant create instance of interface connection .thats why we are creating only references.please guide me
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874



here we cant create instance of interface connection .thats why we are creating only references.please guide me


Ok . You are assigning the reference , the object of Database vendor Connection object ( through polymorphism). So you get the Connection object which is actually the vendor specific . And when you call createStatement() , you know it calls vendor specific class ( polymorphically) to return Statement object ( again a object , whose Class which implements Statement interface).

HTH.
Atul Jawale
Greenhorn

Joined: Jul 24, 2006
Posts: 2
Hi Here is the basic steps to connect with database & execute the query.

1> First you load the drivers i.e. Class.forName("class name here from the .jar file");

When this statement gets executed it will actually create a object of the Driver class (implementation from the jar file)
In the Driver class implementation if you see the default constructor implementation it actually calls the DriverManager.register() method which actually registers this driver class with the DriverManager.

2> Second step is to create the Connection. So when you call DriverManager.getConnection("connection string"); This call will try to connect with the database using all the registerd drivers with it as in first step we have registered the correct driver it will connect using that Driver and will return the Connection object (which is the implementation inside the jar of the Connection interface from java.sql package).

So now you have a connection instance & all the method calls on this instance are implemented in the jar file. And you are using the reference variables to refer the actual instances.

Statement stmt = con.createStatement() ;

here stmt is the reference variable and also the con both are pointing to their implementations from the jar files.

Hope now clears the thing


SCJP (1.4),SCWCD
santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
the object of Database vendor Connection object ( through polymorphism).

as you told connection object of data base vendor is called ... there in my code i had not written any import statement of vendor classes just i had put them in classfiles thats all.in my code i had written import java.sql.*; means all interfaces in sql will come into my class ok. when i type this statement class.forName(); Driver of the corresponding DB will be loaded into my class .and when i type statement Driver manager.getConnection() ; getConnection is a static method in Drivermanager which is in sql package. which gets connectd to a DB and returns connection . in next statement i will Connection .createStatement() this connection object is data base vendor object right. using this i will call create statement which will returns statement object ok. is it so like that
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874



1> First you load the drivers i.e. Class.forName("class name here from the .jar file");

When this statement gets executed it will actually create a object of the Driver class (implementation from the jar file)
In the Driver class implementation if you see the default constructor implementation it actually calls the DriverManager.register() method which actually registers this driver class with the DriverManager.


This is partly false. Class.forName does not create object. It just loads the Class to the JVM. In this case , static intializers are called and thats where the intialization takes place.
Atul Jawale
Greenhorn

Joined: Jul 24, 2006
Posts: 2
Hi Balu , Can you please tell me what is the differenece between loading the class in JVM and creating the object ?
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874

santhosh.R gowda wrote:
the object of Database vendor Connection object ( through polymorphism).

as you told connection object of data base vendor is called ... there in my code i had not written any import statement of vendor classes just i had put them in classfiles thats all.in my code i had written import java.sql.*; means all interfaces in sql will come into my class ok. when i type this statement class.forName(); Driver of the corresponding DB will be loaded into my class .and when i type statement Driver manager.getConnection() ; getConnection is a static method in Drivermanager which is in sql package. which gets connectd to a DB and returns connection . in next statement i will Connection .createStatement() this connection object is data base vendor object right. using this i will call create statement which will returns statement object ok. is it so like that


yes you got it.

One thing to note is the magic of DriverManager.getConnection(" conn string") , it just loads the suitable driver available from CLASSPATH.
santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
yes balu class.forName() will not create object it just loads classfile to JVM .inside driver class static intializer block is there where driverObject is creted and DriverManager.registerDriver(driver) is called where it registers drivers.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38513
    
  23
No longer a beginner's question. Moving.
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

Let the code speak for itself !!





output:


My Website: [Salvin.in] Cool your mind:[Salvin.in/painting] My Sally:[Salvin.in/sally]
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874

Atul Jawale wrote:Hi Balu , Can you please tell me what is the differenece between loading the class in JVM and creating the object ?


Hi Atul , Welcome to Javaranch !!
Please post new question related to Java in JIB forum , its no longer in JIB and moved to JDBC.
santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
Hi Balu , Can you please tell me what is the differenece between loading the class in JVM and creating the object

hi atul loading the class in jVM means forName() method in class is used to load the class in any package. if class not found then classNot found exception will be thrown .if success class is loaded to jvm .Jvm will exceute the class. here JVM will not create object of that class.if you want to create object then you have to code it in static block which gets executed after loading class.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: import java.sql