日期:2014-05-17  浏览次数:20629 次

菜鸟,求高手!使用prepareStatement,sql语句执行成功,控制台报错“'?' 附近有语法错误”
菜鸟一个,求高手解救! ~。~
使用prepareStatement,sql语句执行成功,数据库可以看到插入的数据,控制台打印语句:
INSERT INTO RouteCar (CarID,RouteID) VALUES ( ? ,'11') 
但是,在myeclipse控制台中总是报错:com.microsoft.sqlserver.jdbc.SQLServerException: '?' 附近有语法错误。
代码如下:

Java code

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
            request.setCharacterEncoding("utf8");
            int RouteID = Integer.parseInt(request.getParameter("RouteID"));
            String[] ids =request.getParameterValues("items");// jsp页面传过来的参数,jsp中是checkbox得到的。
            String BindState = "已绑定";
            
            for(int i = 0; i < ids.length; i++){
                ConnectDB db=new ConnectDB();
                db.Connect();
                PreparedStatement pstmt1=null;
                PreparedStatement pstmt2=null;
            
                String sql = "INSERT INTO RouteCar (CarID,RouteID) VALUES ( ? ,'" + RouteID + "') ";
                String sql2 = "UPDATE CarInfo SET BindState ='" + BindState + "' where CarID = ? ";
                String sql3 = "select * from RouteInfo where RouteID = '"+ RouteID + "'";
                int rs = 0;    
                try {
                    pstmt1 = db.conn.prepareStatement(sql);
                    pstmt1.setInt(1,Integer.parseInt(ids[i]));
                    
                    System.out.println(ids[i]);
                    System.out.println(sql);
                    
                    rs = pstmt1.executeUpdate();
                    
                    pstmt2 = db.conn.prepareStatement(sql2);
                    pstmt2.setInt(1,Integer.parseInt(ids[i]));
                    pstmt2.executeUpdate();
                    
                    ResultSet rst=db.stmt.executeQuery(sql3);
                    if(rst.next()){
                        int RouteID2;
                        String StartCity = rst.getString(2);
                        String EndCity = rst.getString(3);
                        String sql4 = "select * from RouteInfo where StartCity = '"+ EndCity + "' and EndCity = '"+ StartCity + "'";
                        ResultSet rst2=db.stmt.executeQuery(sql4);
                        if(rst2.next()){
                            RouteID2 = rst2.getInt(1);
                            db.stmt.executeUpdate("INSERT INTO RouteCar (RouteID,CarID)  VALUES ('" + RouteID2 + "',?) ");
                        }
                        response.setContentType("text/html;charset=UTF-8");
                        PrintWriter  out = response.getWriter();
                        out.print("<meta   http-equiv='Content-Type'   content='text/html;   charset=UTF-8'>");   
                        out.print("<script>");
                        out.print("alert('添加成功!');");
                        out.print("window.location.href='RouteManage/RouteCarsBinding.jsp'");
                        out.print("</script>");
                    }
                } 
                catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    response.setContentType("text/html;charset=UTF-8");