jsp第九次作业

BaseDao.java

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", "root");
            } 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();
        }
    }

}

bookmanageDao.java

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;
    }
}

bookmanage.java

package entity;

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;
    }
    
}

index.jsp

<%@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" >
      <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>

insert.jsp

<%@ 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 'insert.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>
  <form action="doinsert.jsp">
   <h2>新增图书信息</h2>
   图书名称:<input type="text" name = "b_name">
   </br>
   图书作者:<input type="text" name = "b_author">
   </br>
   购买日期<input type="text" name = "b_time">yyyy-Mm-dd格式
      </br>
     图书类别:<select name="b_type">
         
          <option value="0">请选择所属分类</option>
          <option value="1">计算机/软件</option>
          <option value="2">小说/文摘</option>
          <option value="3">杂项</option>
     </select>
     </br>
     <input type="submit" value="增加图书">
     </form>
  </body>
</html>

doinsetServlet.java

package servlet;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.bookmanageDao;
import entity.bookmanage;

/**
 * Servlet implementation class doinsetServlet
 */
@WebServlet("/doinsetServlet")
public class doinsetServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public doinsetServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        int b_type = Integer.parseInt(request.getParameter("b_type"));
        String b_name = request.getParameter("b_name");
        String b_author= request.getParameter("b_author");
        String b_time = request.getParameter("b_time");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date time = null;;
        try {
            time = sdf.parse(b_time);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        bookmanage b = new bookmanage();
        b.setB_type(b_type);
        b.setB_author(b_author);
        b.setB_name(b_name);
        b.setB_time(new java.sql.Date(time.getTime()));
        bookmanageDao bd = new bookmanageDao();
        bd.insert(b); 
              response.sendRedirect("index.jsp");
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

 

deleteServlet.java

package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.bookmanageDao;

/**
 * Servlet implementation class deleteServlet
 */
@WebServlet("/deleteServlet")
public class deleteServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public deleteServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        int b_id = Integer.parseInt(request.getParameter("b_id"));
          bookmanageDao bd = new bookmanageDao();
          bd.delete(b_id); 
          response.sendRedirect("index.jsp");
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

 

 

posted @ 2021-05-11 15:57  ~~_~~  阅读(48)  评论(0编辑  收藏  举报