首页 > 编程语言 >2.23 Javaweb 总结

2.23 Javaweb 总结

时间:2023-02-23 21:59:49浏览次数:46  
标签:总结 Javaweb rs import state SQLException sql 2.23 String

今日不报错了,但是页面一直404,目前没找到问题在哪

AddServlet
package com;

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 java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

@WebServlet("/AddServlet")
public class AddServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    Service service = new Service();


    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编译格式
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        //接收数据
        String riqi = request.getParameter("riqi");
        String keyword = request.getParameter("keyword");
        String zongjie = request.getParameter("zongjie");
        String inday = request.getParameter("inday");
        String maxday = request.getParameter("maxday");

        PrintWriter write = response.getWriter();

        PreparedStatement preparedStatement = null;
        Connection connection = null;
        try {
            connection = Util.getConnection();
            String sql = "insert into day(riqi,keyword,zongjie,inday,maxday) values(?,?,?,?,?)";
            preparedStatement = null;
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, riqi);
            preparedStatement.setString(2, keyword);
            preparedStatement.setString(3, zongjie);
            preparedStatement.setString(4, inday);
            preparedStatement.setString(5, maxday);


            preparedStatement.executeUpdate();
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            } finally {
                Util.close(preparedStatement);
                Util.close(connection);
            }
            request.getRequestDispatcher("liulan.jsp").forward(request,response);
        }
        private boolean isNumeric(String str) {
            for (int i = str.length();--i>=0;){
                if (!Character.isDigit(str.charAt(i))){
                    return false;
                }
            }
            return true;
        }
}
Dao
package com;

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

