-5

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.

Jonas
  • 121,568
  • 97
  • 310
  • 388
Swager
  • 87
  • 6
  • count is variable that count numbers of rows, rs.next() go to the next row in your resultset if exists. Next time use google for this, it's pretty easy question and google will find you a lot of tutorials with that. – Filip Kováč Aug 22 '16 at 09:04
  • Did you try documentation before asking question https://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html#next() ? – Nickname0222022022 Aug 22 '16 at 09:05
  • `next()` gets the next row, or returns `false` is there isn't any. `count` counts the number of rows. The code is as simple as it looks. – Peter Lawrey Aug 22 '16 at 09:08

1 Answers1

1

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.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
  • it means that you are trying to say that it is basically moving cursor forward in ms access table? – Swager Aug 22 '16 at 09:07
  • No. In the resultset. – Kayaman Aug 22 '16 at 09:08
  • It better to assign to a Boolean variable rather than using it straightly in boolean statements. boolean rowExists = rs.next(); rowExists can be used in multiple places wherever you need but rs.next() can't be used since it's moving forward always. – Gopidoss Aug 22 '16 at 09:22
  • @Gopidoss There's no "it's better to assign" rule. In this particular case, the `count` *and* the `while` are useless. It's not "better" in any way. – Kayaman Aug 22 '16 at 09:29
  • Agreed @Kayaman, My point is if the existence of row needs to solve multiple use cases then I would suggest to hold it in a boolean container (rowExists) rather direclty use it in 'Equality Operators' since it's moves the cursor forward one row from its current position. boolean rowExists = lotteryLogRs.next(); while (proceed) { if (rowExists) { //do something } ........ break; ........ } ....... ....... // Add remaining if (rowExists) { //do something } – Gopidoss Aug 22 '16 at 12:24
  • @Gopidoss Of course then you'll have to. But in this case you don't need to, and it only makes the code more unclear. – Kayaman Aug 22 '16 at 12:26