无法使用预准备语句插入MySQL数据库

时间:2018-03-27 19:20:27

标签: mysql sql-insert

我用于数据插入的代码。 这个过程工作正常,直到执行语句。不知道我为什么要面对这个问题。

package com.emandi.dao;
import java.sql.Date;
import java.sql.Statement;
import com.emandi.dbconnection.DBConnection;
import com.emandi.domain.Item;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

public class ItemDAO {

    public void createItem(Item item) throws SQLException, ClassNotFoundException {

        try {

        Class.forName("com.mysql.jdbc.Driver").newInstance();

        } catch (Exception ex) {

        }

       Connection con = DBConnection.getConnection();

       String query = "insert into item(item_id, item_name, total_quantity, available_quantity,date,user_id,item_type_id)"

        + " values (?, ?, ?, ?,?,?,?)";

      PreparedStatement preparedStmt = con.prepareStatement(query);

      Long a12= item.getItemId();

      preparedStmt.setLong(1,a12);
      preparedStmt.setString(2,item.getItemName());
      preparedStmt.setDouble (3,item.getTotalQuantity());
      preparedStmt.setDouble (4,item.getAvailableQuantity());

      //java.sql.Date d= new java.sql.Date(format.parse(source).getTime());
      java.util.Date utilStartDate = item.getDate();

      java.sql.Date sqlStartDate = new java.sql.Date(utilStartDate.getTime());

      preparedStmt.setDate(5,sqlStartDate);

      Long as = item.getUser().getUserId();

      preparedStmt.setLong(6,as);

      Long as1 = item.getItemType().getItemTypeId();

      preparedStmt.setLong(7,as1);  

      System.out.println("Before");

      preparedStmt.executeUpdate();

      System.out.println("After");

    }

}

Class项如下

public class Item {

    private Long itemId;
    private User user;
    private ItemType itemType;
    private String itemName;
    private Double totalQuantity;
    private Double availableQuantity;
    private Date date;
}

我现在还附加了输出屏幕。enter image description here

现在我还使用Total_quantity的正确拼写更新了插入查询。

仍面临问题

0 个答案:

没有答案