1.12.2 The prepared statement has been finalized, have no idea how to fix

Discussion in 'Spigot Plugin Development' started by longbow122, Jul 27, 2021.

  1. Hello, as the title shows, I've got an SQLException that I have no idea how to fix. Fortunately, it's only a warning so it's not serious and doesn't hinder the functionality of the plugin, but it would still be nice if I knew how to fix it for the future. Here is the stacktrace and the relevant code. There may be some bad programming practises in there, if that's the case, please let me know and I'll do my best to correct them. (Line 305 of ContentScore is Line 69 of the pastebin).

    Thanks in advance for helping!
     
  2. How I see at code, you firstly get amount of lines at ContentScore and after that use for for-loop. I can`t be sure that will be best idea.

    If you need get number of rows and also get through all data of table you can do something like that:
    Code (Java):
    public boolean fixContentScoreTable() {
            m.log("fixContentScoreTable method has been called!");
            int rows = getAllRowsInContentScoreTable();
            Connection c = connect();
            try {
                if(c != null) {
                    String s = "SELECT * FROM ContentScore";
                    Statement st = c.createStatement();
                    ResultSet rs = st.executeQuery(s);
               
                    if (rs.next()) {
                        int rows = 0;
                        while (rs.next()) {
                          String name = rs.getString(1);
                          m.log(name);
                          correctContentScoreTable(name, c);
                          rows++;
                        }
                    } else <no data at table (0 rows)>
                    rs.close();
                    st.close();
                    disconnect();
                    return true;
                }
            } catch (SQLException e) {
                e.printStackTrace();
                return false;
            }
            return false;
        }

    Also, at your pastebin line 71 you return true, som if that line was reached - result set, connection and method disconnect will not be called, so maybe that can be error also
     
  3. Tried reading through the suggested fix on the thread, did some googling to try and find out what it meant, but couldn't find anything. Could you explain what that fix suggests?
     
  4. You should always close your ResultSet and statement in a finally block rather than inside the try block. Or you can use try-with-resources and the JVM will do it for you.