这个增删改查困扰了很长时间,没学过相关内容的是真的不会。
这是用idea写的内容。
这是文件名
项目结构需要的jar包
数据库
表
Data.java
package test; import java.sql.*; public class Data { public String shijian, guanjianzi, zongjie, tiansshu, shichang; public String getTime() { return shijian; } public void setTime(String time) { this.shijian = time; } public String getGuanjianzi() { return guanjianzi; } public void setGuanjianzi(String guanjianzi) { this.guanjianzi = guanjianzi; } public String getZongjie() { return zongjie; } public void setZongjie(String zongjie) { this.zongjie = zongjie; } public String getTiansshu() { return tiansshu; } public void setTiansshu(String tiansshu) { this.tiansshu = tiansshu; } public String getShichang() { return shichang; } public void setShichang(String shichang) { this.shichang = shichang; } public Connection getConnection()//连接数据库 { try { Class.forName("com.mysql.cj.jdbc.Driver"); //System.out.println("加载驱动成功"); } catch (ClassNotFoundException e) { e.printStackTrace(); } String user = "root"; String password = "zhoujiaqi123"; String url = "jdbc:mysql://localhost:3306/db3?useSSL=false&serverTimezone=GMT&characterEncoding=utf-8&autoReconnect=true"; Connection con = null; try { con = DriverManager.getConnection(url, user, password); //System.out.println("数据库连接成功"); } catch (SQLException e) { e.printStackTrace(); } return con; } //********************************************************************** //关闭方法 public void close(Connection con) { try { if (con != null) { con.close(); } } catch (SQLException e) { e.printStackTrace(); } } public void close(PreparedStatement preparedStatement) { try { if (preparedStatement != null) { preparedStatement.close(); } } catch (SQLException e) { e.printStackTrace(); } } public void close(ResultSet resultSet) { try { if (resultSet != null) { resultSet.close(); } } catch (SQLException e) { e.printStackTrace(); } } //增 public void adddata(String shijian, String guanjianzi, String zongjie, String tianshu, String shichang) { Connection connection = getConnection(); PreparedStatement preparedStatement = null; try { String sql = "insert into test (shijian ,guanjianzi ,zongjie , tianshu ,shichang ) values (?,?,?,?,?)"; preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, shijian); preparedStatement.setString(2, guanjianzi); preparedStatement.setString(3, zongjie); preparedStatement.setString(4, tianshu); preparedStatement.setString(5, shichang); preparedStatement.executeUpdate(); System.out.println("添加成功"); } catch (SQLException e) { e.printStackTrace(); } finally { close(preparedStatement); close(connection); } } //删 public void deletedata(String shijian) { Connection connection = getConnection(); PreparedStatement preparedStatement = null; try { String sql = "delete from test where shijian = ?"; preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, shijian); preparedStatement.executeUpdate(); //System.out.println("删除成功"); } catch (SQLException e) { e.printStackTrace(); } finally { close(preparedStatement); close(connection); } } //改 public void revisedata(String shijian, String guanjianzi, String zongjie, String tianshu, String shichang) { Connection connection = getConnection(); PreparedStatement preparedStatement = null; try { String sql = "update test set guanjianzi=?, zongjie=?, tianshu=?, shichang=? where shijian=?"; preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, guanjianzi); preparedStatement.setString(2, zongjie); preparedStatement.setString(3, tianshu); preparedStatement.setString(4, shichang); preparedStatement.setString(5, shijian); preparedStatement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { close(preparedStatement); close(connection); } } //判重/判存在 public boolean isSame(String s1) { Connection connection = getConnection(); PreparedStatement preparedStatement = null; ResultSet rs = null; try { String sql = "select * from test"; preparedStatement = connection.prepareStatement(sql); rs = preparedStatement.executeQuery(); while (rs.next()) { if (s1.equals(rs.getObject(1)) || s1.equals(rs.getObject(2))) return true; } preparedStatement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { close(rs); close(preparedStatement); close(connection); } return false; } }
menu.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>主菜单</title> </head> <form action="addInput.jsp" method="get"> <p style="text-align:center;color:white;font-family:宋体; font-size:20px"> 增删改查 <br> 选择功能: <input type="button" value="信息登记" onclick="location.href='fabu.jsp'" /> <input type="button" value="信息删除" onclick="location.href='shanchu.jsp'" /> <input type="button" value="信息修改" onclick="location.href='xiugai.jsp'" /> <input type="button" value="查询信息" onclick="location.href='search.jsp'" /> <input type="button" value="浏览信息" onclick="location.href='show.jsp'" /> <br> </p> </form> </body> </html>
fabu.jsp
<%@page import="test.Data"%> <%@ 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="fabujudge.jsp" method="get"> <p style="text-align:center;color: black; font-family: 宋体; font-size: 20px"> <br> <br>日期 <input type="text" name="shijian" size="20" /> <br>每日关键字 <input type="text" name="guanjianzi" size="15" > <br>每日总结<input type="text" name="zongjie" size="15" > <br>坚持天数 <input type="text" name="tianshu" size="10" ><br> <br>连续最高时长 <input type="text" name="shichang" size="20" > <br><input type="submit" value="提交" /> <input type="reset" value="重置" /> <br> <br> <input type="button" value="返回菜单" onclick="location.href='menu.jsp'" /> <br> </p> </p> </form> </body> </html>
fabujudge.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> <jsp:useBean id="test" class="test.Data" scope="page" /> <% String shijian=(String)request.getParameter("shijian"); String guanjianzi=(String)request.getParameter("guanjianzi"); String zongjie=(String)request.getParameter("zongjie"); String tianshu=(String)request.getParameter("tianshu"); String shichang=(String)request.getParameter("shichang"); test.adddata(shijian, guanjianzi, zongjie, tianshu, shichang); out.print("<script language='javaScript'> alert('添加成功');</script>"); response.setHeader("refresh", "0;url=show.jsp"); %> </body> </html>
shanchu.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="shanchujudge.jsp" method="get"> <p style="text-align:center;color: black; font-family: 宋体; font-size: 20px"> 删除 <br> 要删除时间为: <input type="text" name="shijian" /> <br> <br><input type="submit" value="确定" /> <input type="reset" value="重置" /> <br> <br> <input type="button" value="返回菜单" onclick="location.href='menu.jsp'" /> <br> </p> </form> </body> </html>
shanchujudge.jsp
<%@ page language="java" import="java.sql.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> </head> <body> <jsp:useBean id="test" class="test.Data" scope="page" /> <% String shijian=(String)request.getParameter("shijian"); if(shijian==""){ out.print("<script language='javaScript'> alert('输入为空'); window.history.back(-1); </script>"); } else if(!test.isSame(shijian)) { out.print("<script language='javaScript'> alert('该活动主题不存在'); window.history.back(-1); </script>"); } else { %> <table border="1"style="text-align:center;"> <tr> <td align="center" width=10%>日期</td> <td align="center" width=10%>每日关键字</td> <td align="center" width=10%>每日总结</td> <td align="center" width=10%>坚持天数</td> <td align="center" width=10%>连续最高时长</td> </tr> <% Connection connection = test.getConnection(); PreparedStatement preparedStatement=null; ResultSet rs=null; try { String sql = "select * from test"; preparedStatement=connection.prepareStatement(sql); rs=preparedStatement.executeQuery(); while(rs.next()){ if(shijian.equals(rs.getObject(1))) { %> <tr> <td align="center"><%=rs.getObject(1) %></td> <td align="center"><%=rs.getObject(2) %></td> <td align="center"><%=rs.getObject(3) %></td> <td align="center"><%=rs.getObject(4) %></td> <td align="center"><%=rs.getObject(5) %></td> <td align="center"><a style="color:blue" href='shanchujudge2.jsp?shijian=<%=rs.getObject(1) %>' onclick="javascript:return del()">删除</a> </td> </tr> <% } } } catch (SQLException e) { e.printStackTrace(); }finally{ test.close(rs); test.close(preparedStatement); test.close(connection); } %> </table> <% } %> <script> function del(){ var r = confirm("确定要删除吗?") if (r == true) { return true; } else { return false; } } </script> <p style="text-align:center;color: black; font-family: 宋体; font-size: 20px"> <br> <input type="button" value="返回菜单" onclick="location.href='menu.jsp'" /> <br> </p> </body> </html>
shanchujudge2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="test.Data"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> </head> <body> <input type="button" onclick="openResult()" value="删除"/> <jsp:useBean id="test" class="test.Data" scope="page" /> <% String shijian=(String)request.getParameter("shijian"); test.deletedata(shijian); out.print("<script language='javaScript'> alert('删除成功');</script>"); response.setHeader("refresh", "0;url=shanchu.jsp"); %> <p style="text-align:center;color: black; font-family: 宋体; font-size: 20px"> <br> <input type="button" value="返回菜单" onclick="location.href='menu.jsp'" /> <br> </p> </body> </html>
xiugai.jsp
<%@page import="test.Data"%> <%@ 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="xiugaijudge.jsp" method="get"> <p style="text-align:center;color: black; font-family: 宋体; font-size: 20px"> <br> 输入要修改的日期 <input type="text" name="shijian" /><br> <br><input type="submit" value="提交" /> <input type="reset" value="重置" /> <br> <br> <input type="button" value="返回菜单" onclick="location.href='menu.jsp'" /> <br> </p> </form> </body> </html>
xiugaijudge.jsp
<%@ page language="java" import="java.sql.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>登记</title> </head> <body> <jsp:useBean id="test" class="test.Data" scope="page" /> <% String shijian=(String)request.getParameter("shijian"); if(shijian==""){ out.print("<script language='javaScript'> alert('输入为空'); window.history.back(-1); </script>"); } else if(!test.isSame(shijian)) { out.print("<script language='javaScript'> alert('该住户不存在'); window.history.back(-1); </script>"); } else{ %> <table border="1"style="text-align:center;"> <tr> <td align="center" width=10%>日期</td> <td align="center" width=10%>每日关键字</td> <td align="center" width=10%>每日总结</td> <td align="center" width=10%>坚持天数</td> <td align="center" width=10%>连续最高时长</td> </tr> <% Connection connection = test.getConnection(); PreparedStatement preparedStatement=null; ResultSet rs=null; try { String sql = "select * from test"; preparedStatement=connection.prepareStatement(sql); rs=preparedStatement.executeQuery(); while(rs.next()){ if(shijian.equals(rs.getObject(1))) { %> <tr> <td align="center"><%=rs.getObject(1) %></td> <td align="center"><%=rs.getObject(2) %></td> <td align="center"><%=rs.getObject(3) %></td> <td align="center"><%=rs.getObject(4) %></td> <td align="center"><%=rs.getObject(5) %></td> <td align="center"><a style="color:blue" href='xiugaijudge2.jsp?shijian=<%=rs.getObject(1) %>'>修改</a> </td> </tr> <% } } } catch (SQLException e) { e.printStackTrace(); }finally{ test.close(rs); test.close(preparedStatement); test.close(connection); } %> </table> <% } %> <p style="text-align:center;color: black; font-family: 宋体; font-size: 20px"> <br> <input type="button" value="返回菜单" onclick="location.href='menu.jsp'" /> <br> </p> </body> </html>
xiugaijudge2.jsp
<%@ page language="java" import="java.sql.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> </head> <body> <jsp:useBean id="test" class="test.Data" scope="page" /> <% String shijian=(String)request.getParameter("shijian"); session.setAttribute("shijian",shijian); %> <table border="1"style="text-align:center;"> <tr> <td align="center" width=10%>日期</td> <td align="center" width=10%>每日关键字</td> <td align="center" width=10%>每日总结</td> <td align="center" width=10%>坚持天数</td> <td align="center" width=10%>连续最高时长</td> </tr> <% Connection connection = test.getConnection(); PreparedStatement preparedStatement=null; ResultSet rs=null; try { String sql = "select * from test"; preparedStatement=connection.prepareStatement(sql); rs=preparedStatement.executeQuery(); while(rs.next()){ if(shijian.equals(rs.getObject(1))) { %> <tr> <td align="center"><%=rs.getObject(1) %></td> <td align="center"><%=rs.getObject(2) %></td> <td align="center"><%=rs.getObject(3) %></td> <td align="center"><%=rs.getObject(4) %></td> <td align="center"><%=rs.getObject(5) %></td> </tr> <% } } } catch (SQLException e) { e.printStackTrace(); }finally{ test.close(rs); test.close(preparedStatement); test.close(connection); } %> </table> <form action="xiugaijudge3.jsp" method="get"> <p style="text-align:center;color: black; font-family: 宋体; font-size: 20px"> <br> <br> 修改信息 <br>每日关键字 <input type="test" name="guanjianzi" /><br> <br> 每日总结 <input type="text" name="zongjie" /><br> <br> 坚持天数 <input type="text" name="tianshu" /><br> <br> 连续最高时长 <input type="text" name="shichang" /><br> <br> <br><input type="submit" value="提交" /> <input type="reset" value="重置" /> <br> <br> <input type="button" value="返回菜单" onclick="location.href='menu.jsp'" /> <br> </p> </form> </body> </html>
xiugaijudge3.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> </head> <body> <jsp:useBean id="test" class="test.Data" scope="page" /> <% String shijian=(String)session.getAttribute("shijian"); String guanjianzi=(String)request.getParameter("guanjianzi"); String zongjie=(String)request.getParameter("zongjie"); String tianshu=(String)request.getParameter("tianshu"); String shichang=(String)request.getParameter("shichang"); if(guanjianzi==""||zongjie==""||tianshu==""||shichang=="") { out.print("<script language='javaScript'> alert('输入为空');window.history.back(-1);</script>"); } else { test.revisedata(shijian,guanjianzi, zongjie, tianshu, shichang); out.print("<script language='javaScript'> alert('修改成功');</script>"); response.setHeader("refresh", "0;url=show.jsp"); } %> <p style="text-align:center;color: black; font-family: 宋体; font-size: 20px"> <br> <input type="button" value="返回菜单" onclick="location.href='menu.jsp'" /> <br> </p> </body> </html>
search.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="searchjudge.jsp" method="get"> <p style="text-align:center;color: black; font-family: 宋体; font-size: 20px"> 输入要查询信息的时间/关键字(支持模糊查询): <br> <input type="text" name="data" /> <br> <br><input type="submit" value="提交" /> <input type="reset" value="重置" /> <br> <br> <input type="button" value="返回菜单" onclick="location.href='menu.jsp'" /> <br> </p> </form> </body> </html>
searchjudge.jsp
<%@ page language="java" import="java.sql.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>查询</title> </head> <body style="text-align:center"> 查询结果 <p> <jsp:useBean id="test" class="test.Data" scope="page" /> <% String data=(String)request.getParameter("data"); if(data==""){ out.print("<script language='javaScript'> alert('输入为空'); window.history.back(-1); </script>"); } else { %> <table border="1"style="text-align:center;"> <tr> <td align="center" width=6%>时间</td> <td align="center" width=3%>每日关键字</td> <td align="center" width=10>每日总结</td> <td align="center" width=10%>坚持天数</td> </tr> <% int i=0; Connection connection = test.getConnection(); PreparedStatement preparedStatement=null; ResultSet rs=null; try { String sql= " select * from test where shijian like ? or guanjianzi like ? "; preparedStatement=connection.prepareStatement(sql); preparedStatement.setString(1,"%"+data+"%"); preparedStatement.setString(2,"%"+data+"%"); rs=preparedStatement.executeQuery(); while(rs.next()) { i++; %> <tr> <td align="center"><a style="color:blue" href='searchshowall.jsp?shijian=<%=rs.getObject(1) %>'><%=rs.getObject(1) %></a> </td> <td align="center"><%=rs.getObject(2) %></td> <td align="center"><%=rs.getObject(3) %></td> <td align="center"><%=rs.getObject(4) %></td> </tr> <% } if(i==0) { out.print("<script language='javaScript'> alert('没有查询到有关信息'); window.history.back(-1); </script>"); } } catch (SQLException e) { e.printStackTrace(); }finally{ test.close(rs); test.close(preparedStatement); test.close(connection); } } %> </table> <p style="text-align:center;color: black; font-family: 宋体; font-size: 20px"> <br> <input type="button" value="返回菜单" onclick="location.href='menu.jsp'" /> <br> </p> </body> </html>
searchshowall.jsp
<%@ page language="java" import="java.sql.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>详细信息</title> </head> <body> <jsp:useBean id="test" class="test.Data" scope="page" /> <% String shijian = (String)request.getParameter("shijian"); Connection connection = test.getConnection(); PreparedStatement preparedStatement=null; ResultSet rs=null; try { String sql = "select * from test"; preparedStatement=connection.prepareStatement(sql); rs=preparedStatement.executeQuery(); while(rs.next()){ if(shijian.equals(rs.getObject(1))) { %> <table border="1"style="text-align:center;"> <tr> <td align="center" width=10%>日期</td> <td align="center" width=10%>每日关键字</td> <td align="center" width=10%>每日总结</td> <td align="center" width=10%>坚持天数</td> <td align="center" width=10%>连续最高时长</td> </tr> <tr> <td align="center"><%=rs.getObject(1) %></td> <td align="center"><%=rs.getObject(2) %></td> <td align="center"><%=rs.getObject(3) %></td> <td align="center"><%=rs.getObject(4) %></td> <td align="center"><%=rs.getObject(5) %></td> </tr> <% } } } catch (SQLException e) { e.printStackTrace(); }finally{ test.close(rs); test.close(preparedStatement); test.close(connection); } %> </table> <p style="text-align:center;color: black; font-family: 宋体; font-size: 20px"> <input type="button" name="back" onclick="javascript:window.history.back(-1);" value=返回上一页> <input type="button" value="返回菜单" onclick="location.href='menu.jsp'" /> <br> </p> </body> </html>
show.jsp
<%@ page language="java" import="java.sql.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>浏览信息</title> </head> <body> <jsp:useBean id="test" class="test.Data" scope="page" /> <table border="1"style="text-align:center;"> <tr> <td align="center" width=10%>日期</td> <td align="center" width=10%>每日关键字</td> <td align="center" width=10%>每日总结</td> <td align="center" width=10%>坚持天数</td> <td align="center" width=10%>连续最高时长</td> </tr> <% Connection connection = test.getConnection(); PreparedStatement preparedStatement=null; ResultSet rs=null; try { String sql = "select * from test"; preparedStatement=connection.prepareStatement(sql); rs=preparedStatement.executeQuery(); while(rs.next()){ %> <tr> <td align="center"><%=rs.getObject(1) %></td> <td align="center"><%=rs.getObject(2) %></td> <td align="center"><%=rs.getObject(3) %></td> <td align="center"><%=rs.getObject(4) %></td> <td align="center"><%=rs.getObject(5) %></td> </tr> <% } } catch (SQLException e) { e.printStackTrace(); }finally{ test.close(rs); test.close(preparedStatement); test.close(connection); } %> </table> <p style="text-align:center;color: black; font-family: 宋体; font-size: 20px"> <input type="button" value="返回菜单" onclick="location.href='menu.jsp'" /> <br> </p> </body> </html>
以上就是本次增删改查的全部内容
参考:https://www.cnblogs.com/a8047/p/13991772.html
具体问题请看原作者。
标签:preparedStatement,String,改查,shijian,jsp,简单,增删,close,public From: https://www.cnblogs.com/zjq164/p/17156269.html