How To Use getConnection

How to establish a Database connection between java application and Database?

If we want to establish a connection between java application and the database we will the following piece of code.

Connection con= DriverManager.getConnection(“jdbc:odbc:nag”,”nag”,”system”,”manager”);
Where getConnectin() is a static method from DriverManager class, which can be used to return connection object.

Basically Connection is an interface, how getConnection() will create an object for Connection interface?

Ans:   
Connection is an interface from java.sql package, for which getConnection(_)  was return an anonymous inner class object of the Connection interface.

Note:-  
Anonymous inner class is a nameless inner class, which can be sued to provide an implementation either for the interfaces or for abstract classes.

Eg:   interface I
         {
                void m1();
          }
         Class Outer
         {
                     I  i = new I(){
                                             public void m1()
                                             {
                                             
                                             }
                                             public void m2()
                                             {
}
                                           }
            }
            Outer o = new Outer();
            o.i.m1();   à  correct
            o.i.m2();   à  wrong
                        

getConnection(_) is a static method from DriverManager class, which will call internally  connect() method, this connect() will establish a virtual socket connection in between the java application and the database.

See also
JTree With Nodes From An XML File

Do you have a Java Problem?
Ask It in The Java Forum

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.