Monday, May 25, 2009

JDBC Notes (Source)

JDBC Notes (Source)




Types of JDBC drivers:
Type 1: Drivers that implement JDBC APIs to map to another API such as ODBC. Dependent of native library (limited portability). e.g. JDBC-ODBC bridge
Type 2:Partly written in java and partly in native code (limited portability). Use native client library specific to the data source. E.g. JDBC driver that wraps around the oracle client lib.
Type 3:Fully implemented in Java. Interfaces with middleware server using db independent protocol. Middleware server then interfaces with the actual data source.
Type 4:Fully implemented in Java. Implemented network protocol for a data source. Directly communicates with data source.
Basics steps in using JDBC
  1. Establish a connects
    1. Load the driver
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");// Driver document details class name to use
      • Creates a instance of the driver clas
      • Registers the instance with DriverManager
      • Once created the driver instance can make a connection to DBMS.
            B.  Make a connection
                     i.  Using DriverManager
        • Works with Driver interface to manage the set of drivers available to a jdbc client (code that makes jdbc calls)
        • JDBC client provides the URL (of the data source to connect to) and requests a connection with the DriverManager
        • DriverManager finds out a driver that recognizes the URL and uses the driver to connect to the data source
        • URL Pattern
<<protocol>>:<<sub-protocol>>:<<sub_name/db_name>>[property_list]
e.g.jdbc:derby:babudb
where 
<<protocol>> Usially jdbc
<<sub-protocol>> Specified in driver documentation
<<sub_name/db_name>> Usually data base name
<<property_list>> attributes supported by driver specified in driver documentation
        • Making a connection
public static Connection DriverManager.getConnection(String URL, Properties info);
public static Connection DriverManager.getConnection(String URL, String user, String password);
e.g.
Connection conn = DriverManager.getConnection("jdbc:derby:COFFEES");
Connection conn = DriverManager.getConnection("jdbc:derby:COFFEES", "babu", "babupassword"); // Connect to COFFEES database using babu/babupassword as cred.
          • If one of the drivers registered with DriverManager recognizes JDBC URL, then the driver establishes connection with the specified DBMS
          • DriverManager.getConnection() returns a open connection
          • The DriverManager manages all details of establishing the connection
          • Only driver authers need to know about Driver interface
                    ii.  Using DataSource
        • Increases application portability by using a logical name for data sources instead of referencing driver specific information in the JDBC client/application
        • Some examples:
// DataSource as a JNDI resource
InitialContext ic = new InitialContext()
DataSource ds = ic.lookup("java:comp/env/jdbc/myDB");
Connection con = ds.getConnection();

// DataSource implementation provided by vendor
DataSource ds = (DataSource) org.apache.derby.jdbc.ClientDataSource()
ds.setPort(1527);
ds.setHost("localhost");
ds.setUser("APP");
ds.setPassword("APP");
Connection con = ds.getConnection();

No comments: