Skip to content

Commit

Permalink
implement a simple save method (iluwatar#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
sergejsvisockis committed Apr 1, 2024
1 parent 9a45dd7 commit d7d3a44
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -85,9 +93,9 @@ public List<T> 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;
}
Expand All @@ -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();
}
Expand All @@ -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();
Expand Down

0 comments on commit d7d3a44

Please sign in to comment.