title: Can't declare variable inside While loop
body: <p>I have this code below and it does not worked. However if I declare <code>Integer</code> variable before while loop it can worked. The output will be printed. </p>
  
  <pre><code>        query = "SELECT COUNT(*) AS tot_by_code FROM `hr` WHERE `hr`.`code` = ?";              
          st = conn.prepareStatement(query);
          st.setString(1, code);
          rs = st.executeQuery();
  
          //Integer tot_by_code = null;
          while (rs.next()) {
              Integer tot_by_code = rs.getInt("tot_by_code");
          }
  
  
          System.out.println("Total patient by code : " + tot_by_code);
  </code></pre>
  
  <p>I check other thread and supposedly you can declare variable before loop or inside loop so why I can't ran my code with variable declaration inside while loop? Thanks in advance.</p>