5.18

package Dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import DBUtil.DBUtil;
import Javabean.*;

public class Dao {

    public boolean addrc(Reci reci)
    {
          Connection conn = DBUtil.getConn();
          PreparedStatement pstmt = null;
          boolean f = false;
          int a=0;
          try {
              String sql = "insert into cvpr(id,name,pdf,author,title,booktitle,date) value(?,?,?,?,?,?,?)";
              pstmt = conn.prepareStatement(sql);
              pstmt.setInt(1, reci.getId());
              pstmt.setString(2, reci.getName());
              pstmt.setString(3, reci.getPdf());
              pstmt.setString(4, reci.getAuthor());
              pstmt.setString(5, reci.getTitle());
              pstmt.setString(6, reci.getBooktitle());
              pstmt.setString(7, reci.getDate());
             a = pstmt.executeUpdate();
          }
          catch(SQLException e) {
              e.printStackTrace();
          }
          finally {
              DBUtil.close(pstmt, conn); 
          }
          if(a>0)
          f=true;
          
          return f;
    }
    
     public List<Reci> select(String minzi) {
         List<Reci> list=new ArrayList<Reci>(); 
            Connection con;
            try {
                String sql="select * from cvpr where name = ?";
                con = DBUtil.getConn();
                PreparedStatement pa=con.prepareStatement(sql);
                pa.setString(1,minzi );
                ResultSet rs=pa.executeQuery();
                while(rs.next()) {
                     Reci L = new Reci();
                     int id = rs.getInt("id");
                     String name= rs.getString("name");
                     String pdf = rs.getString("pdf");
                     String author = rs.getString("author");
                     String title = rs.getString("title");
                     String booktitle = rs.getString("booktitle");
                     String date = rs.getString("date");
                    L=new Reci(id,name,pdf,author,title,booktitle,date);
                    list.add(L);
                    
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return list;
        }
     
     public List<Reci> select1(String zz) {
         List<Reci> list=new ArrayList<Reci>(); 
            Connection con;
            try {
                String sql="select * from cvpr where author = ?";
                con = DBUtil.getConn();
                PreparedStatement pa=con.prepareStatement(sql);
                pa.setString(1,zz );
                ResultSet rs=pa.executeQuery();
                while(rs.next()) {
                     Reci L = new Reci();
                     int id = rs.getInt("id");
                     String name= rs.getString("name");
                     String pdf = rs.getString("pdf");
                     String author = rs.getString("author");
                     String title = rs.getString("title");
                     String booktitle = rs.getString("booktitle");
                     String date = rs.getString("date");
                    L=new Reci(id,name,pdf,author,title,booktitle,date);
                    list.add(L);
                    
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return list;
        }
     
    
     
     public boolean update(Reci reci) {
            String sql = "update cvpr set name='" + reci.getName() + "', pdf='" + reci.getPdf()
            + "', author='" +reci.getAuthor() + "',title='" +reci.getTitle()+"',booktitle='" +reci.getBooktitle()+"',date='"+reci.getDate() + "'where id='" + reci.getId() + "'";
        Connection conn = DBUtil.getConn();
        Statement state = null;
        boolean f = false;
        int a = 0;
        try {
            state = conn.createStatement();
            System.out.println("看看是不是执行了");
            a = state.executeUpdate(sql);
            System.out.println(a);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(state, conn);
        }
        
        if (a > 0) {
            f = true;
        }
        System.out.println(f);
        return f;
        }
     
     public static boolean delete(String name) {
            String sql = "DELETE from cvpr where name='" + name + "'";
            Connection conn = DBUtil.getConn();
            Statement state = null;
            boolean f = false;
            int a = 0;
            try {
                state = conn.createStatement();
                System.out.println("看看是不是执行了");
                a = state.executeUpdate(sql);
                System.out.println(a);
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                DBUtil.close(state, conn);
            }

            if (a > 0) {
                f = true;
            }

            System.out.println(f);
            return f;
        }
     

}
package Javabean;

public class Reci {
    private int id;
    private String name;
    private String pdf;
    private String author;
    private String title;
    private String booktitle;
    private String date;
    
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPdf() {
        return pdf;
    }
    public void setPdf(String pdf) {
        this.pdf = pdf;
    }
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getBooktitle() {
        return booktitle;
    }
    public void setBooktitle(String booktitle) {
        this.booktitle = booktitle;
    }
    public String getDate() {
        return date;
    }
    public void setDate(String date) {
        this.date = date;
    }
       
    public Reci() {}
    public Reci(int id,String name,String pdf,String author,String title,String booktitle,String date) {
        this.id=id;
        this.name=name;
        this.pdf=pdf;
        this.author=author;
        this.title=title;
        this.booktitle=booktitle;
        this.date=date;
    }
    public Reci(String name,String pdf,String author,String title,String booktitle,String date) {
         this.name=name;
         this.pdf=pdf;
         this.author=author;
         this.title=title;
         this.booktitle=booktitle;
         this.date=date;
    }
    public Reci(int id,String pdf,String author,String title,String booktitle,String date) {
        this.id=id;
        this.pdf=pdf;
        this.author=author;
        this.title=title;
        this.booktitle=booktitle;
        this.date=date;
   }
}
package reciServlet;


import java.io.IOException;
import java.util.List;

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.Dao;
import Javabean.*;

/**
 * Servlet implementation class reciServlet
 */
@WebServlet("/reciServlet")
public class reciServlet extends HttpServlet {
     private static final long serialVersionUID = 1L;
    // private DBUtil 
        Dao dao = new Dao();
     public reciServlet() {
            super();
            // TODO Auto-generated constructor stub
        }
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        private void addrc(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            req.setCharacterEncoding("utf-8");
            int id = Integer.parseInt(req.getParameter("id"));
            String name = req.getParameter("name");
            String pdf = req.getParameter("pdf");
            String author = req.getParameter("author");
            String title = req.getParameter("title");
            String booktitle = req.getParameter("booktitle");
            String date = req.getParameter("date");
            Reci reci=new Reci(id,name,pdf,author,title,booktitle,date);
            if(dao.addrc(reci)) {
                req.setAttribute("reci",reci);
                req.setAttribute("message","添加成功" );
                req.getRequestDispatcher("add.jsp").forward(req, resp);
            }else {
                req.setAttribute("message","论文名重复,请重新输入" );
                req.getRequestDispatcher("add.jsp").forward(req, resp);
            }
        }
        
        private void show(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            
            String name=request.getParameter("name");
            
            List<Reci> list=dao.select(request.getParameter("minzi"));
            
            request.setAttribute("list",list);
            request.getRequestDispatcher("show.jsp").forward(request, response);
        }
        
 private void showid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            
            
     String author=request.getParameter("author");
     
     List<Reci> list=dao.select1(request.getParameter("zz"));
     
     request.setAttribute("list",list);
     request.getRequestDispatcher("showid.jsp").forward(request, response);
        }

        private void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            req.setCharacterEncoding("utf-8");
            String name = req.getParameter("name");
            //final boolean b = dao.delete(name);
            if (dao.delete(name)) {
                req.setAttribute("message", "删除成功");
                req.getRequestDispatcher("delete.jsp").forward(req, resp);
            } else {
                req.setAttribute("message", "删除失败");
                req.getRequestDispatcher("delete.jsp").forward(req, resp);
            }
        }
        
       
        
        private void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            req.setCharacterEncoding("utf-8");
            int id = Integer.parseInt(req.getParameter("id"));
            String name = req.getParameter("name");
            String pdf = req.getParameter("pdf");
            String author = req.getParameter("author");
            String title = req.getParameter("title");
            String booktitle = req.getParameter("booktitle");
            String date = req.getParameter("date");
            Reci reci=new Reci(id,name,pdf,author,title,booktitle,date);
            if(dao.update(reci)) {
                
                req.setAttribute("message","修改成功" );
                req.getRequestDispatcher("update.jsp").forward(req, resp);    
            } else {
                req.setAttribute("message","修改失败" );
                req.getRequestDispatcher("update.jsp").forward(req, resp);
            }
        }
        
        
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
             request.setCharacterEncoding("utf-8");
                String method = request.getParameter("method");
                if("addrc".equals(method)) {
                    addrc(request,response);
                }
                else if("show".equals(method)) {
                    show(request,response);}
                    else if("showid".equals(method)) {
                        showid(request,response);
                    }
                else if("update".equals(method)) {
                    update(request,response);
                }
                else if("delete".equals(method)) {
                    delete(request,response);
                }
        }
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            request.setCharacterEncoding("utf-8");
            doGet(request, response);
            }
        
        
       
       
        


}
package DBUtil;

