Search code examples
javams-accessiterator

why we use next() method in java?


Here is the code

Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
Connection conn=DriverManager.getConnection("jdbc:ucanaccess://C:\\Users\\MUHAMMAD SHAHAB\\STUD1.accdb");

Statement st = conn.createStatement();
String sql="select Username,Password from SUN where Username='"+user+"'and Password='"+pass+"'";
ResultSet rs=st.executeQuery(sql);

int count=0;
while(rs.next()) {
    count=count+1;
}
if(count>0) {
     JOptionPane.showMessageDialog(null, "Access granted");
} else if(count<1) {
     JOptionPane.showMessageDialog(null,"User Not Found\nAccess is Denied");
}

I am creating a user verification system in Java and I have connected my program with MS Access. I have inserted some records in the fields of table SUN in MS Access and it is working properly. But I just want to know the working of next() method and count variable in my program.


Solution

  • It moves (or tries to, returning a boolean telling whether it succeeded or not) the resultset cursor forward. The count variable is useless, since you can just write if(rs.next()) to determine which message is shown.