質問編集履歴

1

処理の順序を変更しました。

2021/12/12 09:20

投稿

Yakusugi
Yakusugi

スコア123

test CHANGED
File without changes
test CHANGED
@@ -120,8 +120,6 @@
120
120
 
121
121
  List<BudgetTrackerDto> budgetList = new ArrayList<>();
122
122
 
123
-
124
-
125
123
 
126
124
 
127
125
  try (Connection conn = BudgetTrackerDao.getConnection(); PreparedStatement ps = conn.prepareStatement(SQL)) {
@@ -180,29 +178,7 @@
180
178
 
181
179
  btd = new BudgetTrackerDto();
182
180
 
183
-
184
-
185
- String sql = "INSERT INTO budget_table(id,Date,StoreName, ProductName, ProductType, Price) " + "VALUE(?,?,?,?,?,?)";
186
-
187
- PreparedStatement pstmt = myConn.prepareStatement(sql);
188
-
189
- pstmt.setInt(1, btd.getId());
190
-
191
- pstmt.setTime(2, (Time) btd.getDate());
192
-
193
- pstmt.setString(3, btd.getStoreName());
194
-
195
- pstmt.setString(4, btd.getProductName());
196
-
197
- pstmt.setString(5, btd.getProductType());
198
-
199
- pstmt.setInt(6, btd.getPrice());
200
-
201
- pstmt.executeUpdate();
181
+ Statement pstmt;
202
-
203
- pstmt.close();
204
-
205
-
206
182
 
207
183
  try {
208
184
 
@@ -210,11 +186,45 @@
210
186
 
211
187
  myConn = BudgetTrackerDao.getConnection();
212
188
 
213
- mySmt = myConn.createStatement();
189
+ pstmt = myConn.createStatement();
190
+
191
+
192
+
193
+ // String sql = "INSERT INTO budget_table(id,Date,StoreName, ProductName, ProductType, Price) " + "VALUE('"
194
+
195
+ // + btd.getId() + "','" + btd.getDate() + "','" + btd.getStoreName() + "','" + btd.getProductName() + "','" + btd.getProductType()
196
+
197
+ // + "','" + btd.getPrice() + "')";
198
+
199
+
200
+
201
+ String sql = "INSERT INTO budget_table(id,Date,StoreName, ProductName, ProductType, Price) "
202
+
203
+ + "VALUE (?,?,?,?,?,?)";
204
+
205
+ pstmt = myConn.prepareStatement(sql);
206
+
207
+ ((PreparedStatement) pstmt).setInt(1, btd.getId());
208
+
209
+ ((PreparedStatement) pstmt).setTime(2, (Time) btd.getDate());
210
+
211
+ ((PreparedStatement) pstmt).setString(3, btd.getStoreName());
212
+
213
+ ((PreparedStatement) pstmt).setString(4, btd.getProductName());
214
+
215
+ ((PreparedStatement) pstmt).setString(5, btd.getProductType());
216
+
217
+ ((PreparedStatement) pstmt).setInt(6, btd.getPrice());
218
+
219
+ pstmt.executeUpdate(sql);
220
+
221
+ pstmt.close();
222
+
223
+
214
224
 
215
225
  // SQL文発行
216
226
 
217
- rowsCount = mySmt.executeUpdate(sql);
227
+ rowsCount = pstmt.executeUpdate(sql);
218
228
 
219
229
  } catch (SQLException e) {
220
230