resultset iterating only one time on while loop
My problem was that in the getClasses()
method i also modify the rs
variable and so in the while loop the variable is not the same so it returns false.
the solution is to make the ResultSet rs
variable local in every method.
like this:
public class DBConnect {
private Connection conn;
public DBConnect(){
try{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/unal_grades", "root", "");
}catch(Exception ex){
System.out.println("Error: " + ex);
}
}
public ArrayList<Semester> getSemesters(){
ResultSet rs;
try{
Statement st = conn.createStatement();
ArrayList<Semester> semesters = new ArrayList<Semester>();
String query = "Select * from semester";
rs = st.executeQuery(query);
System.out.println("semesters");
while(rs.next()){
int id = rs.getInt("id");
String name = rs.getString("name");
float average = rs.getFloat("average");
boolean active = rs.getBoolean("active");
System.out.println(name+" / "+average+" / "+active);
Semester semester = new Semester(id, name, average, active);
semester.setClasses(getClasses(semester));
semesters.add(semester);
}
System.out.println();
return semesters;
}catch(Exception ex){
System.out.println("Error: " + ex);
}
return null;
}
}
Santiago Alvarez
I am a student at the National University of Colombia in Medellin, Colombia studying Computer Science Front end developer, android, ios, react, angular, react-native.
Updated on June 04, 2022Comments
-
Santiago Alvarez almost 2 years
So im accessing a mySql database in my java code using a
Statement
to execute my query and save the returningResultSet
in aResultSet
object then using that sameResultSet
object, i iterate over the rows from the result and simply print out the data for every row.Here is the code:
public class DBConnect { private Connection conn; private Statement st; private ResultSet rs; public DBConnect(){ try{ Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/unal_grades", "root", ""); st = conn.createStatement(); }catch(Exception ex){ System.out.println("Error: " + ex); } } public ArrayList<Semester> getSemesters(){ try{ ArrayList<Semester> semesters = new ArrayList<Semester>(); String query = "Select * from semester"; rs = st.executeQuery(query); System.out.println("semesters"); while(rs.next()){ int id = rs.getInt("id"); String name = rs.getString("name"); float average = rs.getFloat("average"); boolean active = rs.getBoolean("active"); System.out.println(name+" / "+average+" / "+active); Semester semester = new Semester(id, name, average, active); semester.setClasses(getClasses(semester)); semesters.add(semester); } System.out.println(); return semesters; }catch(Exception ex){ System.out.println("Error: " + ex); } return null; }
My problem is that in the while loop, the
rs.next()
statement is only returning true on the first iteration. In other words, it only prints the first row of my table in the database when actually having many rows.When debugging, if i inspect
rs.next()
before actually running over it, when i actually run over it on the first iteration, it goes into the loop and prints out the second row of my table.Thank you very much for your help.
-
griFlo almost 9 years@SantiagoAlvarez he added
st = con.createStatement();
(What I think you have in your original code. otherwise your Statement variable wouldnt be initialized) -
smali almost 9 yearsPositive...thats why I got that doubt the sample you have written previously has no logical bug or systactical bug...