在jsp页面上显示图书列表信息

在jsp页面上显示图书列表信息

想要在JSP页面上显示图书的各种详细信息,首先要在数据库中将图书信息查询出来,然后将其显示到JSP页面上。

  • 1 创建工具类,连接数据库

public class DbUtil {
	
	private static String jdbcDriver = "com.mysql.jdbc.Driver";
	private static String jdbcUrl = "jdbc:mysql://localhost:3306/db_school";
	private static String userName = "root";
	private static String password = "123456";
	
	/**
	 * @方法名: getConn
	 * @方法说明: 创建连接
	 * @作者: LiYuHui
	 * @邮箱:1327711913@qq.com
	 * @日期: 2020年6月8日下午4:02:06
	 * @return
	 * @return: Connection
	 */
	public static Connection getConn(){
		Connection conn = null;
		try {
			Class.forName(jdbcDriver);
			conn = DriverManager.getConnection(jdbcUrl, userName, password);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}
	
	/**
	 * @方法名: closed
	 * @方法说明: 释放资源
	 * @作者: LiYuHui
	 * @邮箱:1327711913@qq.com
	 * @日期: 2020年6月8日下午4:15:08
	 * @param ps
	 * @param conn
	 * @return: void
	 */
	public static void closed(PreparedStatement ps,Connection conn){
			try {
				if(ps!=null){
					ps.close();
					if(conn!=null){
						conn.close();
					}
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
	}
}
  • 2 创建图书的实体(和表中字段一一对应)

public class BookInfo {
	
	private Integer b_id;
	private String b_name;
	private String b_author;
	private double b_price;
	private String b_content;
	public Integer getB_id() {
		return b_id;
	}
	public void setB_id(Integer b_id) {
		this.b_id = b_id;
	}
	public String getB_name() {
		return b_name;
	}
	public void setB_name(String b_name) {
		this.b_name = b_name;
	}
	public String getB_author() {
		return b_author;
	}
	public void setB_author(String b_author) {
		this.b_author = b_author;
	}
	public double getB_price() {
		return b_price;
	}
	public void setB_price(double b_price) {
		this.b_price = b_price;
	}
	public String getB_content() {
		return b_content;
	}
	public void setB_content(String b_content) {
		this.b_content = b_content;
	}
	@Override
	public String toString() {
		return "BookInfo [b_id=" + b_id + ", b_name=" + b_name + ", b_author=" + b_author + ", b_price=" + b_price
				+ ", b_content=" + b_content + "]";
	}
	
}
  • 3 对数据库进行操作(这里我没有使用接口)

public class BookInfoDao {
	
	public List<BookInfo> findAllBookInfo(){
		String sql = "select * from t_bookInfo";
		Connection conn = DbUtil.getConn();
		PreparedStatement ps = null;
		ResultSet rs = null;
		List<BookInfo> bookList = new ArrayList<BookInfo>();;
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while(rs.next()){
				BookInfo book = new BookInfo();
				book.setB_id(rs.getInt("b_id"));
				book.setB_name(rs.getString("b_name"));
				book.setB_author(rs.getString("b_author"));
				book.setB_price(rs.getDouble("b_price"));
				book.setB_content(rs.getString("b_content"));
				bookList.add(book);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DbUtil.closed(ps, conn);
		}
		return bookList;
	}
}
  • 在JSP页面接收查询到的数据

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="com.lyh.dao.BookInfoDao,com.lyh.model.BookInfo,java.util.*" %>
<!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>
	<body>
		<%
			BookInfoDao bookInfoDao = new BookInfoDao();
			List<BookInfo> bookList = bookInfoDao.findAllBookInfo();
			/* Iterator it = bookList.iterator();
			while(it.hasNext()){
				BookInfo book = it.next();
			} */
		%>
		<table border="1" collspacing="0" collpadding="0" align="center">
			<tr>
				<th colspan="6">书籍信息</th>
			</tr>
			<tr>
				<th>编号</th>
				<th>书名</th>
				<th>作者</th>
				<th>价格</th>
				<th>内容</th>
				<th>操作</th>
			</tr>
			<%
				for(int i=0;i<bookList.size();i++){
			%>
			<tr align="center">
				<td><% out.print(bookList.get(i).getB_id()); %></td>
				<td><% out.print(bookList.get(i).getB_name()); %></td>
				<td><% out.print(bookList.get(i).getB_author()); %></td>
				<td><% out.print(bookList.get(i).getB_price()); %></td>
				<td><% out.print(bookList.get(i).getB_content()); %></td>
				<td>
					<button>修改</button>
					<button>删除</button>
				</td>
			</tr>
			<%	} %>
		</table>
	</body>
</html>

注意:要有数据库,项目里要有sql驱动!!!

  • 最终显示结果

在这里插入图片描述

  • 4
    点赞
  • 61
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值