Feeds:
文章
评论

Posts Tagged ‘jdbc’

What is JDBC and what is it used for?

JDBC is a set of classes and interfaces written in Java to allow other Java programs to send SQL statements to a relational database management system.Oracle provides three categories of JDBC drivers:

  • JDBC Thin Driver (No local Net8 installation required/ handy for applets)
  • JDBC OCI for writing stand-alone Java applications
  • JDBC KPRB driver (default connection) for Java Stored Procedures and Database JSP’s.

Oracle’s JDBC Thin driver uses Java sockets to connect directly to Oracle. It provides its own TCP/IP version of Oracle’s Net8 (SQL*Net) protocol. Because it is 100% Java, this driver is platform independent and can also run from a Web Browser (applets).

Oracle’s JDBC OCI drivers uses Oracle OCI (Oracle Call Interface) to interact with an Oracle database. You must use a JDBC OCI driver appropriate to your Oracle client installation. The OCI driver works through either SQL*Net or Net8.

  • JDBC OCI7 works with an Oracle7 client.
  • JDBC OCI8 works with an Oracle8 client.

Either of these client versions can access Oracle7 or Oracle8 servers.The JDBC OCI drivers allow you to call the OCI directly from Java, thereby providing a high degree of compatibility with a specific Version of Oracle. Because they use native methods, they are platform specific.

Oracle’s JDBC KBPR driver is mainly used for writing Java stored procedures, triggers and database JSPs. It uses the default/ current database session and thus requires no additional database username, password or URL.All three drivers support the same syntax and API’s. Oracle needs three drivers to support different deployment options. Looking at source code, they will only differ in the way you connect to the database. Remember, you must use a JDBC version that matches the version of your Java Development Kit.

How does one connect with the JDBC Thin Driver?

The JDBC thin driver provides the only way to access Oracle from the Web (applets). It is smaller and faster than the OCI drivers, and doesn’t require a pre-installed version of the JDBC drivers.

import java.sql.*;

class dbAccess {
  public static void main (String args []) throws SQLException
  {
        DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());

        Connection conn = DriverManager.getConnection
             (“jdbc:oracle:thin:@hostname:1526:orcl”, “scott”, “tiger”);
                             // @machineName:port:SID,   userid,  password

        Statement stmt = conn.createStatement();
        ResultSet rset = stmt.executeQuery(“select BANNER from SYS.V_$VERSION”);
        while (rset.next())
              System.out.println (rset.getString(1));   // Print col 1
        stmt.close();
  }
}

How does one connect with the JDBC OCI Driver?

One must have Net8 (SQL*Net) installed and working before attempting to use one of the OCI drivers.

import java.sql.*;

class dbAccess {
  public static void main (String args []) throws SQLException
  {
        try {
              Class.forName ("oracle.jdbc.driver.OracleDriver");
        } catch (ClassNotFoundException e) {
              e.printStackTrace();
        }

        Connection conn = DriverManager.getConnection
             ("jdbc:oracle:oci8:@hostname_orcl", "scott", "tiger");
                     // or oci7 @TNSNames_Entry,    userid,  password

        Statement stmt = conn.createStatement();
        ResultSet rset = stmt.executeQuery("select BANNER from SYS.V_$VERSION");
        while (rset.next())
              System.out.println (rset.getString(1));   // Print col 1
        stmt.close();
  }
}

How does one connect with the JDBC KPRB Driver?

One can obtain a handle to the default or current connection (KPRB driver) by calling the OracleDriver.defaultConenction() method. Please note that you do not need to specify a database URL, username or password as you are already connected to a database session. Remember not to close the default connection. Closing the default connection might throw an exception in future releases of Oracle. 

import java.sql.*;
class dbAccess {
  public static void main (String args []) throws SQLException
  {
        Connection conn = (new oracle.jdbc.driver.OracleDriver()).defaultConnection();

        Statement stmt = conn.createStatement();
        ResultSet rset = stmt.executeQuery("select BANNER from SYS.V_$VERSION");
        while (rset.next())
              System.out.println (rset.getString(1));   // Print col 1
        stmt.close();
  }
}

Ref: http://www.orafaq.com/faqjdbc.htm#JDBC

Advertisements

Read Full Post »