import java.io.*;
import java.sql.*;
import java.text.*;
public class TestResultSetUpdates {
Connection conn;
public TestResultSetUpdates() {
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 TestResultSetUpdates().process();
}
public void process() throws IOException, SQLException {
BufferedReader in =
new BufferedReader(new InputStreamReader(System.in));
int rows = 0;
ResultSet rslt = null;
Statement stmt = null;
String sql =
"select code, " +
" description, " +
" inactive_date " +
"from PERSON_IDENTIFIER_TYPE";
// can't use for update clause because of driver defect";
try {
conn.setAutoCommit(false);
stmt = conn.createStatement(
ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
rslt = stmt.executeQuery(sql);
System.out.println("type = " +
formatType(stmt.getResultSetType()));
System.out.println("concurrency = " +
formatConcurrency(stmt.getResultSetConcurrency()));
while (rslt.next()) {
rows++;
if (rslt.getString(1).equals("SDL")) {
rslt.deleteRow();
System.out.print("Deleted, press Enter to continue...");
System.out.println("");
in.readLine();
}
}
rslt.moveToInsertRow();
rslt.updateString(1, "SDL");
rslt.updateString(2, "State Drivers License");
rslt.updateNull(3);
rslt.insertRow();
rslt.moveToCurrentRow();
System.out.print("Inserted, press Enter to continue...");
System.out.println("");
in.readLine();
stmt.close();
stmt = null;
rslt.close();
rslt = 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) { }
}
try {
stmt = conn.createStatement(
ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
rslt = stmt.executeQuery(sql);
while (rslt.next()) {
rows++;
if (rslt.getString(1).equals("SDL")) {
rslt.updateString(2, "State Driver's License");
rslt.updateRow();
System.out.print("Updated, press Enter to continue...");
System.out.println("");
in.readLine();
}
}
conn.commit();
System.out.print("Committed, press Enter to continue...");
System.out.println("");
in.readLine();
stmt.close();
stmt = null;
rslt.close();
rslt = 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();
}
} |