5
5
import java .sql .PreparedStatement ;
6
6
import java .sql .ResultSet ;
7
7
import java .sql .SQLException ;
8
+ import java .sql .Statement ;
8
9
import java .util .ArrayList ;
9
10
import java .util .List ;
10
11
import javax .sql .DataSource ;
@@ -58,6 +59,13 @@ protected Connection getConnection() {
58
59
*/
59
60
protected abstract String getTableName ();
60
61
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
+
61
69
/**
62
70
* Set all the fields into the underlying domain model from the result set.
63
71
*
@@ -85,9 +93,9 @@ public List<T> findAll() {
85
93
PreparedStatement pstmt = conn .prepareStatement (constructFindAllQuery ())) {
86
94
try (ResultSet rs = pstmt .executeQuery ()) {
87
95
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 );
91
99
}
92
100
return recordList ;
93
101
}
@@ -108,9 +116,9 @@ public T findById(Long id) {
108
116
pstmt .setLong (1 , id );
109
117
try (ResultSet rs = pstmt .executeQuery ()) {
110
118
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 ;
114
122
}
115
123
return getDeclaredClassInstance ();
116
124
}
@@ -125,9 +133,8 @@ public T findById(Long id) {
125
133
*/
126
134
public void save () {
127
135
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 )) {
131
138
132
139
setPreparedStatementParams (pstmt );
133
140
pstmt .executeUpdate ();
0 commit comments