DAO方法失败

时间:2018-03-21 15:40:50

标签: java mysql jsp servlets jdbc

我正在尝试在我的JEE项目中调用DAO方法,在MySQL中创建一个表。

以下是代码的外观:

 @Override
String execute(HttpServletRequest request, HttpServletResponse response) throws LoginSampleException {

    int height = Integer.parseInt(request.getParameter("height"));
    int length = Integer.parseInt(request.getParameter("length"));
    int width = Integer.parseInt(request.getParameter("width"));

    LegoHouseAlgorithm lego = new LegoHouseAlgorithm();

    ArrayList<Integer> bricks = lego.calc(height, length, width);

    request.setAttribute("longbrick", Integer.toString(bricks.get(0)));
    request.setAttribute("mediumbrick", Integer.toString(bricks.get(1)));
    request.setAttribute("shortbrick", Integer.toString(bricks.get(2)));
    request.setAttribute("wall3", Integer.toString(bricks.get(3)));
    request.setAttribute("wall4", Integer.toString(bricks.get(4)));
    request.setAttribute("wall5", Integer.toString(bricks.get(5)));

    int finalLongBrick = (bricks.get(0) + bricks.get(3)) * 2;
    int finalMediumBrick = (bricks.get(1) + bricks.get(4)) * 2;
    int finalShortBrick = (bricks.get(2) + bricks.get(5)) * 2;

    request.setAttribute("finallongbrick", finalLongBrick);
    request.setAttribute("finalmediumbrick", finalMediumBrick);
    request.setAttribute("finalshortbrick", finalShortBrick);


    try {
        LogicFacade.makeOrder(height, length, width);
    } catch (SQLException | ClassNotFoundException ex) {
        Logger.getLogger(Order.class.getName()).log(Level.SEVERE, null, ex);
    }


return "order";

我正在使用抽象类的自定义框架来确定调用堆栈:

   public static OrderSample makeOrder(int height, int length, int width) throws SQLException, ClassNotFoundException, LoginSampleException{
    OrderSample order = new OrderSample(height, width, length);
    UserMapper.createOrder(order);
    return order;
  }


public static void createOrder (OrderSample order) throws SQLException,    ClassNotFoundException, LoginSampleException{
    try{
        Connection con = Connector.connection();
        String SQL = "INSERT INTO orders (height, length, width) VALUES (?, ?, ?)";
        PreparedStatement ps = con.prepareStatement(SQL);
        ps.setInt(order.getHeigh(), 1);
        ps.setInt(order.getLength(), 2);
        ps.setInt(order.getWidth(), 3);
        ps.executeUpdate();
        ResultSet rs = ps.getGeneratedKeys();
        rs.next();
        int id = rs.getInt(1);
        order.setId(id);

     } catch ( ClassNotFoundException | SQLException ex ) {
        throw new LoginSampleException(ex.getMessage());
    }    
}

whatevr参数我输入我的html表单我得到错误:

Parameter index out of range ((inserted number) > number of parameters, which is 3). 

2 个答案:

答案 0 :(得分:0)

docs的外观中,您必须在setInt()方法中将参数的顺序切换为createOrder
第一个参数应该是索引,第二个是值

答案 1 :(得分:0)

您错误地输入了setInt()的参数。第一个参数是索引,第二个是值。

要更正它,请使用以下代码:

ps.setInt(1,order.getHeigh());
ps.setInt(2,order.getLength());
ps.setInt(3,order.getWidth());

example中,您可以了解如何使用PreparedStatement