论文查询系统(三)
DBUtil
package DBUtil; import java.sql.*; public class DBUtil { static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; //数据库驱动名 static final String url = "jdbc:mysql://localhost:3306/python?serverTimezone=UTC";//数据库地址 static final String user = "root"; static final String password = "152486"; //连接数据库 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; } //关闭连接 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 pachong";//数据库名称 preparedStatement = conn.prepareStatement(sql); rs = preparedStatement.executeQuery(); if(rs.next()){ System.out.println("数据库不为空"); } else{ System.out.println("数据库为空"); } } }
servlet
package Servlet; import java.io.IOException; import java.io.UnsupportedEncodingException; 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 Bean.bean; import Dao.Dao; /** * Servlet implementation class servlet */ @WebServlet("/servlet") public class servlet extends HttpServlet { Dao Dao=new Dao(); private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public servlet() { super(); // TODO Auto-generated constructor stub } private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("utf-8"); int id = Integer.parseInt(request.getParameter("id")); String name = request.getParameter("name"); String pdf = request.getParameter("pdf"); String author = request.getParameter("author"); String title = request.getParameter("title"); String booktitle = request.getParameter("booktitle"); String date = request.getParameter("date"); bean bean=new bean(id,name,pdf,author,title,booktitle,date); Dao.update(bean); request.setAttribute("message", "修改成功"); request.getRequestDispatcher("servlet?method=list").forward(request, response); } private void delete(HttpServletRequest request, HttpServletResponse response) throws Exception, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("UTF-8"); int id=Integer.parseInt(request.getParameter("id")); Dao.delete(id); //进行数据库的删除操作 request.setAttribute("message", "删除成功"); request.getRequestDispatcher("servlet?method=list").forward(request, response); } private void insert(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { // TODO Auto-generated method stub request.setCharacterEncoding("utf-8"); int id = Integer.parseInt(request.getParameter("id")); String name = request.getParameter("name"); String pdf = request.getParameter("pdf"); String author = request.getParameter("author"); String title = request.getParameter("title"); String booktitle = request.getParameter("booktitle"); String date = request.getParameter("date"); bean bean=new bean(id,name,pdf,author,title,booktitle,date); if(Dao.insert(bean)) { request.setAttribute("message", "添加成功"); request.getRequestDispatcher("index.jsp").forward(request, response); } } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ 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("insert".equals(method)) { insert(request,response); } else if("delete".equals(method)) { try { delete(request,response); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } else if("update".equals(method)) { update(request,response); } /*else if("list".equals(method)) { try { list(request,response); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }*/ } /** * @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); } }