MySql getting uuid

Discussion in 'Spigot Plugin Development' started by DaanSander, May 12, 2015.

  1. Hello I am trying to get the uuid of a player but when i try that i only get tghe first part of the uuid 3de51a4e
    is there a special way to do this or am I doing something wrong
    sorry for bad english

    code:
    Code (Text):
    public static int get(String uuid, SQLType type) {
            try {
               
                ResultSet resultSet = sql.executeQuery("SELECT * FROM `" + table + "` WHERE uuid="" + uuid + "";");
                if(!resultSet.next()) {
                    sql.close();
                    resultSet.close();
                    return -1;
                }
                int chests = resultSet.getInt(type.getName());
                sql.close();
                resultSet.close();
                return chests;
            } catch (SQLException e) {
                e.printStackTrace();
                return -1;
            }
        }
     
  2. Where is your code that actually passes the UUID? The SQL execution doesn't help much if your problem is with getting the UUID
     
  3. ResultSet resultSet = sql.executeQuery("SELECT * FROM `" + table + "` WHERE uuid="" + uuid + "";");
    I think there should be also ' or \"
     
  4. You should really be using prepared statements anyways, it will prevent formatting errors like that if they are the cause.
     
  5. Ahhhh!! OCD,
    Remove the 'sql.close();' in the try catch.

    And add at the end of the try catch add

    Code (Text):
        finally {
            if (sql != null){
                try {
                    sql.close();
                } catch (Exception ignored){}
            }
        }
    This will execute no matter what happens.
    There is a chance that 'resultSet.next()' throws an error. Then it'll never be closed =O (Memory leak)
     
  6. Java 7 OP
    Code (Java):
    try (ResultSet rs = sql.executeQuery(...))
    {
        // Do stuff with the ResultSet
    }
    catch (SQLException ex)
    {
        // Handle exception
    }
     
    • Agree Agree x 1
  7. You win. :( I don't use that, I just like the satisfaction of closing it myself. Knowing everything is in safe hands.