Java上的PreparedStatement问题

时间:2019-05-24 15:12:19

标签: java mysql jdbc

我的pstmt.executeUpdate()出错。.

由于助手的帮助,我找不到错误的原因

public void createCoupon(Coupon coupon) throws Exception {
    Connection connection = DriverManager.getConnection(DB.Database.getUrl(), DB.Database.getUser(),
            DB.Database.getPass());

    String sql = "INSERT INTO COUPON (ID, TITLE, START_DATE, END_DATE, AMOUNT, TYPE, MESSAGE, PRICE, IMAGE) VALUES (?,?,?,?,?,?,?,?,?)";
    try (PreparedStatement pstmt = connection.prepareStatement(sql)) {

        pstmt.setInt(1, coupon.getId());
        pstmt.setString(2, coupon.getTitle());

        pstmt.setDate(3,(java.sql.Date) coupon.getStart_date());
        pstmt.setDate(4,(java.sql.Date) coupon.getEnd_date());
        pstmt.setInt(5,coupon.getAmount());
        pstmt.setString(6, coupon.getType().name());
        pstmt.setString(7, coupon.getMessage());
        pstmt.setDouble(8, coupon.getPrice());
        pstmt.setString(9, coupon.getImage());

        pstmt.executeUpdate();
        System.out.println("Insert " + coupon + " succeed ");
    } catch (SQLException e) {
        e.printStackTrace();
        throw new Exception("Coupon insert failed");

    } finally {
        connection.close();
    }

}
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'TYPE' in 'field list'
  at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
  at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
  at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
  at java.lang.reflect.Constructor.newInstance(Unknown Source)
  at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
  at com.mysql.jdbc.Util.getInstance(Util.java:408)
  at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:944)
  at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3978)
  at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3914)
  at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2530)
  at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2683)
  at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2495)
  at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1903)
  at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2124)
  at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2058)
  at com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5158)
  at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2043)
  at Coupon.CouponDBDAO.createCoupon(CouponDBDAO.java:69)
  at Company.CompanyFacade.createCoupon(CompanyFacade.java:61)
  at Test.main(Test.java:82)
Exception in thread "main" java.lang.Exception: Coupon insert failed
  at Coupon.CouponDBDAO.createCoupon(CouponDBDAO.java:73)
  at Company.CompanyFacade.createCoupon(CompanyFacade.java:61)
  at Test.main(Test.java:82)

0 个答案:

没有答案