在数据库中创建表T_BOOK,包含图书ID,图书名称,图书价格。
模糊查询图书,在图书的显示代码中使用JSTL,如果图书的价格在50元以上,则以黄色字体显示书名。
main.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@page import ="java.lang.String"%> <html> <head><title>查询页面</title></head> <body> <center><form action="/jstl/query"> 书名:(模糊查询)<input type="text" name="name"></br> <input type="submit" value="查询"> </form></center> </body> </html>
book.jsp:
<%@ page pageEncoding="utf-8" import="java.util.*,beans.book"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <html> <body> <center> <table border="1" width="300"> <tr><th>书名</th><th>作者</th><th>价格</th></tr> <c:forEach items="${result}" var="books" > <tr> <td><c:out value="${books.bid}" default="null"/><</td> <td><c:out value="${books.bname}" default="null"/></td> <c:choose > <c:when test="${books.price>=50}"> <td><font color="yellow"><c:out value="${books.price}" default="null"/></font></td> </c:when> <c:otherwise> <td><c:out value="${books.price}" default="null"/></td> </c:otherwise> </c:choose> </tr> </c:forEach> </table> </center> </body> </html>
setBook.java:
package servelt; import java.io.IOException; 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 beans.book; import Dao.BookDao; import javax.servlet.annotation.WebServlet; @WebServlet("/jstl/query") public class setBook extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); List<book> bookList=new ArrayList<book>(); String Bname= request.getParameter("name"); BookDao Bookdao=new BookDao(); try{bookList=Bookdao.findBookByName(Bname);}catch (Exception e){System.out.print("11"); } request.setAttribute("result", bookList);//将查询结果保存到request对象中 //map.addAttribute("result",bookList); //转发到show.jsp显示查询结果,show.jsp位于/jstl/目录下 request.getRequestDispatcher("book.jsp").forward(request, response); } }
book.java:
package beans;
public class book {
private String bname;
private int bid;
private String bauthor;
private float price;
private String binfo;
/*public book(int bid,String name,String author,Float price,String binfo)
{
this.bid=bid;
this.bname=name;
this.bauthor=author;
this.price=price;
this.binfo=binfo;
}*/
public void setBname(String bname) { this.bname=bname; }
public void setBid(int bid) { this.bid=bid; }
public void setBauthor(String bauthor) { this.bauthor=bauthor; }
public void setPrice(float price) { this.price=price; }
public void setBinfo(String info) {
this.binfo=info;
}
public int getBid(){return this.bid;}
public String getBname(){return this.bname;}
public String getBauthor(){return this.bauthor;}
public float getPrice(){return this.price;}
public String getBinfo(){return this.binfo;}
}
BookDao.java:
package Dao;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import beans.book;
import jdbc.JDBCUtil;
public class BookDao {
//向数据库中添加用户记录的方法add()
public void add(book b) throws Exception{
Connection conn = null;
PreparedStatement ps = null;
try {
conn = JDBCUtil.getConnection();
String sql = "insert into books values (?,?,?,?,?)";
ps = conn.prepareStatement(sql);
ps.setInt(1, b.getBid());
ps.setString(2,b.getBname());
ps.setString(3,b.getBauthor());
ps.setFloat(4,b.getPrice());
ps.setString(5,b.getBinfo());
ps.executeUpdate();
}finally {JDBCUtil.free(null,ps, conn);}
}
//修改数据库用户记录的方法update()
public void update(book b) throws Exception{
Connection conn = null;
PreparedStatement ps = null;
try {
conn = JDBCUtil.getConnection();
String sql = "update books set bname=?, bauthor=?, price=?, Binfo=? where bid=? ";
ps = conn.prepareStatement(sql);
ps.setString(1,b.getBname());
ps.setString(2,b.getBauthor());
ps.setFloat(3,b.getPrice());
ps.setString(4,b.getBinfo());
ps.setInt(5, b.getBid());
ps.executeUpdate();
}finally {JDBCUtil.free(null,ps, conn);}
}
//删除数据库用户记录的方法delete()
public void delete(int id) throws Exception{
Connection conn = null;
PreparedStatement ps = null;
try {
conn = JDBCUtil.getConnection();
String sql = "delete from books where bid=?";
ps = conn.prepareStatement(sql);
ps.setInt(1,id);
ps.executeUpdate();
}finally {JDBCUtil.free( null,ps, conn);}
}
public List<book> findBookByName(String name) throws Exception{
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
List<book> bookList=new ArrayList<book>();
try {
conn = JDBCUtil.getConnection();
String sql = "select * from books where bname like ? ";
ps = conn.prepareStatement(sql);
ps.setString(1, "%" + name + "%");
rs=ps.executeQuery();
if(rs.next()){
book b=new book();
b.setBid(rs.getInt(1));
b.setBname(rs.getString(2));
b.setBauthor(rs.getString(3));
b.setPrice(rs.getFloat(4));
b.setBinfo(rs.getString(5));
bookList.add(b);
}
}finally {JDBCUtil.free(rs, ps, conn);}
return bookList;
}
//根据id查询用户的方法findUserById()
public book findUserById(int id) throws Exception{
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
book b=null;
try {
conn = JDBCUtil.getConnection();
String sql = "select * from books where bid=? ";
ps = conn.prepareStatement(sql);
ps.setInt(1, id);
rs=ps.executeQuery();
if(rs.next()){
b=new book();
b.setBid(rs.getInt(1));
b.setBname(rs.getString(2));
b.setBauthor(rs.getString(3));
b.setPrice(rs.getFloat(4));
b.setBinfo(rs.getString(5));
}
}finally {JDBCUtil.free(rs, ps, conn);}
return b;
}
//查询全部用户的方法queryAll()
public List<book> queryAll() throws Exception{
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
List<book> bookList=new ArrayList<book>();
try {
conn = JDBCUtil.getConnection();
String sql = "select * from books ";
ps=conn.prepareStatement(sql,
ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=ps.executeQuery();
while(rs.next()){
book b=new book();
b.setBid(rs.getInt(1));
b.setBname(rs.getString(2));
b.setBauthor(rs.getString(3));
b.setPrice(rs.getFloat(4));
b.setBinfo(rs.getString(5));
bookList.add(b);
}
}finally {JDBCUtil.free(rs, ps, conn);}
return bookList;
}
数据库结构: