Sample JDBC program which connects to Oracle

Sample program that demonstrates using JDBC to connect to Oracle database.

Source code
$ cat TestConnection.java
import java.sql.*;

public class TestConnection {
public static void main (String args []) throws SQLException, InterruptedException
{
DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());
java.util.Properties props = new java.util.Properties();

// user/password@host:port:SID
Connection conn = DriverManager.getConnection
(“jdbc:oracle:thin:” + args[0] + “/” + args[1] + “@” + args[2] + “:” + args[3] + “:” + args[4], props);

Statement stmt = conn.createStatement();
ResultSet rset = stmt.executeQuery(“select to_char(sysdate, ‘DD-MON-YYYY HH24:MI:SS’) from dual”);
while (rset.next()) {
String s = rset.getString(1);
System.out.println(“Date: ” + s);
}
stmt.close();
}
}

To compile and run
$ set CLASSPATH=.:/home/oracle/jar/ojdbc14.jar
$ javac TestConnection.java
$ java TestConnection username passowrd hostname port sid
Date: 14-OCT-2009 19:27:51

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s