Wednesday 18 March 2015

New JDBC test harness for testing oracle connections and SQL

This is my modified connect harness for oracle behind a firewall.  Note that there is a change of the connect string, as I was getting a shed load of ORA-12505 errors not matter how I was formatting the original – so I went hard core.

import java.io.*;
import java.util.*;
import java.sql.*;

public class dbconnect
{
public static void main(String[] args) {
try {
Properties props = new Properties();
props.load(new FileInputStream("dbconnect.properties"));
String DRIVER = "oracle.jdbc.driver.OracleDriver";
String URL = "jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=" + props.getProperty("local_system").trim() + ")(PORT=1529))(CONNECT_DATA=(SERVICE_NAME=" + props.getProperty("serviceName").trim() + ")))";
System.out.println("Connect String..." + URL);
//jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=SCAN-LISTENER-NAME)(PORT=1530))(CONNECT_DATA=(SERVICE_NAME=Service-name)))
//Connect to iSeries
Class.forName(DRIVER);
Connection conn = DriverManager.getConnection(URL, props.getProperty("userId").trim(), props.getProperty("password").trim());
System.out.println("Creating statement..." + URL);
Statement stmt = conn.createStatement();
String sql = "SELECT COUNT(1) as RecCount FROM " + props.getProperty("owner").trim() + ".F0101";
ResultSet rs = stmt.executeQuery(sql);
//STEP 5: Extract data from result set
int RecordCount=0;
while(rs.next())
{
//Retrieve by column name
RecordCount = rs.getInt("RecCount");
}
System.out.print("\nF0101 count: " + RecordCount + "\n");

rs.close();
conn.close();
}
catch (Exception e) {
System.out.println(e);
}
System.out.println("Program worked if there are no errors above!"); // Display the string.
}
}

No comments:

Extending JDE to generative AI