"No operations allowed after statement closed." WTF part 2

Discussion in 'Plugin Development' started by bfgbfggf, Jan 13, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    bfgbfggf

  2. Offline

    LucasEmanuel

    Make a new statement everytime you want to work against the database. :)

    EDIT:
    There is a timeout for the statement-object. It will close if it isn't used within that timeframe if I remember correctly.
     
    bfgbfggf likes this.
  3. Offline

    bfgbfggf

    SQLite don't like many statements if I remember? (or that only for connections?)

    And then...
    I must do something like that: (Db class)
    Code:
            private static void update(String sql) {
                    try {
                            st = con.createStatement();
                            st.executeUpdate(sql);
                            st.close();
                    } catch (SQLException e) {
                            e.printStackTrace();
                    }
            }
     
            private static ResultSet query(String sql) throws SQLException {
                    st = con.createStatement();
                    ResultSet r = st.executeQuery(sql);
                    st.close();
                    return r;
            }
    Or

    Code:
            private static void update(String sql) {
                    try {
                            Statement s = con.createStatement();
                            s.executeUpdate(sql);
                            s.close();
                    } catch (SQLException e) {
                            e.printStackTrace();
                    }
            }
     
            private static ResultSet query(String sql) throws SQLException {
                    Statement s = con.createStatement();
                    ResultSet r = s.executeQuery(sql);
                    s.close();
                    return r;
            }
     
  4. Offline

    LucasEmanuel

    I would use the second version, but you should also remember to manually close the resultsets when you are done with them as well.
     
    bfgbfggf likes this.
  5. Offline

    bfgbfggf

    Ok, Thanks :p I try this, (but not today).

    PS: About your edit... that possible to just check the statement and open it if is closed? Or it's just better to create other?
     
  6. Offline

    LucasEmanuel

Thread Status:
Not open for further replies.

Share This Page