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!

Count Number of Records in Oracle Database Table

843854Oct 6 2004 — edited Oct 7 2004
Please help me to see if I "set" and "return" the number of records in my database table correctly (I am using the Oracle 9i):
   public int getNumberOfRecipientBeans() throws AssertionException, DatabaseException 
   {
      Connection conn = null;
      PreparedStatement stmt = null;

      String query = "SELECT count(*) FROM ContactEntry WHERE ContactTypeID = 6";
      ResultSet rs = null;
      try 
      {
         conn = DBConnection.getDBConnection();
         stmt = conn.prepareStatement( query );
         rs = stmt.executeQuery();
         // do I have to set anything here?
         if ( !rs.next() ) 
         {
            throw new AssertionException("Assertion in servuce.getNumberOfRecipients");
         }
          // Am I returning the counts here?
          return rs.getInt( 1 );
      }
      catch( SQLException sqle ) 
      {
         sqle.printStackTrace();
         throw new DatabaseException( "Error executing SQL in service.getNumberOfRecipients." );
      }
      finally 
      {
         if ( conn != null )
         {
            try
            {
               stmt.close();
               stmt = null;
               conn.close();
            }
            catch( Exception Ex )
            {
               System.out.println( "Problem occurs while closing " + Ex );
            }
            conn = null;
         }       
      }
   }
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 4 2004
Added on Oct 6 2004
13 comments
395 views