FileInputStream fin = null;
int rows = 0;
long person_id = 0;
PreparedStatement pstmt = null;
Statement stmt = null;
ResultSet rslt = null;
try {
conn.setAutoCommit(false);
// get Tim's person_id
stmt = conn.createStatement();
rslt = stmt.executeQuery(
"select person_id " +
"from person " +
"where last_name = 'O''Reilly' " +
"and first_name = 'Tim'");
while (rslt.next()) {
rows++;
person_id = rslt.getLong(1);
}
if (rows > 1) {
System.err.println("Too many rows!");
System.exit(1);
}
else if (rows == 0) {
System.err.println("Not found!");
System.exit(1);
}
rslt.close();
rslt = null;
rows = stmt.executeUpdate(
"delete person_biography " +
"where person_id = " + Long.toString(person_id));
System.out.println(rows + " rows deleted");
stmt.close();
stmt = null;
pstmt = conn.prepareStatement(
"insert into person_biography " +
"( person_id, biography ) " +
"values " +
"( ?, ? )");
pstmt.setLong(1, person_id);
File fileName = new File("tim.txt");
long fileLength = fileName.length();
fin = new FileInputStream(fileName);
System.out.println(fileLength + " bytes read");
byte[] buffer = new byte[(int)fileLength];
int read = fin.read(buffer);
// will throw SQLException with error:
// Data size bigger than max size for this type: ####
// if size is greater than 2000 bytes for Oracle7
// or 4000 bytes for Oracle8i
pstmt.setString(2, new String(buffer));
rows = pstmt.executeUpdate();
System.out.println(rows + " rows inserted");
fin.close();
fin = null;
conn.commit();
pstmt.close();
pstmt = null;
}
catch (IOException e) {
System.out.println("IO Error: " + e.getMessage());
System.exit(1);
}
catch (SQLException e) {
System.out.println("SQL Error: " + e.getMessage());
System.exit(1);
}
finally {
// if (rslt != null) try { rslt.close(); } catch (SQLException ignore) { }
if (stmt != null) try { stmt.close(); } catch (SQLException ignore) { }
if (fin != null) try { fin.close(); } catch (IOException ignore) { }
}