import java.io.*;
import java.sql.*;
import java.text.*;
import oracle.jdbc.driver.*;
public class TestDefineParameterType {
Connection conn;
public TestDefineParameterType() {
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 TestDefineParameterType().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 delete =
"delete person_identifier_type " +
"where code = ?";
try {
System.out.println(insert);
pstmt = conn.prepareStatement(insert);
((OraclePreparedStatement)pstmt).defineParameterType(
1, Types.VARCHAR, 30);
((OraclePreparedStatement)pstmt).defineParameterType(
2, Types.VARCHAR, 80);
pstmt.setString( 1, "SID" );
pstmt.setString( 2, "Student Id" );
pstmt.setNull( 3, Types.TIMESTAMP );
rows = pstmt.executeUpdate();
System.out.println(rows + " rows inserted");
System.out.println("");
pstmt.close();
pstmt = null;
}
catch (SQLException e) {
System.err.println(e.getMessage());
}
finally {
if (pstmt != null)
try { pstmt.close(); } catch (SQLException ignore) { }
}
try {
System.out.println(delete);
pstmt = conn.prepareStatement(delete);
((OraclePreparedStatement)pstmt).defineParameterType(
1, Types.VARCHAR, 30);
pstmt.setString( 1, "SID" );
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();
}
}
|