import java.sql.*;

/**
 * 数据库连接工具
 * @author Hu
 *
 */
public class DBUtil {
    
    public static String url =  "jdbc:mysql://localhost:3306/exercise?serverTimezone=UTC";
    public static String user = "root";
    public static String password = "";
    
    public static Connection getConn () {
        Connection conn = null;
        
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");//加载驱动
            conn = DriverManager.getConnection(url, user, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return conn;
    }
    
    /**
     * 关闭连接
     * @param state
     * @param conn
     */
    public static void close (PreparedStatement preparedState, Connection conn) {
        if (preparedState != null) {
            try {
                preparedState.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    
    public static void close (ResultSet rs, PreparedStatement preparedState, Connection conn) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        
        if (preparedState != null) {
            try {
                preparedState.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 关闭连接
     * @param state
     * @param conn
     */
    public static void close (Statement state, Connection conn) {
        if (state != null) {
            try {
                state.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    
    public static void close (ResultSet rs, Statement state, Connection conn) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        
        if (state != null) {
            try {
                state.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    
    public static void main(String[] args) throws SQLException {
        Connection conn = getConn();
        PreparedStatement preparedStatement = null;
        ResultSet rs = null;
        String sql ="select * from db3";
        preparedStatement = conn.prepareStatement(sql);
        rs = preparedStatement.executeQuery();
        if(rs.next()){
            System.out.println("数据库为空");
        }
        else{
            System.out.println("数据库不为空");
        }
    }
}

 

实现对爬取的内容进行增删改查,使用到了layui模板用于样式美化:

 

posted @ 2022-06-14 22:43  _Pi  阅读(93)  评论(0)    收藏  举报