Ryan
Ryan

Reputation: 359

Java, SQL, id increment statement?

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?

Upvotes: 1

Views: 197

Answers (1)

Vincent Ramdhanie
Vincent Ramdhanie

Reputation: 103145

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.

Upvotes: 1

Related Questions