0

I am making a basic java server (client will come later) that will access my MySQL database. It is supposed to simply check if the username name entered exists. If it does state so, if not create the record. After playing around with a bunch of different things trying to get it work but I came across the (rs.next) and when I use it, I get the error cannot find symbol for it. Here is the piece of my code.

 public void run() { final String queryCheck = "SELECT * from account WHERE username = ?"; try { ObjectInputStream FromClient = new ObjectInputStream( client.getInputStream()); ObjectOutputStream ToClient = new ObjectOutputStream( client.getOutputStream()); InetAddress inetAddress = client.getInetAddress(); String host = "jdbc:mysql://localhost/midterm"; String user = "scott"; String pass = "tiger"; String database = "midterm"; Connection conn = DriverManager.getConnection(host,user,pass); PreparedStatement stmt = conn.prepareStatement(queryCheck); System.out.print("Connected to database\n"); System.out.println("Connect to Client " + clientID + " at " + inetAddress.getHostName() + " " + inetAddress.getHostAddress()); // create data input and out streams while(true) { Object objReceived = FromClient.readObject(); Object objReceived2 = FromClient.readObject(); String username = objReceived.toString(); String userpass = objReceived2.toString(); System.out.print("\nInput received: " + "\nUsername: " + username + " Password: " + userpass); try { stmt.setString(1, username); //stmt.setString(2, userpass); ResultSet rs = stmt.executeQuery(); boolean recordAdded = false; if (!rs.next()) { stmt.addBatch(); stmt.executeBatch(); recordAdded = true; } if (recordAdded) { String txt = ("Account Created!!"); ToClient.writeObject(txt); System.out.println("Account has been Created!"); recordAdded = false; }else { String txt = ("Username already exists!"); ToClient.writeObject(txt); System.out.println("\nAccount already exists!"); } } catch (Exception ex) { } //conn.close(); } } catch (Exception e) { System.err.println("Got an exception! "); System.err.println(e.getMessage()); } } 

To be quite frank I am not sure if I am on the right track or not. I've seen other posts here about the same thing but their codes work with next() while mine does not. Any feedback is appreciated.

2
  • 1
    next() is a method - so you want while (!rs.next()), not while (rs.next) Commented Mar 27, 2015 at 11:44
  • Thank you very much this was exactly the kick in the head I needed! Commented Mar 27, 2015 at 12:45

2 Answers 2

1
 while (!rs.next) // I have also tried stmt.next { stmt.addBatch(); stmt.executeBatch(); recordAdded = true; } 

Replace the above code to:

 if(rs.next()) { stmt.addBatch(); stmt.executeBatch(); recordAdded = true; } 
Sign up to request clarification or add additional context in comments.

Comments

1

Here

while (!rs.next) 

You are saying WHILE rs DOES NOT HAVE NEXT ELEMENT...

To iterate over ResultSet, you must delete the ! so you will have:

while (rs.next) // you say, while there are more elements, loop... { stmt.addBatch(); stmt.executeBatch(); recordAdded = true; } 

3 Comments

This does not work but I thank you for your input. I needed to have the rs.next() in there for it to work.
I see, for some reason however when I run the code the if(recordAdded) statement gets skipped over would you perhaps know?
it does because is out of the while(rs.hasNext()), but after iterate over rs results it must enter at least once. Note you are giving recordAdded=true in each iteration, so in order to check it in a right way you must create a method returning boolean value to user creation and put if(recordAdded) condition inside the while(rs.hasNext()) loop

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.