MrCptObvious
MrCptObvious

Reputation: 45

Add record into MySQL database through java server/client

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.

Upvotes: 0

Views: 66

Answers (2)

Jordi Castilla
Jordi Castilla

Reputation: 26981

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;
}

Upvotes: 1

Azad Hussain
Azad Hussain

Reputation: 161

            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;
            }

Upvotes: 1

Related Questions