Code example for Statement

Methods: close, executeQuery, executeUpdate

0
      try 
      { 
         connection = DriverManager.getConnection( 
            DATABASE_URL, USERNAME, PASSWORD );
         statement = connection.createStatement();
         ResultSet resultSet = statement.executeQuery(
            "SELECT \"Number\" FROM \"Seats\"" + 
            "WHERE (\"Taken\" = 0) AND (\"Location\" = '" + seatType + 
            "') AND (\"Class\" = '" + classType + "')" );
          
         // if requested seat is available, reserve it 
         if ( resultSet.next() )
         { 
            int seat = resultSet.getInt( 1 );
            statement.executeUpdate( "UPDATE \"Seats\" " +
               "SET \"Taken\" = 1 WHERE \"Number\" = " + seat );
            return true; 
         } // end if 
          
         return false; 
      } // end try 
      catch ( SQLException e )
      { 
         e.printStackTrace();
         return false; 
      } // end catch 
      catch ( Exception e )
      { 
         e.printStackTrace();  
         return false; 
      } // end catch 
      finally 
      { 
         try 
         { 
            statement.close();
            connection.close();
         } // end try 
         catch ( Exception e )
         { 
            e.printStackTrace();