欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-05-19 17:08:20
...

在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
	 * @邮箱:aaa@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
	 * @邮箱:aaa@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驱动!!!

  • 最终显示结果

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

相关标签: jsp jdbc java