开学小测总结(下)

接下来就是servlet的一些操作。

Dao.java

package sev;
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 passage.XinWen;
import tool.Tool;
public class Dao {
    public void add(XinWen xw) {
        Connection conn=Tool.getConnection();
        String sql="insert into xinwen (lanmu,timu,zhengwen) values (?,?,?) ";
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, xw.getLanmu());
            ps.setString(2, xw.getTimu());
            ps.setString(3, xw.getZhengwen());
            ps.executeUpdate();
        }catch(SQLException e){
            e.printStackTrace();
        }
        finally {
            Tool.release(conn, null, null);
        }
    }
    public void Up(String lanmu,String timu,String zhengwen ) {//修改
        Connection connn=null;
        PreparedStatement preparedStatement=null;
        try {
            connn= Tool.getConnection();
            String sql="update xinwen set zhengwen=? where lanmu=? and timu=?";
            preparedStatement=connn.prepareStatement(sql);
            preparedStatement.setString(1,zhengwen);
            preparedStatement.setString(2,lanmu);
            preparedStatement.setString(3,timu);
            preparedStatement.executeUpdate();
        }
        catch(SQLException e) {
            e.printStackTrace();
        } finally {
            Tool.release(connn,preparedStatement,null);
        }
    }
    public List<XinWen> findall() {
        List<XinWen> list=new ArrayList<>();
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;
        try {
            //调用Tool连接mysql数据库
            connection= Tool.getConnection();
            String sql="select * from xinwen";//查询语句
            preparedStatement=connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                //从resultSet拿出每个属性数据
                String lanmu=resultSet.getString ("lanmu");
            String timu=resultSet.getString("timu");
            String zhengwen=resultSet.getString("zhengwen");
                //这里可以理解为,resultSet拿出每个属性数据赋予student对象,形成一个有数据的student对象
            XinWen t = new XinWen(lanmu,zhengwen,timu);
                list.add(t);//可能多条数据,放到集合中

            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //调用JDBCTools,关闭connection,preparedStatement,resultSet
            Tool.release(connection,preparedStatement,resultSet);
        }
        return list;
    }
    public List<String> findyj() {
        List<String> list=new ArrayList<>();
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;
        try {
            //调用Tool连接mysql数据库
            connection= Tool.getConnection();
            String sql="select * from xinwen";//查询语句
            preparedStatement=connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                //从resultSet拿出每个属性数据
                String lanmu=resultSet.getString ("lanmu");
            String timu=resultSet.getString("timu");
            String zhengwen=resultSet.getString("yijian");
                //这里可以理解为,resultSet拿出每个属性数据赋予student对象,形成一个有数据的student对象
            String s=lanmu+"  "+timu+"  "+zhengwen;
                list.add(s);//可能多条数据,放到集合中

            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //调用JDBCTools,关闭connection,preparedStatement,resultSet
            Tool.release(connection,preparedStatement,resultSet);
        }
        return list;
    }
    public XinWen finds(String timu,String lanmu) {//查询
        Connection conn=Tool.getConnection();
        PreparedStatement pre=null;
        ResultSet res=null;
        XinWen stu=null;
        String sql="SELECT *FROM xinwen where timu=? and lanmu=?";
        try {
        pre=conn.prepareStatement(sql);
        pre.setString(1, timu);
        pre.setString(2, lanmu);
        res=pre.executeQuery();
        while(res.next()) {
            String lanmu1=res.getString ("lanmu");
            String timu1=res.getString("timu");
            String zhengwen=res.getString("zhengwen");
            stu=new XinWen(lanmu1,zhengwen,timu1);
        }
        }
    catch(SQLException e) {
            
            e.printStackTrace();
        }finally{
            Tool.release(conn, pre, res);
        }
        return stu;
    }
    public void del(String timu) {//删除
        Connection connn=null;
        PreparedStatement preparedStatement=null;
        try {
            connn= Tool.getConnection();
            String sql="delete from xinwen where timu=?";
            preparedStatement=connn.prepareStatement(sql);
            preparedStatement.setString(1,timu);
            preparedStatement.executeUpdate();
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                Tool.release(connn,preparedStatement,null);
            }
    }
    public void adds(XinWen g) {
        Connection conn=Tool.getConnection();
        String sql="insert into yifabu (lanmu,timu,zhengwen) values (?,?,?) ";
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, g.getLanmu());
            ps.setString(2, g.getTimu());
            ps.setString(3, g.getZhengwen());
            ps.executeUpdate();
        }catch(SQLException e){
            e.printStackTrace();
        }
        finally {
            Tool.release(conn, null, null);
        }
    }
    public XinWen findm(String timu) {//查询
        Connection conn=Tool.getConnection();
        XinWen stu=null;
        ResultSet rs = null; 
        Statement stmt = null;  
        try { 
             
            String sql = "SELECT *FROM xinwen where xinwen.timu like '%"+timu+"%';";  //查询语句
            stmt = conn.createStatement();  
            rs = stmt.executeQuery(sql);  
        while(rs.next()) {
            String lanmu1=rs.getString ("lanmu");
            String timu1=rs.getString("timu");
            String zhengwen=rs.getString("zhengwen");
            stu=new XinWen(lanmu1,zhengwen,timu1);
        }
        }
    catch(SQLException e) {
            
            e.printStackTrace();
        }finally{
            Tool.release(conn, stmt, rs);
        }
        return stu;
    }
}

