package dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import entity.bookmanage; public class bookmanageDao extends BaseDao{ public List<bookmanage> selectall() { ArrayList<bookmanage> list = new ArrayList<bookmanage>(); Connection conn = null; PreparedStatement ps =null; ResultSet rs = null; try { conn = getConnection(); String sql = "select * from bookmanage"; ps = conn.prepareStatement(sql); rs = ps.executeQuery(); while(rs.next()){ list.add(new bookmanage(rs.getInt("b_id"),rs.getString("b_name"), rs.getString("b_author"), rs.getDate("b_time"),rs.getInt("b_type"))); } return list; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ closeAll(conn, ps, rs); } return list; } public int insert(bookmanage b){ Connection conn=getConnection(); String sql="insert into bookmanage (b_name,b_author,b_time,b_type)values(?,?,?,?)"; PreparedStatement ps=null; int i=0; try { ps=conn.prepareStatement(sql); ps.setString(1,b.getB_name()); ps.setString(2, b.getB_author()); ps.setDate(3, b.getB_time()); ps.setInt(4, b.getB_type()); i=ps.executeUpdate();//返回受影响的行数 } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ closeAll(conn, ps, null); } return i; } public int delete(int b_id){ Connection conn=getConnection(); String sql="delete from bookmanage where b_id = ?"; PreparedStatement ps=null; int i=0; try { ps=conn.prepareStatement(sql); ps.setInt(1, b_id); i=ps.executeUpdate();//返回受影响的行数 } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ closeAll(conn, ps, null); } return i; } }
package dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class BaseDao { //获取连接 protected Connection getConnection(){ Connection conn=null; try { Class.forName("com.mysql.jdbc.Driver"); // 2.建立连接 conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/test", "root", "123456"); } catch (Exception e) { e.printStackTrace(); } return conn; } //关闭连接 protected void closeAll(Connection con,PreparedStatement ps,ResultSet rs){ try { if(rs != null) rs.close(); if(ps != null) ps.close(); if(con != null) con.close(); } catch (SQLException e) { e.printStackTrace(); } } }
import java.sql.Date; public class bookmanage { private int b_id; private String b_name; private String b_author; private Date b_time; private int b_type; public bookmanage() { super(); } public bookmanage(int b_id,String b_name, String b_author, Date b_time, int b_type) { super(); this.b_id=b_id; this.b_name = b_name; this.b_author = b_author; this.b_time = b_time; this.b_type = b_type; } public int getB_id() { return b_id; } public void setB_id(int 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 Date getB_time() { return b_time; } public void setB_time(Date b_time) { this.b_time = b_time; } public int getB_type() { return b_type; } public void setB_type(int b_type) { this.b_type = b_type; } }
<%@page import="entity.bookmanage"%> <%@page import="dao.bookmanageDao"%> <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> </div><h1 >图书信息</h1> <table cellspacing="0" border="1px" width="549" height="66" style="margin: 0pt auto;"> <tr> <th>图书名称</th> <th>图书作者</th> <th>购买时间</th> <th>图书分类</th> <th>操作</th> </tr> <% bookmanageDao bd = new bookmanageDao(); List<bookmanage> list = bd.selectall(); for(int i = 0 ;i <list.size();i++){ out.print("<tr>"); out.print("<td>"+list.get(i).getB_name()+"</td>"); out.print("<td>"+list.get(i).getB_author()+"</td>"); out.print("<td>"+list.get(i).getB_time()+"</td>"); out.print("<td>"+list.get(i).getB_type()+"</td>"); %> <td><a href="delete.jsp?b_id=<%=list.get(i).getB_id()%>">删除</a></td> <% out.print("</tr>"); } %> </table> <h2><a href="insert.jsp">新增图书信息</a></h2> </body> </html>