Author Topic: Connect to an Oracle database with JDBC  (Read 30962 times)

Offline admin

  • Administrator
  • Sr. Member
  • *****
  • Posts: 296
    • View Profile
Connect to an Oracle database with JDBC
« on: May 18, 2009, 04:23:32 PM »
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."); 
  }
}


Offline admin

  • Administrator
  • Sr. Member
  • *****
  • Posts: 296
    • View Profile
Re: Connect to an Oracle database with JDBC
« Reply #1 on: May 20, 2009, 10:41:07 AM »
Note:
you must add the Orcale JDBC library to your project.