title: can i use same JDBC connection, statement and resultset to execute two queries in JDBC
body: <p>I am authenticating user like</p>
  
  <pre><code>public static boolean login(DataManager dataManager, String userName, String password) {    
      boolean authenticated = false;      
      Connection connection = dataManager.getConnection();        
      if (connection != null) {           
           try {                           
               Statement s = connection.createStatement();
               String sql = "query";                 
               try {                   
                   ResultSet rs = s.executeQuery(sql);                                     
                   try {                      
                       while (rs.next()) {                             
                          String group_code = rs.getString(1);
                          String orgaunit = rs.getString(2);
  
                          authenticated = true;                            
                       } //end of while()                      
                   } finally {                     
                       rs.close();                     
                   }                   
               } finally {
                   s.close();                  
               }
  
           } catch(SQLException e) {               
               //System.out.println("Could not login from dataabse:" + e.getMessage());                
           } finally {             
               dataManager.putConnection(connection);                          
           }                  
      } //end of if (connection != null)      
      return authenticated;       
  } //end of login()
  </code></pre>
  
  <p>I am closing connection in <code>dataManager.putConnection(connection)</code>. I want to ask once user get login then i have to update the status of user and maintain log history. Can i use something like this</p>
  
  <pre><code>try {
      Statement s = connection.createStatement();
      String sql = "query";                 
      try {                    
          ResultSet rs = s.executeQuery(sql);                                  
          try {
              while (rs.next()) {                          
                  String group_code = rs.getString(1);                                                
                  authenticated = true;                            
              } //end of while()  
  
              if (autherntcated == true) {
                  sql = "query2(update status)"; 
                  rs = s.executeQuery(sql);
  
                  while (rs.next()) {
                      //dos tuff
                  }
  
                  sql = "anotherQuery";
                  rs = s.executeQuery(sql);
                  while (rs.next()) {
                      //do stuff
                  }
  
              }
  
          } finally {                      
              rs.close();                      
          }                    
      } finally {
          s.close();                   
      }                
  } catch(SQLException e) {
      //System.out.println("Could not login from dataabse:" + e.getMessage());
  } finally {              
      dataManager.putConnection(connection);                           
  }
  </code></pre>
  
  <p>Means using same connection, same statement and same resultset execute other queries or is it wrong approach ?</p>
  
  <p>Thank you.</p>
  
  <p><strong>Edit
  --------------------------------------------------------------</strong></p>
  
  <pre><code>if (connection != null) {
      try {
          String sql = "query";
          PreparedStatement prepStatement = connection.prepareStatement(sql);
          try {
              ResultSet rs = prepStatement.executeQuery(sql);                 
              try {
                  while (rs.next()) {
                      String group_code = rs.getString(1);
                      authenticated = true;
                  } //end of while()
              } finally {                      
                  rs.close();                      
              }
          } finally {
              prepStatement.close();                   
          }
  
          /// Addition   
          if (authenticated == true) {
              updateUser(connection, userName);
          }
      } catch(SQLException e) {
          //System.out.println("Could not login from dataabse:" + e.getMessage());
      } finally {
          dataManager.putConnection(connection);
      }
  } //end of if (connection != null)
  </code></pre>
  
  <p>UPdate method:</p>
  
  <pre><code>private static void updateUser(Connection connection, String userName) {
  
      try {
          String sql = "UPDATE users SET status_code = 'A' WHERE login_id = '" + userName + "'";    
          PreparedStatement prepStatement = connection.prepareStatement(sql);
          try {
              int numberOfRowsUpdated = prepStatement.executeUpdate(sql);                 
          } finally {
              prepStatement.close();                   
          }
  
          maintainHistory(connection);
  
      } catch(SQLException e) {
      //System.out.println("Could not login from dataabse:" + e.getMessage());
      }
  
  } //end of updateUser()
  </code></pre>
  
  <p>maintainHistory:</p>
  
  <pre><code>private static void maintainHistory(Connection connection) {
  
      try {
          String sql = "INSERT INTO auditlog_user_logins(user_code,logintime,prstid) VALUES ();";
          PreparedStatement prepStatement = connection.prepareStatement(sql);          
          try {            
            int numberOfRowsUpdated = prepStatement.executeUpdate(sql);                   
  
         } finally {
  
          prepStatement.close();                   
  
         }
  
       } catch(SQLException e) {
  
           //System.out.println("Could not login from dataabse:" + e.getMessage());
  
       }
  
  } //end of maintainHistory()
  </code></pre>