sev.java

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

import passage.XinWen;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
@WebServlet("/xw")
public class Sev extends HttpServlet{
Dao d=new Dao();
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
    String method=request.getParameter("method");
    switch(method) {
    case "add":
         String lanmu= request.getParameter("lanmu");
         String zhengwen=request.getParameter("word");
         String timu=request.getParameter("tm");
         XinWen xw=new XinWen(lanmu,zhengwen,timu);
         d.add(xw);
         request.setAttribute("test", "公文上传成功");
               request.getRequestDispatcher("tishi1.jsp").forward(request, response);
        break;
    case "findall"://查询所有数据
    List<XinWen> list = d.findall();
    request.setAttribute("test", list);
    request.getRequestDispatcher("tmessage.jsp").forward(request, response);
    break;
    case "findalls"://查询所有数据
        List<XinWen> listss = d.findall();
        request.setAttribute("test", listss);
        request.getRequestDispatcher("tme.jsp").forward(request, response);
        break;
    case "findAll"://查询所有数据
        List<XinWen> lists = d.findall();
        request.setAttribute("test", lists);
        request.getRequestDispatcher("all.jsp").forward(request, response);
        break;
    case "findyj":
        List<String> list1 = d.findyj();
        request.setAttribute("test", list1);
        request.getRequestDispatcher("yijian.jsp").forward(request, response);
        break;
    case "findById":
        String lanmu1=request.getParameter("lanmu");
        String timu1=request.getParameter("timu");
        XinWen st=d.finds(timu1,lanmu1);
        request.setAttribute("test", st);
        request.getRequestDispatcher("update.jsp").forward(request, response);
        break;
    case "up":
        String l= request.getParameter("lanmu");
           String t=request.getParameter("timu");
           String z=request.getParameter("zhengwen");
        d.Up(l,t,z);
        request.setAttribute("test", "修改成功");
        request.getRequestDispatcher("tishi1.jsp").forward(request, response);
     case "delete"://删除操作
         String timus=request.getParameter("timu");
         d.del(timus);
         request.setAttribute("test", "删除成功");
         request.getRequestDispatcher("tishi1.jsp").forward(request, response);
         break;
     case "fabu":
         String ll= request.getParameter("lanmu");
               String tt=request.getParameter("timu");
               String zz=request.getParameter("zhengwen");
               XinWen xx=new XinWen(ll,zz,tt);
               d.adds(xx);
               request.setAttribute("test", "发布成功");
             request.getRequestDispatcher("tishi1.jsp").forward(request, response);
         break;
     case "findm":
         String timue= request.getParameter("tm");
         XinWen xxx=d.findm(timue);
         request.setAttribute("test", xxx);
             request.getRequestDispatcher("tishi1.jsp").forward(request, response);
         break;
    }
}
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
    doPost(request,response);
}
}

