First, do not use for loop because you only get one column from each row ... every time you press rs.next() , it goes to the next row before you finish getting all the column values from one strings. Instead, use while (rs.next()) to check for the existence of a string.
Secondly, as @shaun mentioned, JDBC columns start at 1, not zero.
Thirdly, to put data in a list, you will need a POJO or bean to store all the information from each row.
So, at the end, the code looks something like this: -
Connection con = ...; PreparedStatement ps = con.prepareStatement("SELECT name, pass FROM loginTable WHERE name = ?"); ps.setString(1, "mike"); ResultSet rs = ps.executeQuery(); List<User> users = new ArrayList<User>(); while(rs.next()) { String name = rs.getString(1); String pass = rs.getString(2); users.add(new User(name, pass)); } ...
source share