MySQL select error

Discussion in 'Spigot Plugin Development' started by DotRar, Jun 9, 2016.

  1. I have a table in my database which contains whether users want to receive messages from their friends only or not. I can change the value they give but for some reason I cannot get it. It always just returns the default thing (false). Here is my code:

    Code (Text):
    public boolean getMessagesFromFriendsOnly(final String uuid) {
            Callable<Boolean> task = new Callable<Boolean>() {
                public Boolean call() {
                    try {
                        PreparedStatement ps = conn.prepareStatement("SELECT * FROM msgsFromFriendsOnly WHERE UUID = '"+uuid+"');");
                        ResultSet resultSet = ps.executeQuery();

                        if(resultSet.first()) {
                            return resultSet.getString("BOOLVALUE").equalsIgnoreCase("true");

                        return false;
                    } catch(SQLException e) {
                        return false;
            try {
                return Executors.newSingleThreadExecutor().submit(task).get();
            } catch(InterruptedException e) {
                return false;
            } catch(ExecutionException e) {
                return false;
    Also, I use unique keys so there is only 1 value at a time for a single person.
    Secondly, I think where I messed up is with the resultSet.first() as I have never used JDBC before and that bit didn't really seem right.

    Thanks :D
  2. @Nex9
    • Don't close your PreparedStatement before reading the data from your ResultSet.
    • Don't swallow exceptions (especially during development, when you'd want the stack trace).
    • Don't forget to close your resources using try-with-resources.
    • Use PreparedStatements properly!
      • Don't concat parameters in, use the designated ? placeholders
      • Use PreparedStatement#setX(int index, X object) to fill the placeholders (index starts at 1!)
    • Create an ExecutorService outside of the method body, otherwise creating one is useless. (because you don't reuse it)