Skip to content

Commit 2fbbb4d

Browse files
implement a simple save method (#79)
1 parent 645eb92 commit 2fbbb4d

File tree

3 files changed

+30
-10
lines changed

3 files changed

+30
-10
lines changed

active-record/src/main/java/com/iluwatar/activerecord/Customer.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ protected String getTableName() {
3636
return "customer";
3737
}
3838

39+
@Override
40+
protected String constructInsertQuery() {
41+
return "INSERT INTO " + getTableName() + " VALUES(?, ?, ?, ?)";
42+
}
43+
3944
@Override
4045
protected void setFieldsFromResultSet(ResultSet rs) throws SQLException {
4146
this.id = rs.getLong("id");
@@ -46,6 +51,9 @@ protected void setFieldsFromResultSet(ResultSet rs) throws SQLException {
4651

4752
@Override
4853
protected void setPreparedStatementParams(PreparedStatement pstmt) throws SQLException {
49-
54+
pstmt.setLong(1, id);
55+
pstmt.setString(2, customerNumber);
56+
pstmt.setString(3, firstName);
57+
pstmt.setString(4, lastName);
5058
}
5159
}

active-record/src/main/java/com/iluwatar/activerecord/Order.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ protected String getTableName() {
2525
return "order";
2626
}
2727

28+
@Override
29+
protected String constructInsertQuery() {
30+
return null;
31+
}
32+
2833
@Override
2934
protected void setFieldsFromResultSet(ResultSet rs) throws SQLException {
3035
this.id = rs.getLong("id");

active-record/src/main/java/com/iluwatar/activerecord/RecordBase.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.sql.PreparedStatement;
66
import java.sql.ResultSet;
77
import java.sql.SQLException;
8+
import java.sql.Statement;
89
import java.util.ArrayList;
910
import java.util.List;
1011
import javax.sql.DataSource;
@@ -58,6 +59,13 @@ protected Connection getConnection() {
5859
*/
5960
protected abstract String getTableName();
6061

62+
/**
63+
* Constructs an INSERT query which is being used for an insertion purposes.
64+
*
65+
* @return an insertion query.
66+
*/
67+
protected abstract String constructInsertQuery();
68+
6169
/**
6270
* Set all the fields into the underlying domain model from the result set.
6371
*
@@ -85,9 +93,9 @@ public List<T> findAll() {
8593
PreparedStatement pstmt = conn.prepareStatement(constructFindAllQuery())) {
8694
try (ResultSet rs = pstmt.executeQuery()) {
8795
while (rs.next()) {
88-
T record = getDeclaredClassInstance();
89-
record.setFieldsFromResultSet(rs);
90-
recordList.add(record);
96+
T theRecord = getDeclaredClassInstance();
97+
theRecord.setFieldsFromResultSet(rs);
98+
recordList.add(theRecord);
9199
}
92100
return recordList;
93101
}
@@ -108,9 +116,9 @@ public T findById(Long id) {
108116
pstmt.setLong(1, id);
109117
try (ResultSet rs = pstmt.executeQuery()) {
110118
if (rs.next()) {
111-
T record = getDeclaredClassInstance();
112-
record.setFieldsFromResultSet(rs);
113-
return record;
119+
T theRecord = getDeclaredClassInstance();
120+
theRecord.setFieldsFromResultSet(rs);
121+
return theRecord;
114122
}
115123
return getDeclaredClassInstance();
116124
}
@@ -125,9 +133,8 @@ public T findById(Long id) {
125133
*/
126134
public void save() {
127135
try (Connection connection = getConnection();
128-
// TODO
129-
PreparedStatement pstmt = connection.prepareStatement(null,
130-
PreparedStatement.RETURN_GENERATED_KEYS)) {
136+
PreparedStatement pstmt = connection.prepareStatement(constructInsertQuery(),
137+
Statement.RETURN_GENERATED_KEYS)) {
131138

132139
setPreparedStatementParams(pstmt);
133140
pstmt.executeUpdate();

0 commit comments

Comments
 (0)