Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

conn.rollback() must be caught or declared to be thrown

843859Aug 14 2006 — edited Aug 14 2006
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....
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 11 2006
Added on Aug 14 2006
10 comments
201 views