package hhh; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import hhh.Bean; @WebServlet("/Servlet") public class Servlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); String name = request.getParameter("name"); String document = request.getParameter("document"); String organ = request.getParameter("organ"); String text = request.getParameter("text"); try { Bean bean = new Bean(name, document, organ, text); ArrayList<finaBean> arr = Dao.find(bean); request.setAttribute("routeMain", arr); request.getRequestDispatcher("/result.jsp").forward(request, response); } catch (Exception e) { e.printStackTrace(); } finally { } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }
package hhh; import java.sql.DriverManager; import java.sql.SQLException; import java.util.ArrayList; import java.sql.Connection; import java.sql.Date; import java.sql.PreparedStatement; import java.sql.ResultSet; public class Dao extends Bean { private static final String URL = "jdbc:mysql://localhost:3306/www"; private static final String USERNAME = "root"; private static final String PWD = "1234"; public static ArrayList<finaBean> find(Bean bean) { ArrayList<finaBean> resultArray = new ArrayList<>(); Connection connection = null; PreparedStatement pstmt = null; ResultSet rs = null; try { // 1���������������ؾ��������� Class.forName("com.mysql.cj.jdbc.Driver"); // 2�������ݿ⽨������ connection = DriverManager.getConnection(URL, USERNAME, PWD); // 3������sql��ִ���� String sql = "select * from policy where name like ? and document like ? and organ like ? and text like ?"; pstmt = connection.prepareStatement(sql);// Ԥ���� pstmt.setString(1, "%"+bean.getS()+"%"); pstmt.setString(2, "%"+bean.getD()+"%"); pstmt.setString(3, "%"+bean.getE()+"%"); pstmt.setString(4, "%"+bean.getA()+"%"); rs = pstmt.executeQuery(); while (rs.next()) { finaBean finaBean = new finaBean(); finaBean.s = (String) rs.getObject("name"); finaBean.d = (String) rs.getObject("organ"); finaBean.e = (String) rs.getString("pubdata"); finaBean.a = (String) rs.getObject("type"); finaBean.q= (String) rs.getObject("text"); resultArray.add(finaBean); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (pstmt != null) pstmt.close(); if (connection != null) connection.close(); } catch (SQLException e) { e.printStackTrace(); } } return resultArray; } }
标签:总结,java,14,request,import,finaBean,pstmt,String From: https://www.cnblogs.com/liucaizhi/p/17316799.html