Question: How do I test a JDBC connection to
Oracle?
Answer:
Here is some sample Java code to test an Oracle JDBC connection:
import java.sql.*;
public class TestDBOracle {
public static void main(String[] args)
throws
ClassNotFoundException, SQLException
{
Class.forName("oracle.jdbc.driver.OracleDriver");
//
jdbc:oracle:thin:@//host:port/service
String url =
"jdbc:oracle:thin:@//myhost:1521/mydb";
// Real
Application Cluster database
String url =
"jdbc:oracle:thin:@(DESCRIPTION=(LOAD_BALANCE=yes)(ADDRESS=(PROTOCOL=TCP)(HOST=NODE1-VIP)(PORT=1521))(ADDRESS=(PROTOCOL=TCP)(HOST=NODE2-VIP)(PORT=1521))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=ORARAC)))";
Connection conn =
DriverManager.getConnection(url,"myschema","mypassw");
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 ("Success!");
}
}
Also see this related page on
installing and
using JDBC for Oracle.