4.2

package dao;

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

import database.Database;
import util.Falv;

public class FalvDao {
    public List<Falv> ffind(){
        List<Falv> list=new ArrayList<>();
        Connection conn=null;
        ResultSet rs=null;
        PreparedStatement pstm=null;
        Falv falv=null;
        try {
            conn=Database.getConnection();
            String sql="select title,time,fenlei"+" from falv";
            pstm=conn.prepareStatement(sql);
            rs=pstm.executeQuery();
            while(rs.next()) {
                String title=rs.getString("title");
                String time=rs.getString("time");
                String fenlei=rs.getString("fenlei");
                falv=new Falv(title,time,fenlei);
                list.add(falv);
                
                System.out.println("title:"+title);
                System.out.println("time:"+time);
                
            }
        }catch(Exception e) {
            e.printStackTrace();
        }finally {
            Database.close(conn, pstm, rs);
        }
        return list;
    }
    

}
package servlet;

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.FalvDao;
import util.Falv;
@WebServlet("/falvservlet")
public class FalvServlet extends HttpServlet{
    
    
        private static final long serialVersionUID = 1L;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public FalvServlet() {
            super();
            // TODO Auto-generated constructor stub
        }

        FalvDao dao=new FalvDao();
        public void find(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            response.setCharacterEncoding("UTF-8");
            request.setCharacterEncoding("UTF-8");

            List<Falv> list=dao.ffind();
            request.setAttribute("list", list);
            
            System.out.println("查询成功");
            request.getRequestDispatcher("falv.jsp").forward(request,response);
            
        }
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            response.setCharacterEncoding("UTF-8");
            request.setCharacterEncoding("UTF-8");
            System.out.println("����Servlet");
            String method = request.getParameter("method");
              System.out.println(method);
               if("find".equals(method)) {
                  find(request,response);
              }
        }

        /**
         * @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 @ 2024-04-02 16:40  好(justice)……  阅读(4)  评论(0编辑  收藏  举报