Hi, I'm having problems compiling the following code
private void toInsert(String insert_foo, int insert_bar) {
InitialContext context = null;
Connection conn = null;
PreparedStatement pstmt= null;
try {
context = new InitialContext();
DataSource ds = (DataSource) context.lookup("java:comp/env/jdbc/TestDB");
conn = ds.getConnection();
conn.setAutoCommit(false);
pstmt = conn.prepareStatement("INSERT into testdata values(?, ?)");
pstmt.setString(1, insert_foo);
pstmt.setInt(2, insert_bar);
pstmt.executeUpdate();
conn.commit();
conn.setAutoCommit(true);
pstmt.close();
pstmt = null;
conn.close();
conn = null;
context.close();
context = null;
}
catch (Exception e) {
conn.rollback();
}
finally {
if (pstmt != null) {
try { pstmt.close(); }
catch (SQLException e) {;}
pstmt = null;
}
if (conn != null) {
try { conn.close(); }
catch (SQLException e) {;}
conn = null;
}
if (context != null) {
try { context.close(); }
catch (NamingException e) {;}
context = null;
}
}
}
I got this error when I try to compile the above;
unreported exception java.sql.SQLException; must be caught or declared to be thrown
conn.rollback();
I search and read through a lot of posts here that rollback() could be used in the catch block but why I can't I compile it?
Please help me out, thank you.
puzzled....