Make your own free website on Tripod.com

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * This class tests the environment to check if OCI driver is configured and
 * connection to database is possible thru JDBC. Uses OCI driver to connect.
 * 
 * Refer to http://myjdbc.tripod.com/basic/jdbcoci.html for more information on
 * how to handle any error during connection.
 */
public class JDBCOCITest {

  // Database connect string
  // format: jdbc:oracle:oci:@tnsentry where tnsentry is the tnsentryname in tnsnames.ora
  // found in ORACLE_HOME/network/admin folder
  private String jdbcURL = "jdbc:oracle:oci:@ias904";
  
  private Connection conn = null;

  public JDBCOCITest( )  throws ClassNotFoundException  {
    Class.forName("oracle.jdbc.driver.OracleDriver");
  }  

  public static void main(String[] args)  throws ClassNotFoundException, SQLException {
    JDBCOCITest check = new JDBCOCITest();
    check.dbconnect();
    check.doSomething();
  }

  public void dbconnect() throws SQLException   {

    System.out.println("Connecting with URL="+jdbcURL+" as scott/tiger");

    try {

      conn = DriverManager.getConnection( jdbcURL, "scott" , "tiger");

      System.out.println("Connected to Database");

    } catch (SQLException sqlEx)     {
      System.out.println(" Error connecting to database : " + sqlEx.toString());
      System.out.println(" Refer to http://myjdbc.tripod.com/basic/jdbcoci.html for the appropriate solution to this error ");
    }

  }

  public void doSomething() throws SQLException   {

    Statement stmt = null;
    ResultSet rset = null;
    try {
      stmt = conn.createStatement();
      System.out.println("Created Statement object");

      rset = stmt.executeQuery(" SELECT 'PASSED' FROM Dual ");
      System.out.println("Retrieved ResultSet object");

      if(rset.next())
        System.out.println("Connection :"+rset.getString(1));

    } catch (SQLException sqlEx)     {

    } finally  {
      try {
        System.out.println("Closing Statment & ResultSet Objects");
        if (rset != null) rset.close();
        if (stmt != null) stmt.close();
        if (conn != null) {
          System.out.println("Disconnecting...");
          conn.close();
          System.out.println("Disconnected from Database");
        }
      } catch (Exception e)  { }
    }

  }

}