import java.io.*;
import java.sql.*;
import java.text.*;
public class TestSetObject {
Connection conn;
public TestSetObject() {
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();
}
}
public static void main(String[] args)
throws Exception, IOException {
new TestSetObject().process();
}
public void process() throws IOException, SQLException {
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);
pstmt.setObject(1, new Long(999999999));
pstmt.setObject(2, "Krishnamurti");
pstmt.setObject(3, "Jiddu");
pstmt.setNull(4, Types.VARCHAR);
pstmt.setObject(5, Date.valueOf("1895-01-01"));
pstmt.setObject(6, "Unknown");
rows = pstmt.executeUpdate();
System.out.println(rows + " rows inserted");
System.out.println("");
}
catch (SQLException e) {
System.err.println(e.getMessage());
}
finally {
if (pstmt != null)
try { pstmt.close(); } catch (SQLException ignore) { }
}
try {
System.out.println(update);
pstmt = conn.prepareStatement(update);
pstmt.setObject(1, Date.valueOf("1895-05-12"));
pstmt.setObject(2, new Double(999999999));
rows = pstmt.executeUpdate();
System.out.println(rows + " rows updated");
System.out.println("");
}
catch (SQLException e) {
System.err.println(e.getMessage());
}
finally {
if (pstmt != null)
try { pstmt.close(); } catch (SQLException ignore) { }
}
try {
System.out.println(select);
pstmt = conn.prepareStatement(select);
pstmt.setObject(1, "\"K\"");
pstmt.setObject(2, new Integer(999999999));
pstmt.setObject(3, "1");
rslt = 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 { pstmt.close(); } catch (SQLException ignore) { }
}
try {
System.out.println(delete);
pstmt = conn.prepareStatement(delete);
pstmt.setObject(1, new java.math.BigDecimal(999999999));
rows = pstmt.executeUpdate();
System.out.println(rows + " rows deleted");
}
catch (SQLException e) {
System.err.println(e.getMessage());
}
finally {
if (pstmt != null)
try { pstmt.close(); } catch (SQLException ignore) { }
}
}
protected void finalize()
throws Throwable {
if (conn != null) { try { conn.close(); } catch (SQLException ignore) { } }
super.finalize();
}
} |