Excuse: To begin I want to tell you that im not an expert java programmer at all and do not know all the ins and outs of arrayLists and iteration. So far I've come a long way but I somehow do not really understand my problem because I've little experience with Lists in java.
Problem: I've tried to put my sql query results(ResultSet (I Think this is also a array list, so im probably using a double array list which is not needed.)) into a ArrayList, after that I've tried to print each row into my console but I'm having a problem where the first row does not get printed.
Solutions: I'm good as long as the following two things can happen: 1. Put the results in a normal array (So no ArrayList), This makes it easier for me because I know exactly how to handle arrays. 2. You guys can tell me why the first row does not get printed
My Database: I have two colums, the first one is the primary key (ID) and the second one is a command (String(Varchar) I want to execute once I get it.
My code:
public List<String> getCommands(){
String sql = "SELECT * FROM query";
try {
PreparedStatement stmt = CONNECTION.prepareStatement(sql);
ResultSet results = stmt.executeQuery();
if (results.next()) {
List<String> row = new ArrayList<String>();
while(results.next()) {
row.add(results.getString(2));
}
return row;
} else {
return null;
}
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
List<String> results = SQL.getCommands();
if(results != null){
getLogger().info("Found at least 1 command");
for (Iterator<String> i = results.iterator(); i.hasNext();) {
String row = i.next();
getLogger().info("command: " + row);
}
}else{
getLogger().info("Can't find commands");
}
My output: It prints everything exept the first row in the database.
results.next()twice, so it skips the first row. You should remove theifclause.arraylist iteration.