Home > java > jdbc hello world

jdbc hello world

September 25th, 2009

I am in a java mood today, let’s check how to print hello world with jdbc :)

import java.sql.*;
public class HelloWorld {
  public  static void main(String[] args) throws SQLException {
    DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
    ResultSet res = DriverManager.
      getConnection("jdbc:oracle:thin:@srv1:1521:DB01", "scott", "tiger").
      prepareCall("select 'Hello World' txt from dual").
      executeQuery();
    res.next();
    System.out.println(res.getString("TXT"));
  }
}

let’s compile
javac -classpath $ORACLE_HOME/jdbc/lib/classes12.jar HelloWorld.java

and run

$ java -classpath $ORACLE_HOME/jdbc/lib/classes12.jar:. HelloWorld
Hello World

that’s all folks!

Bookmark and Share

  1. christophe K
    September 25th, 2009 at 15:10 | #1

    Just one problem: find the correct version of classes12.jar

    Performance team is fond of this solution :)

  2. September 25th, 2009 at 21:38 | #2

    well, you can use old or new classes zip/jar, but the newest one have new capabilities, like tnsnames :)

    What’s exactly your problem ?

  3. September 26th, 2009 at 10:52 | #3

    A java is a good program, and a nice way to develop with oracle database.
    JDBC Thin doesn’t need tnsnames… that’s nice. But if need to use RAC(TAF) JDBC OCI may a better way …

    By the way, Connection Pooling + Caching are good thing to learn and …

    Enjoy!

  4. September 26th, 2009 at 12:39 | #4
  5. Al
    September 30th, 2009 at 11:57 | #5

    classes12.jar ??? This library is designed to work with Java 1.2 and 1.3 ! Since Java 1.4 was released in 2002, you should use ojdbc*.jar ;) (the “*” depending on your server version)

    I just checked http://www.oracle.com/technology/software/tech/java/sqlj_jdbc/index.html , 10gR2 seems to be the last version with a classes12.jar. Since 11gR1 it’s all ojdbc*.jar

  6. September 30th, 2009 at 21:33 | #6

    thanks for the the comment :) yes, I noticed there were no more classes102.zip in my classpath …

    Very helpful comment!

  1. October 1st, 2009 at 06:32 | #1