Daof.java

package sev;
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 passage.XinWen;
import tool.Tool;
public class Daof {
    public List<XinWen> findall() {
        List<XinWen> list=new ArrayList<>();
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;
        try {
            //调用Tool连接mysql数据库
            connection= Tool.getConnection();
            String sql="select * from yifabu";//查询语句
            preparedStatement=connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                //从resultSet拿出每个属性数据
                String lanmu=resultSet.getString ("lanmu");
            String timu=resultSet.getString("timu");
            String zhengwen=resultSet.getString("zhengwen");
                //这里可以理解为,resultSet拿出每个属性数据赋予student对象,形成一个有数据的student对象
            XinWen t = new XinWen(lanmu,zhengwen,timu);
                list.add(t);//可能多条数据,放到集合中

            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //调用JDBCTools,关闭connection,preparedStatement,resultSet
            Tool.release(connection,preparedStatement,resultSet);
        }
        return list;
    }
    public String cp(String lanmu,String timu) {
        Connection conn=Tool.getConnection();
        PreparedStatement pre=null;
        ResultSet res=null;
        String stu=null;
        String sql="SELECT *FROM yifabu where lanmu=? and timu=? ";
        try {
        pre=conn.prepareStatement(sql);
        pre.setString(1, lanmu);
        pre.setString(2, timu);
        res=pre.executeQuery();
        while(res.next()) {
            String pl=res.getString("pinglun");
            stu=pl;
        }
        }
    catch(SQLException e) {
            
            e.printStackTrace();
        }finally{
            Tool.release(conn, pre, res);
        }
        return stu;
    }
    public void Up(String lanmu,String timu,String pinglun) {//修改
        Connection connn=null;
        PreparedStatement preparedStatement=null;
        try {
            connn=Tool.getConnection();
            connn= Tool.getConnection();
            String sql="update yifabu set pinglun=? where lanmu=? and timu=?";
            preparedStatement=connn.prepareStatement(sql);
            preparedStatement.setString(1,pinglun);
            preparedStatement.setString(2,lanmu);
            preparedStatement.setString(3,timu);
            preparedStatement.executeUpdate();
        }
        catch(SQLException e) {
            e.printStackTrace();
        } finally {
            Tool.release(connn,preparedStatement,null);
        }
    }
}

Sevf.java

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

import passage.XinWen;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
@WebServlet("/xwf")
public class Sevf extends HttpServlet{
Daof d=new Daof();
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
    String method=request.getParameter("method");
    switch(method) {
    case "findAll"://查询所有数据
        List<XinWen> lists = d.findall();
        request.setAttribute("test", lists);
        request.getRequestDispatcher("alls.jsp").forward(request, response);
        break;
    case "cp":
        String lanmu=request.getParameter("lanmu");
        String timu=request.getParameter("timu");
        String pinglun=d.cp(lanmu, timu);
        request.setAttribute("test", pinglun);
        request.getRequestDispatcher("pinglun.jsp").forward(request, response);
        break;
    case "fa":
        String lanmus=request.getParameter("lanmu");
        String timus=request.getParameter("timu");
        request.setAttribute("test", lanmus);
        request.setAttribute("tests", timus);
        request.getRequestDispatcher("fabiaopl.jsp").forward(request, response);
        break;
    case "fas":
        String lanmuss=request.getParameter("lanmu");
        String timuss=request.getParameter("timu");
        String pl=request.getParameter("pl");
        d.Up(lanmuss, timuss, pl);
        break;
    }
}
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
    doPost(request,response);
}
}

总结本次的开学测试,有许多的功能没有实现,总分40分,得了24分,一些细节的功能没有完成,之后的两个权限没有完成,一些用户的信息管理。之前的那个修改意见不可以提出。

posted @ 2023-02-13 19:31  一个小虎牙  阅读(27)  评论(0编辑  收藏  举报