import java.io.*;
import java.sql.*;
import java.text.*;
public class TestPlaceHolder {
Connection conn;
public TestPlaceHolder() {
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 TestPlaceHolder().process();
}
public void process() throws IOException, SQLException {
int rows = 0;
ResultSet rslt = null;
PreparedStatement pstmt = null;
String insert =
"insert into person_identifier_type " +
"( code, description, inactive_date ) " +
"values " +
"( ?, ?, ? )";
String update =
"update person_identifier_type " +
"set description = ? " +
"where code = ?";
String delete =
"delete person_identifier_type " +
"where code = ?";
String select =
"select ?, code, description " +
"from person_identifier_type " +
"where code = ? " +
"order by ?";
try {
System.out.println(insert);
pstmt = conn.prepareStatement(insert);
pstmt.setString( 1, "SID" );
pstmt.setString( 2, "Student Id" );
pstmt.setNull( 3, Types.TIMESTAMP );
rows = pstmt.executeUpdate();
pstmt.close();
pstmt = null;
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.setString( 1, "Student ID" );
pstmt.setString( 2, "SID" );
rows = pstmt.executeUpdate();
pstmt.close();
pstmt = null;
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.setString( 1, "A CONSTANT" );
pstmt.setString( 2, "SID" );
pstmt.setString( 3, "A" );
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));
}
pstmt.close();
pstmt = null;
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.setString( 1, "SID" );
rows = pstmt.executeUpdate();
pstmt.close();
pstmt = null;
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();
}
} |