@Override public void addEntry(final Entry entry) throws IOException { final String table_name = this.sql_table_name; final List<String> keys = Collections.newList(); final String stm = "INSERT INTO " + table_name + " " + this.paramString(entry, keys, "(", ")"); final MySQLConnection connection = this.db.obtainConnection(); connection.checkIsOpen(); try { final Connection mysql_connection = connection.getConnection(); final PreparedStatement statement = mysql_connection.prepareStatement(stm); for (int i = 0; i < keys.size(); i++) { final String key = keys.getElementAt(i); final String value = entry.getValue(key); statement.setString(i + 1, value); } statement.execute(); } catch (final SQLException e) { e.printStackTrace(); throw new IOException(e); } finally { this.db.releaseConnection(connection); } }
@Override public void replaceEntries(final List<Entry> batch) throws IOException { if (batch.size() == 0) { return; } final Entry entry0 = batch.getElementAt(0); final String table_name = this.sql_table_name; final List<String> keys = Collections.newList(); final String stm = "REPLACE " + table_name + " " + this.paramString(entry0, keys, "(", ")"); final MySQLConnection connection = this.db.obtainConnection(); connection.checkIsOpen(); try { final Connection mysql_connection = connection.getConnection(); final PreparedStatement statement = mysql_connection.prepareStatement(stm); for (int b = 0; b < batch.size(); b++) { final Entry entry = batch.getElementAt(b); for (int i = 0; i < keys.size(); i++) { final String key = keys.getElementAt(i); final String value = entry.getValue(key); statement.setString(i + 1, value); } statement.addBatch(); } statement.executeBatch(); } catch (final SQLException e) { e.printStackTrace(); throw new IOException(e); } finally { this.db.releaseConnection(connection); } }
@Override public Collection<Entry> findEntries(final String key, final String value) throws IOException { Debug.checkNull("key", key); Debug.checkNull("value", value); final MySQLConnection connection = this.db.obtainConnection(); connection.checkIsOpen(); try { final Connection mysql_connection = connection.getConnection(); final String table_name = this.sql_table_name; final String stm = "SELECT * FROM " + table_name + " WHERE " + key + " = ?"; final PreparedStatement statement = mysql_connection // .prepareStatement(stm); statement.setString(1, value); final ResultSet result = statement.executeQuery(); final List<Entry> res = this.collectResult(result); return res; } catch (final SQLException e) { e.printStackTrace(); throw new IOException(e); } finally { this.db.releaseConnection(connection); } }
@Override public void clear() throws IOException { L.d("clear sql table", this.sql_table_name); final String request = "TRUNCATE " + this.sql_table_name; final MySQLConnection connection = this.db.obtainConnection(); connection.checkIsOpen(); try { final Connection mysql_connection = connection.getConnection(); final PreparedStatement statement = mysql_connection.prepareStatement(request); statement.execute(); L.d(" ", "done"); } catch (final SQLException e) { e.printStackTrace(); throw new IOException(e); } finally { this.db.releaseConnection(connection); } }
@Override public List<Entry> listAll() throws IOException { final MySQLConnection connection = this.db.obtainConnection(); connection.checkIsOpen(); try { final Connection mysql_connection = connection.getConnection(); final Statement statement = mysql_connection.createStatement(); final String request = "SELECT * FROM " + this.sql_table_name; final ResultSet result = statement.executeQuery(request); final List<Entry> resultList = this.collectResult(result); return resultList; } catch (final SQLException e) { e.printStackTrace(); throw new IOException(e); } finally { this.db.releaseConnection(connection); } }