1

I'm using this method:

public static boolean match(String name, String password) {
                ResultSet result = null;
                try {
                        result = World.database().executeQuery("SELECT password FROM " + PLAYER_TABLE + " WHERE username='" + name + "'");
                        if (result.next()) {
                                String passwordResult = result.getString("password");
                                String encryptedPassword = MD5Encryption.encrypt(password);
                                if (encryptedPassword.equals(passwordResult))
                                        return true;
                        } else {
                                System.out.println("match(String name, String password: false.");
                                StringBuilder query = new StringBuilder();
                                query.append("INSERT INTO `frostblades_users` (id, username, displayName, previousname, lastChanged, password, email, reg_ip, reg_date, last_ip, last_online, picture, rights, playTime, forum, gender, donator, donatorTill, mute, muteTill, banned, online, sof_points, location, hitpoints, inventory_items, inventory_amount, equipment_items, equipment_amount, bank_items, bank_amount, bank_tabs, skills_level, skills_experience, looks, colours, points, friends, ignores, quest_points, music, prayer_points, pouch, run_energy, magic_book, prayer_book, familiar, bob_items, bob_amount, quest_states, quest_progress) VALUES(");
                                query.append("'" + player().definition().index() + "'").append(",");
                                query.append("'" + log.type().index() + "'").append(",");
                                query.append("'" + log.key() + "'").append(",");
                                query.append("'" + log.value() + "'").append(",");
                                query.append("'" + log.date() + "'").append(")");
                                World.database().executeUpdate(query.toString());
                        }

Now I'm using my adventure logs as an example:

StringBuilder query = new StringBuilder();
                                query.append("INSERT INTO `frostblades_adventurers_logs` (user_id, log_type, log_key, log_value, log_date) VALUES(");
                                query.append("'" + player().definition().index() + "'").append(",");
                                query.append("'" + log.type().index() + "'").append(",");
                                query.append("'" + log.key() + "'").append(",");
                                query.append("'" + log.value() + "'").append(",");
                                query.append("'" + log.date() + "'").append(")");
                                World.database().executeUpdate(query.toString());

How would I get the "player().definition().index()" which is the id to increment ++ for every new account?

3
  • Thank you. It wasn't formatting it right, I am on my mobile device. Commented Nov 27, 2013 at 3:40
  • Use PreparedStatement, please. Do not incorporate user-provided data in a statement through string concatenation. Otherwise, you create a nasty security hazard. Google "Bobby Tables" to see the explanation. Commented Nov 27, 2013 at 4:30
  • No longer need this I'll replace the thread with the problem. I fixed my original problem... Commented Nov 27, 2013 at 4:50

1 Answer 1

1

To check if the username exist you can execute a query like this:

  SELECT Count(*) as numusers FROM users WHERE username = 'THEUSERNAME';

If the resulting numusers > 0 then the username exist otherwise it does not. So you need to use your query builder to create and execute the query.

As for the ID, the simplest method would be to let the database increment the id, so you do not insert an id into the database.

Sign up to request clarification or add additional context in comments.

4 Comments

The database automatically does the id increment for you?
If you set the id field to be auto increment then it can. Your insert statement could then simply ignore this field altogether.
So may I ask this question: Moving to paste because it's too long. pastebin.com/mxf4eq2q
Sorry for making a paste but I'm not to sure if you would prefer me to replace the main thread w/ it or not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.