qq_57931996 2023-04-06 19:49 采纳率: 88.9%
浏览 52
已结题

JSP 数据库操作 语法和结果集报错

JSP Web网页实训
参数cid=2

img


报错

img


数据库

img

img

HomeDAO

package dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import entity.Classification;
import entity.Commodity;
import entity.Information;
import entity.Knowledge;
import entity.Recruit;
import util.DBHelper;
import util.PageBean;

/**
 * 前端集合
 */
public class HomeDAO {



    /**
     * 产品搜索
     *
     * @param pb
     *            分页对象
     * @param cid
     *            分类ID
     * @param title
     *            商品名
     * @return
     * @throws Exception
     */
    public Map<String, Object> searchCommodity(PageBean pb, int cid, String title) throws Exception {
        PreparedStatement stmt = null; // 产品语句对象
        ResultSet rs = null; // 产品数据集
        List<Commodity> list = new ArrayList<Commodity>(); // 产品集合

        PreparedStatement cstmt = null; // 分类语句对象
        ResultSet crs = null; // 分类数据集
        List<Classification> clist = new ArrayList<Classification>(); // 分类集合
        // 查询总记录数;设置到pb对象中
        int totalCount = this.searchTotalCount(cid, title);  //------------------------------------------------------------142行
        pb.setTotalCount(totalCount);
        // 判断(预防当前页为首页或末页时,点击上一页或下一页出错)
        if (pb.getCurrentPage() <= 0) {
            pb.setCurrentPage(1); // 把当前页设置为1
        } else if (pb.getCurrentPage() > pb.getTotalPage()) {
            pb.setCurrentPage(pb.getTotalPage()); // 把当前页设置为最大页数
        }

        // 获取当前页:计算查询的起始行、返回的行数
        int currentPage = pb.getCurrentPage(); // 当前页
        int index = (currentPage - 1) * pb.getPageCount(); // 查询的起始行
        int count = pb.getPageCount(); // 查询返回的行数
        try {
            // 商品

            // 判断 搜索条件为分类
            if (title == null || "".equals(title.trim())) {
                String sql = "select * from commodity where class_id=? limit ?,?;"; // SQL语句
                stmt = DBHelper.getConnection().prepareStatement(sql); // 创建链接对象
                stmt.setInt(1, cid);
                stmt.setInt(2, index);
                stmt.setInt(3, count);
            } else { // 搜索条件为分类与名称
                String sql = "select * from commodity where title like concat('%',?,'%') and class_id=? limit ?,?;"; // SQL语句
                stmt = DBHelper.getConnection().prepareStatement(sql); // 创建链接对象
                stmt.setString(1, title);
                stmt.setInt(2, cid);
                stmt.setInt(3, index);
                stmt.setInt(4, count);
            }
            rs = stmt.executeQuery(); // 获得数据集//------------------------------------------------------------173行
            while (rs.next()) {
                Commodity lists = new Commodity();
                lists.setId(rs.getInt("id"));
                lists.setTitle(rs.getString("title"));
                lists.setPrice(rs.getFloat("price"));
                lists.setPhoto(rs.getString("photo"));
                list.add(lists); // 把一个商品信息加入集合
            }

            // 分类
            String sqls = "select * from commodity_class"; // SQL语句
            cstmt = DBHelper.getConnection().prepareStatement(sqls); // 创建链接对象
            crs = cstmt.executeQuery(); // 获得数据集
            while (crs.next()) {
                Classification lists = new Classification();
                lists.setId(crs.getInt("id"));
                lists.setTitle(crs.getString("title"));
                clist.add(lists); // 把一个分类信息加入集合
            }

            String url = "search_commodity&cid=" + cid + "&title=" + title;
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("PageDate", pb);
            map.put("list", list);
            map.put("clist", clist);
            map.put("cid", cid); // 分类 用于判断分页与跳转
            map.put("url", url); // 搜索商品名 用于分页跳转
            return map;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        } finally {
            // 释放数据集对象
            if (rs != null) {
                try {
                    rs.close();
                    rs = null;
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
            if (crs != null) {
                try {
                    crs.close();
                    crs = null;
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
            // 释放语句对象
            if (stmt != null) {
                try {
                    stmt.close();
                    stmt = null;
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
            if (cstmt != null) {
                try {
                    cstmt.close();
                    cstmt = null;
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
    }

   
    /**
     * 产品展示搜索 获取总数
     *
     * @param cid
     *            分类ID
     * @param title
     *            商品名
     * @return
     * @throws Exception
     */
    public int searchTotalCount(int cid, String title) throws Exception {
        PreparedStatement pStatement = null; // 语句对象
        ResultSet rSet = null; // 数据集
        int count = 0;
        try {
            // 判断 搜索条件为分类
            if (title == null || "".equals(title.trim())) {
                String sql = "select * from commodity where class_id=?; "; // SQL语句
                pStatement = DBHelper.getConnection().prepareStatement(sql);
                pStatement.setInt(1, cid);
            } else { // 搜索条件为分类与名称
                String sql = "select * from commodity where title like concat('%',?,'%') and class_id=?; "; // SQL语句
                pStatement = DBHelper.getConnection().prepareStatement(sql); // 创建链接对象
                pStatement.setString(1, title);
                pStatement.setInt(2, cid);
            }
            rSet = pStatement.executeQuery();
            rSet.last(); // 移到最后一行//------------------------------------------------------------696行
            count = rSet.getRow(); // 得到当前行号,也就是记录数
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 释放数据集对象
            if (rSet != null) {
                try {
                    rSet.close();
                    rSet = null;
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
            // 释放语句对象
            if (pStatement != null) {
                try {
                    pStatement.close();
                    pStatement = null;
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
        return count;
    }

  
}

HomeServlet

package servlet;

import dao.HomeDAO;
import entity.Information;
import entity.Knowledge;
import entity.Recruit;
import util.PageBean;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;

@WebServlet(name = "HomeServlet", value = "/HomeServlet")
public class HomeServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private String uri;
    //Constructor of the object.
    public HomeServlet() {
        super();
    }
    //Destruction of the servlet.
    public void destroy() {
        super.destroy();// Just puts "destroy" string in log
        // Put your code here
    }

    /**
     * The doGet method of the servlet.
     *
     * This method is called when a form has its tag value method equals to get.
     *
     * @param request
     *          the request send by the client to the server
     * @param response
     *          the response send by the server to the client
     * @throws ServletException
     *          if an error occurred
     * @throws IOException
     *          if an error occurred
     */
       @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        //获取操作的类型
        String method = request.getParameter("method");
        //判断
        if ("commodity".equals(method)) {
            //产品展示
            commodity(request, response);
        }else if ("knowledge".equals(method)){
            //红酒知识
            knowledge(request, response);
        }else if ("news".equals(method)){
            //公司资讯
            news(request, response);
        }else if ("recruit".equals(method)){
            //招贤纳士
            recruit(request, response);
        }else if ("search_commodity".equals(method)){
            //产品展示搜索
            search_commodity(request, response);//------------------------------------------------------------66行
        }else if ("commodity_detail".equals(method)){
            //商品详情
            commodity_detail(request, response);
        }else if ("knowledge_detail".equals(method)){
            //红酒知识详情
            knowledge_detail(request, response);
        }else if ("information_detail".equals(method)){
            //公司资讯详情
            information_detail(request, response);
        }
    }

    /**
     * The doPost method of the servlet.
     *
     * This method is called when a form has its tag value method equals to post.
     *
     * @param request
     *          the request send by the client to the server
     * @param response
     *          the response send by the server to the client
     * @throws ServletException
     *          if an error occurred
     * @throws IOException
     *          if an error occured
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);//------------------------------------------------------------95行
    }
    
    //产品搜索
    public void search_commodity(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
        try {
            //获取查询参数
            String search_name = request.getParameter("search_name");
            String cids = request.getParameter("cid");
            //获取”当前页“参数(当第一次访问当前页为null)
            String currPage = request.getParameter("currentPage");
            //判断
            if (currPage == null || "".equals(currPage.trim())){
                currPage = "1"; //第一次访问,设置当前页为1;
            }
            //转换
            int currentPage = Integer.parseInt(currPage);
            int cid = Integer.parseInt(cids);
            //创建PageBean对象,设置当前页参数
            PageBean pageBean = new PageBean();
            pageBean.setCurrentPage(currentPage);
            //调用dao对象,获取结果
            HomeDAO dao = new HomeDAO();
            Map<String, Object> result = dao.searchCommodity(pageBean, cid, search_name);//------------------144行
            //保存
            request.setAttribute("result", result);
            //跳转
            uri = "/Home/product_display.jsp";
        }catch (Exception e){
            e.printStackTrace();
        }
        //转发
        request.getRequestDispatcher(uri).forward(request, response);
    }
   
    /**
     * Initialization of the servlet.
     *
     * @throws ServletException
     *              if an error occurs
     */
    public void init() throws ServletException{
        //Put you code here
    }
}


  • 写回答

4条回答 默认 最新

  • CSDN专家-sinJack 2023-04-06 20:30
    关注

    主要报错信息发出来,具体报错所对应的行代码是哪一句。

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • 瞬间的未来式 2023-04-06 20:03
    关注

    整个项目打包看下

    评论
  • 有问必答小助手 2023-04-06 23:14
    关注
    您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
    PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632
    评论
  • qq_57931996 2023-04-07 17:01
    关注

    java.sql.SQLException: Operation not allowed for a result set of type ResultSet.TYPE_FORWARD_ONLY.
    解决办法:
    http://t.csdn.cn/LrX9c

    img

    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 4月7日
  • 已采纳回答 4月6日
  • 创建了问题 4月6日

悬赏问题

  • ¥20 求完整顺利登陆QQ邮箱的python代码
  • ¥15 基带到底是什么?为什么手机厂和厂外完全两个概念
  • ¥15 怎么下载MySQL,怎么卸干净原来的MySQL
  • ¥15 网络打印机Ip地址自动获取出现问题
  • ¥15 求局部放电案例库,用于预测局部放电类型
  • ¥100 QT Open62541
  • ¥15 stata合并季度数据和日度数据
  • ¥15 谁能提供rabbitmq,erlang,socat压缩包,记住版本要对应
  • ¥15 Vue3 中使用 `vue-router` 只能跳转到主页面?
  • ¥15 用QT,进行QGIS二次开发,如何在添加栅格图层时,将黑白的矢量图渲染成彩色