Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        2年前关闭。
                                                                                            
                
        
我试图在我的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());
    }
}


我以html格式键入whatevr参数时出现错误:

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

最佳答案

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

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

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


example中,您可以学习如何使用PreparedStatement

07-27 20:29