I have this code below and it does not worked. However if I declare Integer variable before while loop it can worked. The output will be printed.
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);
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.