title: JDBC "database table is locked" error
body: <p>I am developing a simple Java library that will provide database access. Currently I am working on accessing SQLite. I have a class named SQlite.java which only implements instance methods. Below are the implementation of a few methods:</p>
  
  <pre><code>public ResultSet retrieve(String query) {
      try {
          if (this.connection != null) {
              this.statement = this.connection.createStatement();
              return this.statement.executeQuery(query);
          }
      } catch (Exception e) {
          System.err.println("[ERROR] " + e.getMessage());
      }
      return null;
  }
  
  public ResultSet listTables() {
      try {
          return this.retrieve("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name");
      } catch (Exception e) {
          System.err.println("[ERROR] " + e.getMessage());
      }
      return null;
  }
  
  public boolean hasTable(String tableName) {
      try {
          ResultSet rs = this.listTables();
          while (rs.next()) {
              if (rs.getString(1).equals(tableName)) {
                  return true;
              }
          }
      } catch (Exception e) {
          System.err.println("[ERROR] " + e.getMessage());
      }
      return false;
  }
  
  public void update(String query) {
      try {
          if (this.connection != null) {
              this.statement = this.connection.createStatement();
              this.statement.executeUpdate(query);
          }
      } catch (Exception e) {
          System.err.println("[ERROR] " + e.getMessage());
      }
  }
  
  public void dropTable(String tableName) {
      try {
          if (this.hasTable(tableName)) {
              this.update("DROP TABLE " + tableName); // TEST!
          } else {
              System.err.println("[ERROR] Table '" + tableName + "' not found!");
          }
      } catch (Exception e) {
          System.err.println("[ERROR] " + e.getMessage());
      }
  }
  </code></pre>
  
  <p>When I test the dropTable() method, I get an exception saying "database table is locked". I am guessing this is due a non-closed SELECT statement that possibly gets called in the hasTable() method. As far as I know the database tables are locked even when retrieval queries are run so that the tables cannot be updated while others are trying to select data. But how to solve this, I could't be sure. Any ideas?</p>