public List<Record> getAllRows() { Connection connection; Statement statement; String sql; ResultSet resultSet; List<Record> list = new ArrayList<Record>(); AccountEntry accountEntry; try { connection = SQLiteManager.getInstance().getConnection(); statement = connection.createStatement(); sql = "SELECT ae.id, account_id, a.name AS account_name, booked_document_id, text, amount, created_time " + " FROM account_entries ae, accounts a WHERE a.id = ae.account_id ORDER BY ae.id"; resultSet = statement.executeQuery(sql); while (resultSet.next()) { accountEntry = createFromResultSet(resultSet); list.add(accountEntry); } statement.close(); resultSet.close(); } catch (SQLException e) { System.err.println("SQLException: " + e.getMessage()); e.printStackTrace(); } return list; }
/* (non-Javadoc) * @see model.dao.IDataModel#insert() */ @Override public Long insert() throws SQLException { try { Class.forName(Settings.DATABASE_JDBC_CLASSNAME); } catch (ClassNotFoundException e) { System.err.println("ClassNotFoundException: " + e.getMessage()); e.printStackTrace(); } Connection conn; PreparedStatement statement; try { conn = SQLiteManager.getInstance().getConnection(); String sql = "INSERT INTO account_entries (account_id, booked_document_id, text, amount, created_time)" + " VALUES (?, ?, ?, ?, ?)"; statement = conn.prepareStatement(sql); statement.setLong(1, getAccount().getId()); statement.setLong(2, bookedDocument.getId()); statement.setString(3, text); statement.setDouble(4, amount); statement.setString(5, util.Date.toSqlTimeString(Calendar.getInstance().getTime())); statement.execute(); sql = "SELECT last_insert_rowid()"; ResultSet resultSet = conn.createStatement().executeQuery(sql); if (resultSet.next()) id = resultSet.getLong(1); else throw new SQLException("unable to fetch insert-id"); resultSet.close(); } catch (SQLException e) { System.err.println("SQLException: " + e.getMessage()); e.printStackTrace(); } return id; }
/* (non-Javadoc) * @see model.dao.IDataModel#delete() */ @Override public void delete() { Connection conn; PreparedStatement statement; try { conn = SQLiteManager.getInstance().getConnection(); String sql = "DELETE FROM account_entries WHERE id = ?"; statement = conn.prepareStatement(sql); statement.setLong(1, id); statement.execute(); } catch (SQLException e) { System.err.println("SQLException: " + e.getMessage()); e.printStackTrace(); } }