<!--<%@page import="sun.awt.SunHints.Value"%>--> <%@ page language="java" 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"> <%@page import="java.util.List"%> <%@page import="example.bean.book.Book"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>所有图书信息</title> <style type="text/css"> td { font-size: 12px; } h2 { margin: 0px } </style> <script type="text/javascript"> function check(form) { with (form) { if (bookCount.value == "") { alert("请输入更新数量!"); return false; } if (isNaN(bookCount.value)) { alert("格式错误!"); return false; } return true; } } </script> </head> <body> <table bordercolor="white" bgcolor="black" cellpadding="1" cellspacing="1"> <tr bgcolor="white"> <td colspan="7"> <h2>所有图书信息</h2> </td> </tr> <tr bgcolor="#e1ffc1"> <td><b>ID</b></td> <td><b>图书名称</b></td> <td><b>价格</b></td> <td><b>数量</b></td> <td><b>作者</b></td> <td><b>修改</b></td> <td><b>删除</b></td> </tr> <% // 获取图书信息集合 List<Book> list = (List<Book>) request.getAttribute("list"); // 判断集合是否有效 if (list == null || list.size() < 1) { out.print("没有数据!"); } else { // 遍历图书集合中的数据 for (Book book : list) { %> <tr bgcolor="white"> <td><%=book.getId()%></td> <td><%=book.getName()%></td> <td><%=book.getPrice()%></td> <td><%=book.getBookCount()%></td> <td><%=book.getAuthor()%></td> <td > <form action="UpdateServlet" method="post" onsubmit="return check(this);"> <input type="hidden" value="<%=book.getId()%>"> <input type="text" size="3"> <input type="submit" value="修改数量"> </form> </td> <td> <a href="DeleteServlet?id=<%=book.getId()%>">删除</a> </td> </tr> <% } } %> </table> <h2> <a href="https://www.jb51.net/index.jsp">返回添加图书信息页面</a> </h2> </body> </html>
6、jdbc简单的封装
1)、ConnectionFactory.java工厂类
package example.dao.book; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class ConnectionFactory { private String driverClassName = "com.mysql.jdbc.Driver"; private String url = "jdbc:mysql://localhost:3306/db_book?useUnicode=true&characterEncoding=utf-8"; private String userName = "root"; private String password = ""; private static ConnectionFactory connectionFactory=null; private ConnectionFactory() { try { Class.forName(driverClassName); } catch (ClassNotFoundException e) { e.printStackTrace(); } } public Connection getConnection() throws SQLException { return DriverManager.getConnection(url, userName, password); } public static ConnectionFactory getInstance() { if (null==connectionFactory) { connectionFactory=new ConnectionFactory(); } return connectionFactory; } }
2)、BookJdbcDao.java数据库操作封装类
package example.dao.book; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import example.bean.book.Book; public class BookJdbcDao { private PreparedStatement ptmt = null; private ResultSet rs = null; public BookJdbcDao() { } public void findAll(Connection conn) throws SQLException { //to do } public void delete(Connection conn, int id) throws SQLException { String sql = "delete from tb_books where id=?"; try{ ptmt = conn.prepareStatement(sql); // 对SQL语句中的第一个占位符赋值 ptmt.setInt(1, id); // 执行更新操作 ptmt.executeUpdate(); }finally{ if (null!=ptmt) { ptmt.close(); } if (null!=conn) { conn.close(); } } } public void update(Connection conn, int id ,int bookcount) throws SQLException { //to do } }