JDBCを使ってCUIベースのデータベースアプリを作成しております。
下記、86,87行目で記述しているInsert文でSyntaxエラーが起きているのですが、DBeaverで同じSQLを実行するとデータを入れることは出来ました。
何故JDBCではエラーが起きるのか、ヒントがあればご教示頂けますと幸いです。
String sql = "INSERT INTO budget_table(id,Date,StoreName, ProductName, ProductType, Price) " + "VALUE (?,?,?,?,?,?)";
エラー内容 2021/12/15 15:49
Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method setDate(int, java.sql.Date) in the type PreparedStatement is not applicable for the arguments (int, java.util.Date) at BudgetTrackerCui/com.jdbc.budgettracker.dao.BudgetTrackerDao.insertIntoTable(BudgetTrackerDao.java:93) at BudgetTrackerCui/com.jdbc.budgettracker.main.BudgetTrackerMain.main(BudgetTrackerMain.java:130)
BudgetTrackerDao.java
package com.jdbc.budgettracker.dao; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Time; import java.util.Date; import java.util.ArrayList; import java.util.List; import java.util.Properties; import com.jdbc.budgettracker.core.BudgetTrackerDto; public class BudgetTrackerDao { BudgetTrackerDto btd; private Connection myConn; private PreparedStatement mySmt; private static final String SELECTALL = "select * from budget_table;"; // private static final String SELECTBYSTORENAME = "select * from budget_table where store name = ?;"; private static Connection getConnection() { try { // get db properties Properties props = new Properties(); props.load(new FileInputStream( "/home/yosuke/git/BudgetTrackerCui/BudgetTrackerCui/sql/config_budgettracker.properties")); String user = props.getProperty("user"); String password = props.getProperty("password"); String dburl = props.getProperty("dburl"); // Class.forName(RDB_DRIVE); Connection con = DriverManager.getConnection(dburl, user, password); return con; } catch (Exception e) { throw new IllegalStateException(e); } } // select all public List<BudgetTrackerDto> selectAll() throws FileNotFoundException, IOException { List<BudgetTrackerDto> budgetList = new ArrayList<>(); try (Connection conn = BudgetTrackerDao.getConnection(); PreparedStatement ps = conn.prepareStatement(SELECTALL)) { try (ResultSet rs = ps.executeQuery()) { while (rs.next()) { btd = new BudgetTrackerDto(); btd.setId(rs.getInt("ID")); btd.setDate(rs.getDate("Date")); btd.setStoreName(rs.getString("StoreName")); btd.setProductName(rs.getString("ProductName")); btd.setProductType(rs.getString("ProductType")); btd.setPrice(rs.getInt("Price")); budgetList.add(btd); } } } catch (SQLException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { } return budgetList; } public int insertIntoTable(BudgetTrackerDto btd) throws SQLException { int rowsCount = 0; PreparedStatement pstmt; try { // DBに接続 myConn = BudgetTrackerDao.getConnection(); // pstmt = (PreparedStatement) myConn.createStatement(); // String sql = "INSERT INTO budget_table(id,Date,StoreName, ProductName, ProductType, Price) " + "VALUE('" // + btd.getId() + "','" + btd.getDate() + "','" + btd.getStoreName() + "','" + btd.getProductName() + "','" + btd.getProductType() // + "','" + btd.getPrice() + "')"; String sql = "INSERT INTO budget_table(id,Date,StoreName, ProductName, ProductType, Price) " + "VALUE (?,?,?,?,?,?)"; pstmt = myConn.prepareStatement(sql); pstmt.setInt(1, btd.getId()); Date date = btd.getDate(); long timeInMilliSeconds = date.getTime(); java.sql.Date date1 = new java.sql.Date(timeInMilliSeconds); pstmt.setDate(2, date1); pstmt.setString(3, btd.getStoreName()); pstmt.setString(4, btd.getProductName()); pstmt.setString(5, btd.getProductType()); pstmt.setInt(6, btd.getPrice()); // SQL文発行 pstmt.executeUpdate(); System.out.println("Suucessfully added"); pstmt.close(); //rowsCount = pstmt.executeUpdate(sql); } catch (SQLException e) { System.out.println("Errorが発生しました!\n" + e + "\n"); } finally { // リソースの開放 if (mySmt != null) { try { mySmt.close(); } catch (SQLException ignore) { } } if (myConn != null) { try { myConn.close(); } catch (SQLException ignore) { } } } return 0; } }
BudgetTrackerDto.java
package com.jdbc.budgettracker.core; import java.util.Date; public class BudgetTrackerDto { private int id; private Date date; private String storeName; private String productName; private String productType; private int price; public BudgetTrackerDto() { } public BudgetTrackerDto(int id, Date date, String storeName, String productName, String productType, int price) { super(); this.id = id; this.date = date; this.storeName = storeName; this.productName = productName; this.productType = productType; this.price = price; } public int getId() { return id; } public void setId(int id) { this.id = id; } public Date getDate() { return date; } public void setDate(Date insertDate) { this.date = insertDate; } public String getStoreName() { return storeName; } public void setStoreName(String storeName) { this.storeName = storeName; } public String getProductName() { return productName; } public void setProductName(String productName) { this.productName = productName; } public String getProductType() { return productType; } public void setProductType(String productType) { this.productType = productType; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } }
BudgetTrackerMain.java
package com.jdbc.budgettracker.main; import java.io.FileNotFoundException; import java.io.IOException; import java.sql.SQLException; import java.util.Date; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Scanner; import com.jdbc.budgettracker.core.BudgetTrackerDto; import com.jdbc.budgettracker.dao.BudgetTrackerDao; public class BudgetTrackerMain { 一部省略 switch (initialNumInt) { case 2: // Insert int insertcannerInt = 0; System.out.println("You chose " + initialSwitchMap.get(2)); budgetTrackerDto = new BudgetTrackerDto(); Scanner insertScanner = new Scanner(System.in); System.out.print("Input an ID: "); int insertScannerInt = insertScanner.nextInt(); budgetTrackerDto.setId(insertScannerInt); System.out.print("Input Date (yyyy-MM-dd): "); String insertScannerStr = insertScanner.next(); Date insertDate=(Date) new SimpleDateFormat("yyyy-MM-dd").parse(insertScannerStr); budgetTrackerDto.setDate(insertDate); System.out.print("Input a store name: "); insertScannerStr = insertScanner.next(); budgetTrackerDto.setStoreName(insertScannerStr); System.out.print("Input a product name: "); insertScannerStr = insertScanner.next(); budgetTrackerDto.setProductName(insertScannerStr); System.out.print("Input a product type: "); insertScannerStr = insertScanner.next(); budgetTrackerDto.setProductType(insertScannerStr); System.out.print("Input price: "); insertScannerStr = insertScanner.next(); insertcannerInt = Integer.parseInt(insertScannerStr); budgetTrackerDto.setPrice(insertcannerInt); budgetTrackerDao = new BudgetTrackerDao(); budgetTrackerDao.insertIntoTable(budgetTrackerDto); } } }
回答2件
あなたの回答
tips
プレビュー