Using Oracle JDBC from the Student Clusters
- Login to one of the undergraduate/graduate servers.
- Set environment variables:
- Compile and run your java program
Note: You'll need at least java version 1.4 to compile properly. Send email to help if you are unsure of which version you are running.
For more info:
JDBC Developer's Guide and Reference 10g Release 1 (10.1)
(you will have to set up an OTN account to access the documents, but it's free!)
Example program -- dualDBtest.java (dowloadable copy:
dualDbTest.java )
import java.sql.*;
import oracle.jdbc.*;
import oracle.jdbc.pool.OracleDataSource;
class dualDbTest
{
public static void main (String args[])
{
// Oracle connection string
String url = "jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=abbott.rutgers.edu)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=student.rutgers.edu)))";
// Build an connection
Connection conn = null;
//Build a statement
Statement stmt = null;
try
{
// create an OracleDataSource instance
OracleDataSource ods = new OracleDataSource();
// make the connection
ods.setURL(url);
ods.setUser(""); // change to your username
ods.setPassword(""); // change to your password
conn = ods.getConnection();
// get meta data
DatabaseMetaData meta = conn.getMetaData();
System.out.println("JDBC driver version is " + meta.getDriverVersion());
// Create a statement
stmt = conn.createStatement();
//Select all columns from the test table
ResultSet rset = stmt.executeQuery("select * from dual");
//Iterate through the result and print the first column
while(rset.next())
System.out.println(rset.getString(1));
}
catch (SQLException sqlEx)
{
// Handle the excpetion
System.err.println( "Message: " + sqlEx.getMessage() );
System.err.println( "Error Code: " + sqlEx.getErrorCode() );
System.err.println( "SQL State: " + sqlEx.getSQLState() );
}
finally
{
try
{ // Closing connection *should* close statement and result set
if (stmt != null) stmt.close();
if (conn != null) conn.close();
}
catch (SQLException sqlEx)
{
System.err.println("SQLException NOT handled");
}
}
}
}