您的位置:首页 > 其它

ResultSet 基础没学好

2015-12-09 11:35 190 查看
作业要求写一个图书模糊查询,一个页面提交到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();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: