工程目录:
bean:通过bean传递servlet中的数据给dao
package Bean; public class bean { private String words; private int id; public String getWords() { return words; } public void setWords(String words) { this.words = words; } public bean(String words) { this.words=words; } public bean() { } public bean(int id, String words) { this.id=id; this.words=words; } public int getId() { // TODO Auto-generated method stub return this.id; }; }bean
dao:实现对数据库的操作
package Dao; import java.sql.ResultSet; import java.sql.SQLDataException; import java.sql.SQLException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.Statement; import Util.DBUtil; import Bean.bean; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class dao{ private DBUtil dbutil=new DBUtil(); public dao() {}; public boolean cook(bean b) { boolean f=false; String sql="insert into words(id,words) values(?,?)"; Connection conn = null; Statement state=null; PreparedStatement ps=null; ResultSet rs=null; int cnt=0; try { conn = DBUtil.getConnection(); //state=conn.createStatement(); ps=conn.prepareStatement(sql); ps.setString(1, null); ps.setString(2, b.getWords()); cnt=ps.executeUpdate(); if(cnt>0) f=true; }catch(Exception e) { e.printStackTrace(); }finally { DBUtil.close(conn); } return f; } public boolean discard(int id) { boolean f=false; String sql="delete from words where id='"+id+"'"; Connection conn = null; //Statement state=null; PreparedStatement ps=null; ResultSet rs=null; int cnt=0; try { conn = DBUtil.getConnection(); //state=conn.createStatement(); ps=conn.prepareStatement(sql); ps.executeUpdate(); //state.executeUpdate(sql); cnt=ps.executeUpdate(); if(cnt>0) f=true; }catch(Exception e) { e.printStackTrace(); }finally { DBUtil.close(conn); DBUtil.close(ps); //DBUtil.close(state); } return f; } public boolean add(bean b) { boolean f=false; String sql="update words set id='"+b.getId()+"', words='"+b.getWords()+"' where id='"+b.getId()+"'"; Connection conn = null; Statement state=null; PreparedStatement ps=null; //ResultSet rs=null; int cnt=0; try { conn = DBUtil.getConnection(); ps=conn.prepareStatement(sql); ps.executeUpdate(); cnt=ps.executeUpdate(sql); if(cnt>0) f=true; }catch(Exception e) { e.printStackTrace(); }finally { DBUtil.close(conn); DBUtil.close(ps); DBUtil.close(state); } return f; } }dao
servlet:接收来自前端的数据,并利用dao对数据库进行操作
package servlet; import Bean.bean; import Util.DBUtil; import Dao.dao; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class Servlet */ @WebServlet("/Servlet") public class Servlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public Servlet() { super(); // TODO Auto-generated constructor stub } private void cook(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{ request.setCharacterEncoding("UTF-8"); String words=request.getParameter("words"); dao dao=new dao(); bean b=new bean(words); try { if(dao.cook(b)) { request.setAttribute("messqge","成功上桌"); request.getRequestDispatcher("success.jsp").forward(request, response); } else { request.setAttribute("messqge","上桌失败"); request.getRequestDispatcher("defeat.jsp").forward(request, response); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } private void discard(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{ request.setCharacterEncoding("UTF-8"); int id=Integer.parseInt(request.getParameter("id")); dao dao=new dao(); try { if(dao.discard(id)) { request.setAttribute("messqge","成功丢弃"); request.getRequestDispatcher("success.jsp").forward(request, response); } else { request.setAttribute("messqge","丢弃失败"); request.getRequestDispatcher("defeat.jsp").forward(request, response); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } private void add(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{ request.setCharacterEncoding("UTF-8"); int id=Integer.parseInt(request.getParameter("id")); String words=request.getParameter("words"); dao dao=new dao(); bean b=new bean(id,words); try { if(dao.add(b)) { request.setAttribute("messqge","成功加料"); request.getRequestDispatcher("success.jsp").forward(request, response); } else { request.setAttribute("messqge","加料失败"); request.getRequestDispatcher("defeat.jsp").forward(request, response); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("utf-8"); String method=request.getParameter("method"); if("cook".equals(method)) { cook(request,response); } else if("discard".equals(method)) { discard(request,response); } else if("add".equals(method)) { add(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); } }servlet
DBUtil:链接工具
package Util; import java.sql.*; public class DBUtil { public static Connection getConnection() throws ClassNotFoundException, SQLException { Connection connection = null;//连接数据库 Statement stmt = null;//Statement 对象用于将 SQL 语句发送到数据库中。 ResultSet rs = null; //1. 导入驱动jar包 //2.注册驱动 Class.forName("com.mysql.cj.jdbc.Driver"); // 好像要设置时区?? connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/Mydea?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT", "root", "200214"); return connection; } public static void close(Connection connection) { try { if (connection != null) { connection.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void close(PreparedStatement preparedStatement) { try { if (preparedStatement != null) { preparedStatement.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void close(ResultSet resultSet) { try { if (resultSet != null) { resultSet.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void close(Statement st) { try { if (st != null) { st.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void close(Statement state, Connection connection) { try { if (connection != null) { connection.close(); } if (state != null) { state.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void close(ResultSet rs, Statement state, Connection conn) { try { if (conn != null) { conn.close(); } if (state != null) { state.close(); } if(rs != null) { rs.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }DBUtil
mune.jsp 菜单
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>鸡汤大全</title> </head> <body> <p style="text-align:center;font-size:20px" > <input type="button" value="开始喝汤" onclick="location.href='start.jsp'" /><br> <input type="button" value="我要煮汤" onclick="location.href='cook.jsp'" /><br> <input type="button" value="我要倒汤" onclick="location.href='discard.jsp'" /><br> <input type="button" value="给汤加料" onclick="location.href='add.jsp'" /><br> <input type="button" value="我要找汤" onclick="location.href='find.jsp'" /><br> <br> </p> </body> </html>mune
cook.jsp 增加页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>煮汤环节</title> </head> <body> <% Object message = request.getAttribute("message"); if (message != null && !"".equals(message)) { %> <script type="text/javascript"> alert("<%=request.getAttribute("message")%>"); //弹出对话框 </script> <% } %> <h3 align="center" >开始煮汤</h2> <form action="Servlet?method=cook" method="post"> <table align="center" width=0 border="2"> <td><input type="text" name="words" wideth="600"></td> <td> <button type="submit">上桌</button> </td> <td><input type="button" value="蒜啦蒜啦" onclick="location.href='mune.jsp'" /></td> </table> </form> </body> </html>cook
discard.jsp 删除页面,利用按钮控制删除的数据
<%@ page import="Util.DBUtil"%> <%@ page import="java.sql.Connection" %> <%@ page import="java.sql.PreparedStatement" %> <%@ page import="java.sql.ResultSet" %> <%@ page import="java.sql.SQLException" %> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <% Object message = request.getAttribute("message"); if (message != null && !"".equals(message)) { %> <script type="text/javascript"> alert("<%=request.getAttribute("message")%>"); //弹出对话框 </script> <% } %> <body> <form action="Servlet?method=discard" align="center" method="post"> <table align="center" border="1"> <tr> <td align="center"> id </td> <td align="center"> 鸡汤 </td> <td align="center"> 删除 </td> </tr> <% Connection conn=DBUtil.getConnection(); PreparedStatement pre=null; ResultSet in=null; try{ // 按照添加时间排序:: pre=conn.prepareStatement("select * from words"); in = pre.executeQuery(); while(in.next()){ %> <tr> <td><%=in.getInt(1)%></td> <td><%=in.getString(2)%></td> <td><button type="submit" name="id" value="<%=in.getString(1)%>"> 删除</button></td>> </tr> <% } } catch (SQLException e) { throw new RuntimeException(e); }finally { conn.close(); pre.close(); in.close(); } %> </table> <td><input type="button" value="蒜啦蒜啦" onclick="location.href='mune.jsp'" /></td> </form> </body> </html>discard
find.jsp 查询页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>找汤</title> </head> <body> <form action=finded.jsp method="post" align="center"> <tr> <select name="se"> <option value="1">id</option> <option value="2">鸡汤</option> </select> <input type="text" name="lect"></input> </tr> <tr> <button type="submit">开找</button> <td><input type="button" value="蒜啦蒜啦" onclick="location.href='mune.jsp'" /></td> </tr> </form> </body> </html>find
finded.jsp 输出查询结果
<%@ page import="Util.DBUtil"%> <%@ page import="java.sql.Connection" %> <%@ page import="java.sql.PreparedStatement" %> <%@ page import="java.sql.ResultSet" %> <%@ page import="java.sql.SQLException" %> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>找到了啥</title> </head> <% Object message = request.getAttribute("message"); if (message != null && !"".equals(message)) { %> <script type="text/javascript"> alert("<%=request.getAttribute("message")%>"); //弹出对话框 </script> <% } %> <body> <h3 align="center">找到了啥</h3> <form align="center" > <table align="center" border="1"> <tr> <td align="center"> id </td> <td align="center"> 鸡汤 </td> </tr> <% request.setCharacterEncoding("utf-8"); int id=Integer.parseInt(request.getParameter("se")); String lect=request.getParameter("lect"); if(id==1){ Connection conn=DBUtil.getConnection(); PreparedStatement pre=null; ResultSet in=null; try{ // 按照添加时间排序:: pre=conn.prepareStatement("select * from words"); in = pre.executeQuery(); while(in.next()){ if(lect.equals(in.getString(1))){ %> <tr> <td><%=in.getInt(1)%></td> <td><%=in.getString(2)%></td> </tr> <% } } } catch (SQLException e) { throw new RuntimeException(e); }finally { conn.close(); pre.close(); in.close(); } } %> <% if(id==2) { Connection conn=DBUtil.getConnection(); PreparedStatement pre=null; //Statement ResultSet in=null; int cnt=0; try{ // 按照添加时间排序:: pre=conn.prepareStatement("select * from words where words like '%"+lect+"%'"); String sql="select * from words where words like '%"+lect+"%'"; conn.createStatement(); in = pre.executeQuery(sql); %> <% while(in.next()) { %> <tr> <td><%=in.getInt(1)%></td> <td><%=in.getString(2)%></td> </tr> <% } } catch (SQLException e) { throw new RuntimeException(e); }finally { conn.close(); pre.close(); in.close(); } } %> </table> <p align="center"> <input type="button" value="O.o" onclick="location.href='mune.jsp'" /> <input type="button" value="T.T" onclick="location.href='find.jsp'" /> </p> </body> </html>finded
add.jsp 修改页面,利用按钮控制修改的数据
<%@ page import="Util.DBUtil"%> <%@ page import="java.sql.Connection" %> <%@ page import="java.sql.PreparedStatement" %> <%@ page import="java.sql.ResultSet" %> <%@ page import="java.sql.SQLException" %> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>料加给谁</title> </head> <% Object message = request.getAttribute("message"); if (message != null && !"".equals(message)) { %> <script type="text/javascript"> alert("<%=request.getAttribute("message")%>"); //弹出对话框 </script> <% } %> <body> <h3 align="center"> 加料咯</h3> <form action="add1.jsp" align="center" method="post"> <table align="center" border="1"> <tr> <td align="center"> id </td> <td align="center"> 鸡汤 </td> <td align="center"> 加料 </td> </tr> <% Connection conn=DBUtil.getConnection(); PreparedStatement pre=null; ResultSet in=null; try{ // 按照添加时间排序:: pre=conn.prepareStatement("select * from words"); in = pre.executeQuery(); while(in.next()){ %> <tr> <td><%=in.getInt(1)%></td> <td><%=in.getString(2)%></td> <td><button type="submit" name="id" value="<%=in.getString(1)%>">加料</button></td>> </tr> <% } } catch (SQLException e) { throw new RuntimeException(e); }finally { conn.close(); pre.close(); in.close(); } %> </table> <td><input type="button" value="蒜啦蒜啦" onclick="location.href='mune.jsp'" /></td> </form> </body> </html>add
add1.jsp 录入修改的值
<%@ page import="Util.DBUtil"%> <%@ page import="java.sql.Connection" %> <%@ page import="java.sql.PreparedStatement" %> <%@ page import="java.sql.ResultSet" %> <%@ page import="java.sql.SQLException" %> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>给汤加点料</title> </head> <% Object message = request.getAttribute("message"); if (message != null && !"".equals(message)) { %> <script type="text/javascript"> alert("<%=request.getAttribute("message")%>"); //弹出对话框 </script> <% } %> <body> <form action="Servlet?method=add" align="center" method="post"> <table align="center" border="1"> <tr> <td align="center"> id </td> <td align="center"> 鸡汤 </td> </tr> <% int id=Integer.parseInt(request.getParameter("id")); Connection conn=DBUtil.getConnection(); PreparedStatement pre=null; ResultSet in=null; try{ // 按照添加时间排序:: pre=conn.prepareStatement("select * from words"); in = pre.executeQuery(); while(in.next()){ if(id==in.getInt(1)){ %> <tr> <td><%=in.getInt(1)%></td> <td><%=in.getString(2)%></td> </tr> <tr> <td><input name="id" value="<%=in.getString(1)%>"></button></td>> <td><input type="text" name="words" ></td> </tr> <% } } } catch (SQLException e) { throw new RuntimeException(e); }finally { conn.close(); pre.close(); in.close(); } %> </table> <button type="submit">上桌</button> <td><input type="button" value="蒜啦蒜啦" onclick="location.href='mune.jsp'" /></td> </form> </body> </html>add1
start.jsp 随机输出一条数据,利用random实现
<%@ page import="Util.DBUtil" %> <%@ page import="java.sql.Connection" %> <%@ page import="java.math.*" %> <%@ page import="java.sql.PreparedStatement" %> <%@ page import="java.sql.ResultSet" %> <%@ page import="java.sql.SQLException" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%--<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>--%> <html> <head> <title>浏览</title> </head> <body> <h2 align="center" >------------------------------------------------------------------</h2><br> <table align="center" border="1"> <% int random=(int)(1+Math.random()*5); Connection connection= DBUtil.getConnection(); PreparedStatement preparedStatement=null; ResultSet in=null; try{ // 按照添加时间排序:: preparedStatement=connection.prepareStatement("select * from Words"); in = preparedStatement.executeQuery(); while(in.next()){ if(in.getInt(1)==random){ %> <tr> <td><%=in.getString(1)%></td> <td><%=in.getString(2)%></td> </tr> <% } } }catch (SQLException e) { throw new RuntimeException(e); }finally { connection.close(); preparedStatement.close(); in.close(); } %> <p style="text-align:center;color: black; font-family: 宋体; font-size: 20px"> <br> <input type="button" value="好吃不嫌多" onclick="location.href='start.jsp'" /> <input type="button" value="匿了匿了" onclick="location.href='mune.jsp'" /> <br> </table> </body> </html>start
success.jsp 操作成功则跳转到此页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>失败咯</title> </head> <body> <h1 align="center"> 成功咯<br> <input type="button" value="返回主界面" onclick="location.href='mune.jsp'" /> </h1> </body> </html>success
defeat.jsp 操作失败跳转到此页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>失败咯</title> </head> <body> <h1 align="center"> 失败咯<br> <input type="button" value="返回主界面" onclick="location.href='mune.jsp'" /> </h1> </body> </html>defeat
本来应该昨天发的,但是没有解决模糊查询的小问题(气抖冷,原因是没有限制传入数据的编码格式,导致中文变乱码查询失败)和再次发烧实在没心思找错,就当周末编程总结吧
标签:25,26,request,words,2023,close,import,null,public From: https://www.cnblogs.com/xxaxf/p/17156512.html