ResultSet 基础没学好

作业要求写一个图书模糊查询,一个页面提交到Servlet处理链接数据库模糊查找后返回信息,咋看好简单;

但是写好后运行,输入后没有结果显示,但是输入某个字符又会显示一条记录;如下数据库,输入‘学’ 只能显示其中一条‘我的大学’

后来研究许久发现是在得到ResultSet 后多使用了一次造成指针错过第一条记录

论学好基础的重要性

book_idbook_namebook_price
10000悲惨世界11
10001世界尽头与冷酷仙境11
10002三体11
10003我的大学11
10004高等数学11
10005黑客与画家11
10006松本行弘的程序世界11

这是JSP

<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>图书模糊查找</title>
</head>
		<%
			ArrayList name = (ArrayList)session.getAttribute("name");
			ArrayList id = (ArrayList)session.getAttribute("id");
			ArrayList pri = (ArrayList)session.getAttribute("pri");
			if(name == null){
				name = new ArrayList();
				id = new ArrayList();
				pri = new ArrayList();
			}
			%>
<body>
	<center>
	<form name="form" method="post" action="servlets/FindBook">
	请输入图书信息: <input name="info" type="text"><input type="submit" value="查找"><HR>
	<table width=600px border="1" >
		<tr >
			<td height=30px; width=200px>ID</td>
			<td height=30px; width=200px>书名</td>
			<td height=30px; width=200px>价格</td>
		</tr>
		<%
		if(id.size()>0){
			for(int i=0; i<id.size(); i++){
				out.print("<tr>");
					out.print("<td>"+id.get(i)+"</td>");
					out.print("<td>"+name.get(i)+"</td>");
					out.print("<td>"+pri.get(i)+"</td>");
				out.print("</tr>");
			}
		}else{
			out.print("<tr>");
			out.print("<td></td>");
			out.print("<td></td>");
			out.print("<td></td>");
			out.print("</tr>");
		}
			%>
	</table>
	</form>
	</center>
</body>
</html>

这是Servlet

package servlets;


import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.mysql.jdbc.Connection;

/**
 * Servlet implementation class FindBook
 */
public class FindBook extends HttpServlet {
	private static final long serialVersionUID = 1L;
    public FindBook() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) 
	throws ServletException, IOException{
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) 
	throws ServletException, IOException{
		String info = new String(request.getParameter("info"));
		
		try {
			
			FindBook(info, request, response);
			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}	
	}

	private void FindBook(String info, HttpServletRequest request, HttpServletResponse response) 
	throws ClassNotFoundException, SQLException, ServletException, IOException{
		// TODO Auto-generated method stub
		
		HttpSession session = request.getSession();
		
		ArrayList name = new ArrayList();
		ArrayList id = new ArrayList();
		ArrayList pri = new ArrayList();
				//链接数据库
		Class.forName("com.mysql.jdbc.Driver");
		Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase","root", "");
		String sql = "select * from t_book where book_name like '%"+info+"%'";
		Statement psta = con.createStatement();
		ResultSet rs = psta.executeQuery(sql);
		
				if(rs != null){	
					//如果用if(rs.next()) 结果集将会导致rs指针指向第一条记录 后续的while判断 无法获取rs中的第一条内容
					while(rs.next()){
						String book_id = rs.getString("BOOK_ID");
						String book_name = rs.getString("BOOK_NAME");
						String book_price = rs.getString("BOOK_PRICE");
						name.add(book_name);
						id.add(book_id);
						pri.add(book_price);
					}
					session.setAttribute("name", name);
					session.setAttribute("id", id);
					session.setAttribute("pri", pri);
					response.sendRedirect("/Servlet/Find_book.jsp");
				}else{
					response.sendRedirect("/Servlet/Find_book.jsp");
				}
				rs.close();
				psta.close();
				con.close();
	}

	}


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值