55import java .sql .PreparedStatement ;
66import java .sql .ResultSet ;
77import java .sql .SQLException ;
8+ import java .sql .Statement ;
89import java .util .ArrayList ;
910import java .util .List ;
1011import 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