public class Dao {
    public boolean add(Bean ten) throws ClassNotFoundException, SQLException {
        String sql = "insert into day(riqi,keyword,zongjie,inday,maxday)values"
                + "('" + ten.getRiqi() + "','" + ten.getKeyword() + "','" + ten.getZongjie() + "','" + ten.getInday() + "','" + ten.getMaxday() + "')";

        Connection conn = Util.getConnection();
        Statement state = null;
        boolean f = false;
        int a = 0;
        try {
            state = conn.createStatement();
            state.executeUpdate(sql);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {

            Util.close(state, conn);
        }
        if (a > 0)
            f = true;
        return f;
    }


    public Bean getbytitle(String name) throws ClassNotFoundException, SQLException {
        String sql = "select * from day where Date ='" + name + "'";
        Connection conn = Util.getConnection();
        Statement state = null;
        ResultSet rs = null;
        Bean ten = null;

        try {
            state = conn.createStatement();
            rs = state.executeQuery(sql);
            while (rs.next()) {


                String title2 = rs.getString("riqi");
                String zi2 = rs.getString("keyword");
                String person2 = rs.getString("zongjie");
                String date2 = rs.getString("inday");
                String neirong2 = rs.getString("maxday");

                ten = new Bean(title2, zi2, person2, date2, neirong2);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            Util.close(rs, state, conn);
        }

        return ten;
    }
    public boolean delete(String name) throws SQLException, ClassNotFoundException {
        String sql="delete from daka where date='" + name + "'";
        Connection conn = Util.getConnection();
        Statement state = null;
        int a = 0;
        boolean f = false;
        try {
            state = conn.createStatement();
            a = state.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            Util.close(state, conn);
        }

        if (a > 0) {
            f = true;
        }
        return f;
    }
    public boolean name(String name) throws SQLException, ClassNotFoundException {
        boolean flag = false;
        String sql = "select * from day where date = '" + name + "'";
        Connection conn = Util.getConnection();
        Statement state = null;
        ResultSet rs = null;

        try {
            state = conn.createStatement();
            rs = state.executeQuery(sql);
            while (rs.next()) {
                flag = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            Util.close(rs, state, conn);
        }
        return flag;
    }
    public List<Bean> search(String keyword, String riqi) throws SQLException, ClassNotFoundException {
        String sql = "select * from day where ";


        if (keyword != "") {
            sql += "zhuti like '%" +keyword+ "%'";
        }

        if (riqi != "") {
            sql += "time like '%" +riqi+ "%'";
        }

        List<Bean> list = new ArrayList<>();
        Connection conn = Util.getConnection();
        Statement state = null;
        ResultSet rs = null;
        Bean bean = null;
        try {
            state = conn.createStatement();
            rs = state.executeQuery(sql);
            while (rs.next()) {
                String title2 = rs.getString("title");
                String zi2 = rs.getString("zi");
                String person2 = rs.getString("person");
                String date2=rs.getString("date");
                String neirong2=rs.getString("neirong");
                bean = new Bean(title2, zi2 ,person2,date2,neirong2);

                list.add(bean);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            Util.close(rs, state, conn);
        }

        return list;
    }

    public List<Bean> list() throws SQLException, ClassNotFoundException {
        String sql = "select * from day";
        List<Bean> list = new ArrayList<>();
        Connection conn = Util.getConnection();
        Statement state = null;
        ResultSet rs = null;

        try {
            state = conn.createStatement();
            rs = state.executeQuery(sql);
            while (rs.next()) {
                Bean bean = null;
                int id=rs.getInt("id");
                String title2 = rs.getString("date");
                String zi2 = rs.getString("zi");
                String person2 = rs.getString("zong");
                String date2=rs.getString("days");
                String neirong2=rs.getString("Mdays");

                bean = new Bean(title2, zi2 ,person2,date2,neirong2);

                list.add(bean);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            Util.close(rs, state, conn);
        }

        return list;
    }
}
Service
package com;

import java.sql.SQLException;
import java.util.List;

public class Service {
    Dao tDao=new Dao();
    public boolean add(Bean ten) throws SQLException, ClassNotFoundException {
        boolean f = false;
        if(!tDao.name(ten.getRiqi()))
        {
            tDao.add(ten);
            f=true;
        }
        return f;
    }

    public boolean del(String title) throws SQLException, ClassNotFoundException {
        tDao.delete(title);
        return true;
    }




    public Bean getbytitle(String title) throws SQLException, ClassNotFoundException {
        return tDao.getbytitle(title);
    }




    public List<Bean> list() throws SQLException, ClassNotFoundException {
        return tDao.list();
    }
}

  

 

 

标签:总结,Javaweb,rs,import,state,SQLException,sql,2.23,String
From: https://www.cnblogs.com/kk4458/p/17149571.html

相关文章

  • 每日总结-23.2.23
    每日总结-23.2.23今天下午用了大约一个半小时小时的时间在复习昨天学的基础上学了在activity_main.xml文件中的线性布局,也称作LinearLayout布局,学习了其中的像id,ba......
  • 2.23
    今天我正式跟着课程学习Androidstudio的使用教程,首先我学习了一些简单的代码,比如怎样用log打印信息,之后我学习了控件相关的知识,且这部分知识有些复杂,课程也比较多,我只学习......
  • 2023.2.23每日总结
     今天学习了Androidstudio关于EditText标签的运用EditText标签1.android:hint输入提示2.android:textColorHint输入提示文字的颜色3.android:inputType输入类型I......
  • 2.23每日总结——安卓
      今天学习了Android程序的签名打包,同时正在注册Github。   昨天弄了半天的Github也没弄好,找室友帮忙弄好了。今天复习了一下上次课的代码。根据直接考过的复习......
  • 2.23博客
    1.今天写了把删除改了改,昨天有个错误,翻了翻日志发现有一行多了个字母。顺便也把修改写了,两个功能差不太多。  2.今天那个页面删除完直接跳转菜单,没跳转显示信息,改了......
  • 2023年2月23日学习总结
    今天继续学习AndroidApp的基础内容学习了AndroidStudio软件的一些功能和项目目录文件的说明,学习了其中的java文件,xml文件,清单注册文件和一些资源文件中一些基础的内容......
  • vue总结
    vue总结1前端发展史 -react,vue--》前端工程化---》前后端分离-大前端:flutter,uni-app2Vue介绍 -读音,单页面应用(spa),组件化开发,mvvm架构-Vue版本问题:vue2......
  • 2023年2.23软工日报
    今天上了工程数学的课,工程数学挺难的,听不懂,很多高数线代都忘了,上课听不上,打算周六补救一下,复习复习高数和线代。今天打了60行代码,学习了学习了安卓开发。MainActivity.ja......
  • 2月23日每日总结
    今日学习了JavaScript的String对象方法主要内容有:vars="ksjvdnasnvdlj"//1.length属性返回字符串的长度console.log(s.length)//2.charAt方法返......
  • 学习记录(2.23)
    今天总共学习了4h,其中有3h是在课上学习了工程数学的相关知识,为考研的专业课打下了一点点基础。之后用了1h的时间继续进行了对安卓程序开发的学习。因为今天要准备......