diff --git a/active-record/src/main/java/com/iluwatar/activerecord/Customer.java b/active-record/src/main/java/com/iluwatar/activerecord/Customer.java index d179c26bd8ba..f87344f2661d 100644 --- a/active-record/src/main/java/com/iluwatar/activerecord/Customer.java +++ b/active-record/src/main/java/com/iluwatar/activerecord/Customer.java @@ -36,6 +36,11 @@ protected String getTableName() { return "customer"; } + @Override + protected String constructInsertQuery() { + return "INSERT INTO " + getTableName() + " VALUES(?, ?, ?, ?)"; + } + @Override protected void setFieldsFromResultSet(ResultSet rs) throws SQLException { this.id = rs.getLong("id"); @@ -46,6 +51,9 @@ protected void setFieldsFromResultSet(ResultSet rs) throws SQLException { @Override protected void setPreparedStatementParams(PreparedStatement pstmt) throws SQLException { - + pstmt.setLong(1, id); + pstmt.setString(2, customerNumber); + pstmt.setString(3, firstName); + pstmt.setString(4, lastName); } } diff --git a/active-record/src/main/java/com/iluwatar/activerecord/Order.java b/active-record/src/main/java/com/iluwatar/activerecord/Order.java index 0e151bb11b93..8d5f048629ae 100644 --- a/active-record/src/main/java/com/iluwatar/activerecord/Order.java +++ b/active-record/src/main/java/com/iluwatar/activerecord/Order.java @@ -25,6 +25,11 @@ protected String getTableName() { return "order"; } + @Override + protected String constructInsertQuery() { + return null; + } + @Override protected void setFieldsFromResultSet(ResultSet rs) throws SQLException { this.id = rs.getLong("id"); diff --git a/active-record/src/main/java/com/iluwatar/activerecord/RecordBase.java b/active-record/src/main/java/com/iluwatar/activerecord/RecordBase.java index 6cc3b4a8662f..bdecf260c226 100644 --- a/active-record/src/main/java/com/iluwatar/activerecord/RecordBase.java +++ b/active-record/src/main/java/com/iluwatar/activerecord/RecordBase.java @@ -5,6 +5,7 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.sql.Statement; import java.util.ArrayList; import java.util.List; import javax.sql.DataSource; @@ -58,6 +59,13 @@ protected Connection getConnection() { */ protected abstract String getTableName(); + /** + * Constructs an INSERT query which is being used for an insertion purposes. + * + * @return an insertion query. + */ + protected abstract String constructInsertQuery(); + /** * Set all the fields into the underlying domain model from the result set. * @@ -85,9 +93,9 @@ public List findAll() { PreparedStatement pstmt = conn.prepareStatement(constructFindAllQuery())) { try (ResultSet rs = pstmt.executeQuery()) { while (rs.next()) { - T record = getDeclaredClassInstance(); - record.setFieldsFromResultSet(rs); - recordList.add(record); + T theRecord = getDeclaredClassInstance(); + theRecord.setFieldsFromResultSet(rs); + recordList.add(theRecord); } return recordList; } @@ -108,9 +116,9 @@ public T findById(Long id) { pstmt.setLong(1, id); try (ResultSet rs = pstmt.executeQuery()) { if (rs.next()) { - T record = getDeclaredClassInstance(); - record.setFieldsFromResultSet(rs); - return record; + T theRecord = getDeclaredClassInstance(); + theRecord.setFieldsFromResultSet(rs); + return theRecord; } return getDeclaredClassInstance(); } @@ -125,9 +133,8 @@ public T findById(Long id) { */ public void save() { try (Connection connection = getConnection(); - // TODO - PreparedStatement pstmt = connection.prepareStatement(null, - PreparedStatement.RETURN_GENERATED_KEYS)) { + PreparedStatement pstmt = connection.prepareStatement(constructInsertQuery(), + Statement.RETURN_GENERATED_KEYS)) { setPreparedStatementParams(pstmt); pstmt.executeUpdate();