• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

import java.sql

 
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

which object ?


here we cant create instance of interface connection .thats why we are creating only references.please guide me
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
santhosh.R gowda
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79987
399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No longer a beginner's question. Moving.
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let the code speak for itself !!





output:
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Water! People swim in water! Even tiny ads swim in water:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic