Connect to an Oracle database with JDBC

From CodeCodex

Implementations[edit]

Java[edit]

Oracle Corporation has released a free 100% JAVA driver. It is available at their Web site. All you need is to include the required jar in the classpath.

import java.sql.*;
 
public class TestDBOracle {
 
  public static void main(String[] args) 
      throws ClassNotFoundException, SQLException
  {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    //
    // or
    // DriverManager.registerDriver 
    //        (new oracle.jdbc.driver.OracleDriver());
 
        String url = "jdbc:oracle:thin:@//server.local:1521/prod";
    //               jdbc:oracle:thin:@//host:port/service
    // or 
    // String url = "jdbc:oracle:thin:@server.local:1521:prodsid";
    //               jdbc:oracle:thin:@host:port:SID
           
    Connection conn = 
         DriverManager.getConnection(url,"scott", "tiger");
 
    conn.setAutoCommit(false);
    Statement stmt = conn.createStatement();
    ResultSet rset = 
         stmt.executeQuery("select BANNER from SYS.V_$VERSION");
    while (rset.next()) {
         System.out.println (rset.getString(1));  
    }
    stmt.close();
    System.out.println ("Ok.");  
  }
}

 

See also this HowTo to connect using the Oracle Connection Pool.