JDBC Oracle Connection
DBAsupport.com Forums - Powered by vBulletin
Results 1 to 2 of 2

Thread: JDBC Oracle Connection

  1. #1
    Join Date
    Feb 2003
    Location
    Tehran/Iran
    Posts
    94

    JDBC Oracle Connection

    Dear all,
    I am runing oracle 9i on Linux Redhat 9. The database is installed on directory:
    /opt/oracle/products/9.2.0 . Do you have example for connecting to oracle 9i from Java 2 application. The problem is the Class.forname statement in the examples I 've seen. What does the Class.forname means and please give an exmple how to connect from java2 to oracle9i minstance.

    // hamhey

  2. #2
    Join Date
    Dec 2001
    Location
    UK
    Posts
    1,684
    Hi.

    Now I'm no Java expert, but I wrote this some time ago as a reminder to myself:

    Code:
    package tsh.misc;
    
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.sql.Connection;
    import java.sql.Statement;
    import java.sql.ResultSet;
    import java.util.Properties;
    
    public class JDBCConnectionTest {
    
      public JDBCConnectionTest() {
        try {
          DriverManager.registerDriver (new oracle.jdbc.OracleDriver());
          normalConnectionThin();
          normalConnectionOCI();
          sysdbaConnectionThin();
          sysdbaConnectionOCI();
        }
        catch (SQLException ex) {
          System.out.println(ex.getLocalizedMessage());
        }
      }
    
      private void normalConnectionThin() throws SQLException {
        Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@w016331:1521:dba1", "scott", "tiger");
        Statement  stmt = conn.createStatement();
        ResultSet  rs   = stmt.executeQuery("SELECT SYSDATE FROM dual");
        while (rs.next()) {
          System.out.println("normalConnectionThin(): SYSDATE=" + rs.getString("SYSDATE"));
        }
        rs.close();
        stmt.close();
        conn.close();
      }
    
      private void normalConnectionOCI() throws SQLException {
        Connection conn = DriverManager.getConnection("jdbc:oracle:oci:@dba1", "scott", "tiger");
        Statement  stmt = conn.createStatement();
        ResultSet  rs   = stmt.executeQuery("SELECT SYSDATE FROM dual");
        while (rs.next()) {
          System.out.println("normalConnectionOCI(): SYSDATE=" + rs.getString("SYSDATE"));
        }
        rs.close();
        stmt.close();
        conn.close();
      }
    
      private void sysdbaConnectionThin() throws SQLException {
    
        Properties info = new Properties();
        info.put ("user", "sys");
        info.put ("password", "password");
        info.put ("internal_logon", "sysdba");
        info.put ("database", "w016331:1521:dba1");
    
        Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@", info);
        Statement  stmt = conn.createStatement();
        ResultSet  rs   = stmt.executeQuery("SELECT SYSDATE FROM dual");
        while (rs.next()) {
          System.out.println("sysdbaConnectionThin(): SYSDATE=" + rs.getString("SYSDATE"));
        }
        rs.close();
        stmt.close();
        conn.close();
      }
    
      private void sysdbaConnectionOCI() throws SQLException {
    
        Properties info = new Properties();
        info.put ("user", "sys");
        info.put ("password", "password");
        info.put ("internal_logon", "sysdba");
        info.put ("database", "dba1");
    
        Connection conn = DriverManager.getConnection("jdbc:oracle:oci:@", info);
        Statement  stmt = conn.createStatement();
        ResultSet  rs   = stmt.executeQuery("SELECT SYSDATE FROM dual");
        while (rs.next()) {
          System.out.println("sysdbaConnectionOCI(): SYSDATE=" + rs.getString("SYSDATE"));
        }
        rs.close();
        stmt.close();
        conn.close();
      }
    
      public static void main (String[] args) {
        new JDBCConnectionTest();
      }
    }
    Hope this answers your question.

    I don't user that Class.forname thing, but it does ring a bell. Perhaps that's another way to achieve the same goal...

    Cheers

    Tim...
    Tim...
    OCP DBA 7.3, 8, 8i, 9i, 10g, 11g
    OCA PL/SQL Developer
    Oracle ACE Director
    My website: www.oracle-base.com
    My blog: www.oracle-base.com/blog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Click Here to Expand Forum to Full Width