TestSetOracleObjectpublic class TestSetOracleObject extends Object
Fields Summary |
---|
Connection | conn |
Constructors Summary |
---|
public TestSetOracleObject()
try {
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
conn = DriverManager.getConnection(
"jdbc:oracle:oci8:@dssw2k01", "scott", "tiger");
// "jdbc:oracle:thin:@dssw2k01:1521:orcl", "scott", "tiger");
}
catch (SQLException e) {
System.err.println(e.getMessage());
e.printStackTrace();
}
|
Methods Summary |
---|
protected void | finalize()
if (conn != null) { try { conn.close(); } catch (SQLException ignore) { } }
super.finalize();
| public static void | main(java.lang.String[] args)
new TestSetOracleObject().process();
| public void | process()
int rows = 0;
ResultSet rslt = null;
PreparedStatement pstmt = null;
String insert =
"insert into person " +
"( person_id, last_name, first_name, " +
"middle_name, birth_date, mothers_maiden_name ) " +
"values " +
"( ?, ?, ?, ?, ?, ? )";
String update =
"update person " +
"set birth_date = ? " +
"where person_id = ?";
String delete =
"delete person " +
"where person_id = ?";
String select =
"select ?, first_name, last_name " +
"from person " +
"where person_id = ? " +
"order by ?";
try {
System.out.println(insert);
pstmt = conn.prepareStatement(insert);
((OraclePreparedStatement)pstmt).setOracleObject(1, new NUMBER(999999999));
((OraclePreparedStatement)pstmt).setOracleObject(2, new CHAR("Krishnamurti", CHAR.DEFAULT_CHARSET));
((OraclePreparedStatement)pstmt).setOracleObject(3, new CHAR("Jiddu", CHAR.DEFAULT_CHARSET));
((OraclePreparedStatement)pstmt).setNull(4, Types.VARCHAR);
((OraclePreparedStatement)pstmt).setOracleObject(5, new DATE(Date.valueOf("1895-01-01")));
((OraclePreparedStatement)pstmt).setOracleObject(6, new CHAR("Unknown", CHAR.DEFAULT_CHARSET));
rows = ((OraclePreparedStatement)pstmt).executeUpdate();
System.out.println(rows + " rows inserted");
System.out.println("");
}
catch (SQLException e) {
System.err.println(e.getMessage());
}
finally {
if (pstmt != null)
try { ((OraclePreparedStatement)pstmt).close(); } catch (SQLException ignore) { }
}
try {
System.out.println(update);
pstmt = conn.prepareStatement(update);
((OraclePreparedStatement)pstmt).setOracleObject(1, new DATE(Date.valueOf("1895-05-12")));
((OraclePreparedStatement)pstmt).setOracleObject(2, new NUMBER(999999999));
rows = ((OraclePreparedStatement)pstmt).executeUpdate();
System.out.println(rows + " rows updated");
System.out.println("");
}
catch (SQLException e) {
System.err.println(e.getMessage());
}
finally {
if (pstmt != null)
try { ((OraclePreparedStatement)pstmt).close(); } catch (SQLException ignore) { }
}
try {
System.out.println(select);
pstmt = conn.prepareStatement(select);
((OraclePreparedStatement)pstmt).setOracleObject(1, new CHAR("\"K\"", CHAR.DEFAULT_CHARSET));
((OraclePreparedStatement)pstmt).setOracleObject(2, new NUMBER(999999999));
((OraclePreparedStatement)pstmt).setOracleObject(3, new CHAR("1", CHAR.DEFAULT_CHARSET));
rslt = ((OraclePreparedStatement)pstmt).executeQuery();
rows = 0;
while (rslt.next()) {
rows++;
System.out.print(rslt.getString(1) + " ");
System.out.print(rslt.getString(2) + " ");
System.out.println(rslt.getString(3));
}
System.out.println(rows + " rows selected");
System.out.println("");
}
catch (SQLException e) {
System.err.println(e.getMessage());
}
finally {
if (rslt != null)
try { rslt.close(); } catch (SQLException ignore) { }
if (pstmt != null)
try { ((OraclePreparedStatement)pstmt).close(); } catch (SQLException ignore) { }
}
try {
System.out.println(delete);
pstmt = conn.prepareStatement(delete);
((OraclePreparedStatement)pstmt).setOracleObject(1, new NUMBER(999999999));
rows = ((OraclePreparedStatement)pstmt).executeUpdate();
System.out.println(rows + " rows deleted");
}
catch (SQLException e) {
System.err.println(e.getMessage());
}
finally {
if (pstmt != null)
try { ((OraclePreparedStatement)pstmt).close(); } catch (SQLException ignore) { }
}
|
|