2 cxxae cxxae 于 2015.06.03 14:50 提问

Parameter index out of range 好心人求解决在线等 急

代码如下 求解哪里错误
package com.pact.mobilestore;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class openorder extends HttpServlet {
/**

  • */ private static final long serialVersionUID = 1L;

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 统一编码格式
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");

Connection conn = null; // 连接对象

PreparedStatement preparedStatement = null;

HttpSession session = request.getSession();

Object obj = session.getAttribute("currId");

String UId = (String) obj;

String option = request.getParameter("option");

int op =Integer.parseInt(option);

   switch(op) 
{ 
case 0: 
    try {
        Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql://localhost:3306/mobilestore";
        String userName = "root";
        String pass = "root";
        conn = DriverManager.getConnection(url, userName, pass);

        String sql = "select * from `order` where UId =?";
        preparedStatement = conn.prepareStatement(sql);
        preparedStatement.setString(1, UId);
        java.sql.ResultSet rs = preparedStatement.executeQuery();
        List<Entery4> list = new ArrayList<Entery4>();

        while (rs.next()) {
            Entery4 ent4 = new Entery4();
            String OId = rs.getString("OId");
            String Date = rs.getString("Date");
            String payDate = rs.getString("payDate");
            String Total = rs.getString("Total"); // 购买的数量
            String state = rs.getString("state");

            ent4.setOId(OId);
            ent4.setUserId(UId);
            ent4.setDate(Date);
            ent4.setPayDate(payDate);
            ent4.setTotal(Total);
            ent4.setState(state);

            list.add(ent4);

        }
        rs.close();

        request.setAttribute("info", list);

    } catch (SQLException e) {
        // TODO: handle exception
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        try {
            preparedStatement.close();
            conn.close();
        } catch (Exception e2) {
            // TODO: handle exception
        }
    }               
    request.getRequestDispatcher("myorder.jsp").forward(request,
            response);
       break; 

   case 1: 
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/mobilestore";
            String userName = "root";
            String pass = "root";
            conn = DriverManager.getConnection(url, userName, pass);

            String sql = "select * from `order` where UId = ?and state =?";
            preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setString(1, UId);
            preparedStatement.setString(2, "未支付");
            java.sql.ResultSet rs = preparedStatement.executeQuery();
            List<Entery4> list = new ArrayList<Entery4>();

            while (rs.next()) {
                Entery4 ent4 = new Entery4();
                String OId = rs.getString("OId");
                String Date = rs.getString("Date");
                String payDate = rs.getString("payDate");
                String Total = rs.getString("Total"); // 购买的数量
                String state = rs.getString("state");

                ent4.setOId(OId);
                ent4.setUserId(UId);
                ent4.setDate(Date);
                ent4.setPayDate(payDate);
                ent4.setTotal(Total);
                ent4.setState(state);

                list.add(ent4);

            }
            rs.close();

            request.setAttribute("info", list);

        } catch (SQLException e) {
            // TODO: handle exception
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                preparedStatement.close();
                conn.close();
            } catch (Exception e2) {
                // TODO: handle exception
            }
        }               
        request.getRequestDispatcher("myorder1.jsp").forward(request,
                response);

       break; 
   case 2: 
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/mobilestore";
            String userName = "root";
            String pass = "root";
            conn = DriverManager.getConnection(url, userName, pass);

            String sql = "select * from `order` where UId = ? and state =?";
            preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setString(1, UId);
            preparedStatement.setString(2, "已支付");
            java.sql.ResultSet rs = preparedStatement.executeQuery();
            List<Entery4> list = new ArrayList<Entery4>();

            while (rs.next()) {
                Entery4 ent4 = new Entery4();
                String OId = rs.getString("OId");
                String Date = rs.getString("Date");
                String payDate = rs.getString("payDate");
                String Total = rs.getString("Total"); // 购买的数量
                String state = rs.getString("state");

                ent4.setOId(OId);
                ent4.setUserId(UId);
                ent4.setDate(Date);
                ent4.setPayDate(payDate);
                ent4.setTotal(Total);
                ent4.setState(state);

                list.add(ent4);

            }
            rs.close();

            request.setAttribute("info", list);

        } catch (SQLException e) {
            // TODO: handle exception
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                preparedStatement.close();
                conn.close();
            } catch (Exception e2) {
                // TODO: handle exception
            }
        }               
        request.getRequestDispatcher("myorder2.jsp").forward(request,
                response); 

       break; 

// default:
// System.out.println("default");
// break;
}
}
public void doGet(HttpServletRequest req, HttpServletResponse resp)

    throws ServletException, IOException {
        doPost(req, resp);

    }   

}
错误提示截取
java.sql.SQLException: Parameter index out of range (2 > number of parameters, which is 1).
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:910)
at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:2715)
at com.mysql.jdbc.PreparedStatement.setString(PreparedStatement.java:3546)
at com.pact.mobilestore.openorder.doPost(openorder.java:56)
at com.pact.mobilestore.openorder.doGet(openorder.java:221)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)

2个回答

edouardzyc
edouardzyc   2015.06.03 16:20

你用的预编译的方式,预编译替换参数的时候去找? 来替换成你set的值, ?and没有被解析出来,程序认为你就一个参数了

Tiger_Zhao
Tiger_Zhao   Rxr 2015.06.04 08:53

UId是不是数值类型?用setInt试试。

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!