teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

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

2021/12/12 09:20

投稿

Yakusugi
Yakusugi

スコア123

title CHANGED
File without changes
body CHANGED
@@ -59,7 +59,6 @@
59
59
 
60
60
  public List<BudgetTrackerDto> selectAll() throws FileNotFoundException, IOException {
61
61
  List<BudgetTrackerDto> budgetList = new ArrayList<>();
62
-
63
62
 
64
63
  try (Connection conn = BudgetTrackerDao.getConnection(); PreparedStatement ps = conn.prepareStatement(SQL)) {
65
64
 
@@ -89,24 +88,30 @@
89
88
  public int insertIntoTable(BudgetTrackerDto budgetTrackerDto) throws SQLException {
90
89
  int rowsCount = 0;
91
90
  btd = new BudgetTrackerDto();
92
-
93
- String sql = "INSERT INTO budget_table(id,Date,StoreName, ProductName, ProductType, Price) " + "VALUE(?,?,?,?,?,?)";
94
- PreparedStatement pstmt = myConn.prepareStatement(sql);
95
- pstmt.setInt(1, btd.getId());
96
- pstmt.setTime(2, (Time) btd.getDate());
97
- pstmt.setString(3, btd.getStoreName());
98
- pstmt.setString(4, btd.getProductName());
99
- pstmt.setString(5, btd.getProductType());
100
- pstmt.setInt(6, btd.getPrice());
101
- pstmt.executeUpdate();
91
+ Statement pstmt;
102
- pstmt.close();
103
-
104
92
  try {
105
93
  // DBに接続
106
94
  myConn = BudgetTrackerDao.getConnection();
107
- mySmt = myConn.createStatement();
95
+ pstmt = myConn.createStatement();
96
+
97
+ // String sql = "INSERT INTO budget_table(id,Date,StoreName, ProductName, ProductType, Price) " + "VALUE('"
98
+ // + btd.getId() + "','" + btd.getDate() + "','" + btd.getStoreName() + "','" + btd.getProductName() + "','" + btd.getProductType()
99
+ // + "','" + btd.getPrice() + "')";
100
+
101
+ String sql = "INSERT INTO budget_table(id,Date,StoreName, ProductName, ProductType, Price) "
102
+ + "VALUE (?,?,?,?,?,?)";
103
+ pstmt = myConn.prepareStatement(sql);
104
+ ((PreparedStatement) pstmt).setInt(1, btd.getId());
105
+ ((PreparedStatement) pstmt).setTime(2, (Time) btd.getDate());
106
+ ((PreparedStatement) pstmt).setString(3, btd.getStoreName());
107
+ ((PreparedStatement) pstmt).setString(4, btd.getProductName());
108
+ ((PreparedStatement) pstmt).setString(5, btd.getProductType());
109
+ ((PreparedStatement) pstmt).setInt(6, btd.getPrice());
110
+ pstmt.executeUpdate(sql);
111
+ pstmt.close();
112
+
108
113
  // SQL文発行
109
- rowsCount = mySmt.executeUpdate(sql);
114
+ rowsCount = pstmt.executeUpdate(sql);
110
115
  } catch (SQLException e) {
111
116
  System.out.println("Errorが発生しました!\n" + e + "\n");
112
117
  } finally {