今日不报错了,但是页面一直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