JDBC Knowledge  Hot PDF Print E-mail
Tag it:
Delicious
Furl it!
Digg
NewsVine
Reddit
YahooMyWeb
Technorati
Articles Reviews Java
Written by Junaid   
Sunday, 06 May 2007

{mos_sb_discuss:34}

JDBC has got two kind of drivers  — "thin" driver —or OCI driver — connecting to Oracle —, A  database accessable class, — insert, update and select functionalities. 



Using JDBC to Connect to a Database

With everything available ,Connectivity is our first concern

This is achieved  achieved with the following lines of code:

Class.forName([LOCATION OF DRIVER]);
Connection jdbcConnection =
      DriverManager.getConnection
      ([LOCATION OF DATASOURCE]);


For datasource we can use the following:

Class.forName(sun.jdbc.odbc.JdbcOdbcDriver);
Connection jdbcConnection =
   DriverManager.getConnection
   (jdbc:odbc:Access);


Note that the JdbcOdbcDriver is part of the basic JDK distribution (You can find it in the classes.zip file in the "lib" directory probably). If you are using a driver other than ODBC, you should check to see what syntax it requires for specifying data source name. mSQL, for example, uses something like

Connection jdbcConnection =
       DriverManager.getConnection
       (jdbc:msql://hostname:8888/testing_db");



JDBC facilitates  high-level classes that enables us  to write database applications. networking and database protocols are visible(transparent) to the application developer. classes within JDBC drivers handles them.

JDBC specifications is in agreement with Java community

Oracle provides two main types of drivers,'Thin OCI driver' and 'The ``thin'' driver' .

The OCI driver.

The OCI (type 2) driver consists of java wrappers to the low-level Oracle Call Interface (OCI) libraries used by utilities like SQL*Plus to access the database server. The OCI driver gives  better performance that the thin driver.

The ``thin'' driver.

Also called type 4 driver, the thin driver is a wholly pure Java implementation of Oracle's networking protocol (Net8). Being self-contained, it can be used on any machine with--or without Oracle installed--or even distributed with application classes in an applet.

Connecting to Oracle

First, we use the thin driver in order to connect to Oracle to execute an update statement. Below example can be considered for undertstanding

import java.io.*;
import java.sql.*;

public class OraThin {
  public static void main(String[] args) {
    try {
      Connection con=null;
      Class.forName("oracle.jdbc.driver.OracleDriver");
      con=DriverManager.getConnection(
        "jdbc:oracle:thin:@system_name:1525:database_name",
        "scott",
        "tiger");
      Statement s=con.createStatement();
      s.execute("
        INSERT INTO BOOKS VALUES
        (
        'Far From Madding Crowed',
        'Thomas Hardy',
        4567891231,
        '15-NOV-1995'
        )
      ");
      s.close();
      con.close();
   } catch(Exception e){e.printStackTrace();}
 }
}


BRIEF OF ABOVE CODE:-

    * The package java.sql containing the JDBC classes is imported.
    * The class OracleDriver, in the package oracle.jdbc.driver is dynamically loaded into the Java runtime using Class.forName(...).
    * A connection is requested to the database corresponding to the connection URL with the statement DriverManager.getConnection(...).
    * scott/tiger is an Oracle user/password.
    * We use instances of the Connection and Statement classes to perform a database operation.
    * The resources hold by these are released using their close() method.


User reviews

There are no user reviews for this item.

Add new review




Powered by jReviews

Last Updated ( Sunday, 10 June 2007 )
 
< Prev   Next >