import java.io.*;
import java.sql.*;
import java.text.*;
public class TestCursorPositioning {
Connection conn;
public TestCursorPositioning() {
try {
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@dssw2k01:1521:orcl", "scott", "tiger");
}
catch (SQLException e) {
System.err.println(e.getMessage());
e.printStackTrace();
}
}
public static void main(String[] args)
throws Exception, IOException {
new TestCursorPositioning().process();
}
public void process() throws IOException, SQLException {
int rows = 0;
ResultSet rslt = null;
Statement stmt = null;
try {
stmt = conn.createStatement();
rslt = stmt.executeQuery(
"select code, " +
" description, " +
" inactive_date " +
"from PERSON_IDENTIFIER_TYPE");
System.out.println("type = " +
formatType(stmt.getResultSetType()));
System.out.println("concurrency = " +
formatConcurrency(stmt.getResultSetConcurrency()));
System.out.println("before first = " +
new Boolean(rslt.isBeforeFirst()).toString());
while (rslt.next()) {
rows++;
if (rslt.isFirst())
System.out.println("the first row = " + Integer.toString(rows));
// Contrary to JDBC API doc, but in accordance with Oracle's doc
//if (rslt.isLast())
// System.out.println("the last row = " + Integer.toString(rows));
}
System.out.println("after last = " +
new Boolean(rslt.isAfterLast()).toString());
System.out.println(Integer.toString(rows) + " rows selected");
System.out.println(" ");
rslt.close();
rslt = null;
stmt.close();
stmt = null;
}
catch (SQLException e) {
System.err.println(e.getMessage());
}
finally {
if (rslt != null)
try { rslt.close(); } catch (SQLException ignore) { }
if (stmt != null)
try { stmt.close(); } catch (SQLException ignore) { }
}
rows = 0;
try {
stmt = conn.createStatement(
ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
rslt = stmt.executeQuery(
"select code, " +
" description, " +
" inactive_date " +
"from PERSON_IDENTIFIER_TYPE");
System.out.println("type = " +
formatType(stmt.getResultSetType()));
System.out.println("concurrency = " +
formatConcurrency(stmt.getResultSetConcurrency()));
System.out.println("before first = " +
new Boolean(rslt.isBeforeFirst()).toString());
while (rslt.next()) {
rows++;
if (rslt.isFirst())
System.out.println("the first row = " + Integer.toString(rows));
if (rslt.isLast())
System.out.println("the last row = " + Integer.toString(rows));
}
System.out.println("after last = " +
new Boolean(rslt.isAfterLast()).toString());
System.out.println(Integer.toString(rows) + " rows selected");
if (rslt.previous())
System.out.println("the prev row = " +
Integer.toString(rslt.getRow()));
if (rslt.relative(-1))
System.out.println("rel -1 row = " +
Integer.toString(rslt.getRow()));
if (rslt.absolute(2))
System.out.println("abs 2 row = " +
Integer.toString(rslt.getRow()));
rslt.beforeFirst();
System.out.println("bef first row = " +
Integer.toString(rslt.getRow()));
if (rslt.first())
System.out.println("first row = " +
Integer.toString(rslt.getRow()));
if (rslt.last())
System.out.println("last row = " +
Integer.toString(rslt.getRow()));
rslt.afterLast();
System.out.println("aft last row = " +
Integer.toString(rslt.getRow()));
System.out.println(" ");
rslt.close();
rslt = null;
stmt.close();
stmt = null;
}
catch (SQLException e) {
System.err.println(e.getMessage());
}
finally {
if (rslt != null)
try { rslt.close(); } catch (SQLException ignore) { }
if (stmt != null)
try { stmt.close(); } catch (SQLException ignore) { }
}
}
private String formatType(int type) {
switch (type) {
case ResultSet.TYPE_FORWARD_ONLY:
return "TYPE_FORWARD_ONLY";
case ResultSet.TYPE_SCROLL_INSENSITIVE:
return "TYPE_SCROLL_INSENSITIVE";
case ResultSet.TYPE_SCROLL_SENSITIVE:
return "TYPE_SCROLL_SENSITIVE";
default:
return "TYPE_UNKNOWN";
}
}
private String formatConcurrency(int Concurrency) {
switch (Concurrency) {
case ResultSet.CONCUR_READ_ONLY:
return "CONCUR_READ_ONLY";
case ResultSet.CONCUR_UPDATABLE:
return "CONCUR_UPDATABLE";
default:
return "CONCUR_UNKNOWN";
}
}
protected void finalize()
throws Throwable {
if (conn != null)
try { conn.close(); } catch (SQLException ignore) { }
super.finalize();
}
} |