1. int ret_val = -1;
  2.  
  3. Statement count_stmt = null;
  4. ResultSet count_rs = null;
  5. Connection conn = getConnection();
  6.  
  7. try {
  8.  
  9. count_stmt = conn.createStatement();
  10. count_rs = count_stmt.executeQuery(sql);
  11. count_rs.next();
  12.  
  13. ret_val = count_rs.getInt(1);
  14.  
  15. } catch (SQLException se) {
  16. System.out.println("SQLException: " + se.getMessage());
  17. System.out.println("SQLState: " + se.getSQLState());
  18. System.out.println("VendorError: " + se.getErrorCode());
  19. } finally {
  20. // it is a good idea to release
  21. // resources in a finally{} block
  22. // in reverse-order of their creation
  23. // if they are no-longer needed
  24. if (count_rs != null) {
  25. try {
  26. count_rs.close();
  27. } catch (SQLException sqlEx) { } // ignore
  28. count_rs = null;
  29. }
  30. if (count_stmt != null) {
  31. try {
  32. count_stmt.close();
  33. } catch (SQLException sqlEx) { } // ignore
  34. count_stmt = null;
  35. }
  36. if (conn != null) {
  37. try {
  38. conn.close();
  39. } catch (SQLException sqlEx) { } // ignore
  40. conn = null;
  41. }
  42. }
  43.  
  44.  
  45. return ret_val;
  46. }