package jdbc; import java.sql.*; public class tools { private String zhuti, mudi, type, time, address, people, content, plan; public String getZhuti() { return zhuti; } public void setZhuti(String zhuti) { this.zhuti = zhuti; } public String getMudi() { return mudi; } public void setMudi(String mudi) { this.mudi = mudi; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getTime() { return time; } public void setTime(String time) { this.time = time; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getPeople() { return people; } public void setPeople(String people) { this.people = people; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String getPlan() { return plan; } public void setPlan(String plan) { this.plan = plan; } public tools() { super(); } //连接数据库 public Connection getConnection() throws Exception { try{ Class.forName("com.mysql.jdbc.Driver"); //System.out.println("加载驱动成功"); }catch(ClassNotFoundException e) { e.printStackTrace(); } String user="root"; String password="6666"; String url = "jdbc:mysql://127.0.0.1:3306/aaa"; Connection con=DriverManager.getConnection(url,user,password); //System.out.println("数据库连接成功"); 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 zhuti,String mudi,String type,String time,String address,String people,String content,String plan) throws Exception { Connection conn = getConnection(); PreparedStatement pstmt = null; String sql = "insert into huodong(活动主题,活动目的,活动类型,活动时间,活动地点,活动对象,活动内容,活动日程安排) values(?,?,?,?,?,?,?,?)"; pstmt = conn.prepareStatement(sql); pstmt.setString(1, zhuti); pstmt.setString(2, mudi); pstmt.setString(3, type); pstmt.setString(4, time); pstmt.setString(5, address); pstmt.setString(6, people); pstmt.setString(7, content); pstmt.setString(8, plan); pstmt.executeUpdate(); close(conn); close(pstmt); } //删 public void deletedata(String zhuti) throws Exception { Connection conn = getConnection(); PreparedStatement pstmt = null; String sql = "delete from huodong where 活动主题 = ?"; pstmt = conn.prepareStatement(sql); pstmt.setString(1, zhuti); pstmt.executeUpdate(); close(conn); close(pstmt); } //改 public void updata(String zhuti,String mudi,String type,String time,String address,String people,String content,String plan) throws Exception { Connection conn = getConnection(); PreparedStatement pstmt = null; String sql = "update huodong set 活动目的=?,活动类型=?,活动时间=?,活动地点=?,活动对象=?,活动内容=?,活动日程安排=? where 活动主题=?"; pstmt = conn.prepareStatement(sql); pstmt.setString(1,mudi); pstmt.setString(2,type); pstmt.setString(3,time); pstmt.setString(4,address); pstmt.setString(5,people); pstmt.setString(6,content); pstmt.setString(7,plan); pstmt.setString(8,zhuti); pstmt.executeUpdate(); close(conn);; close(pstmt); } //判存在 public boolean isSame(String zhuti) throws Exception { Connection conn = getConnection(); PreparedStatement pstmt = null; ResultSet rs = null; String sqlString = "select * from huodong"; pstmt = conn.prepareStatement(sqlString); rs = pstmt.executeQuery(); while(rs.next()) { if(zhuti.equals(rs.getObject(1))); return true; } close(conn); close(pstmt); return false; } //判空 public boolean isEmpty(String zhuti,String mudi,String type,String time,String address,String people,String content,String plan) { if(zhuti==null||mudi==null||type==null||time==null||address==null||people==null||content==null||plan==null) { return true; } else return false; } }
菜单
menu.jsp
<%@page import="java.security.DrbgParameters.NextBytes"%> <%@page import="java.sql.ResultSet"%> <%@page import="java.sql.PreparedStatement"%> <%@page import="java.sql.Connection"%> <%@ 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="add.jsp" method="get"> <p style="text-align:center;color:white;font-family:宋体; font-size:20px"> 校园社团活动管理系统 <br> 选择功能: <input type="button" value="发布活动信息" onclick="location.href='add.jsp'" /> <input type="button" value="修改活动信息" onclick="location.href='updata.jsp'" /> <input type="button" value="删除活动信息" onclick="location.href='delete.jsp'" /> <input type="button" value="查询活动信息" onclick="location.href='search.jsp'" /> <input type="button" value="活动信息浏览" onclick="location.href='show.jsp'" /> <br> </p> </form> </body> </html>
添加
add.jsp
addjudge.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="addjudge.jsp" method="get"> <p style="text-align:center;color: black; font-family: 宋体; font-size: 20px"> 发布活动信息页面 <br> <br>活动主题<input type = "text" name = "zhuti" maxlength = "20"/><br> <br>活动目的<input type = "text" name = "mudi" maxlength = "50"/><br> <br>活动类型 <input type="radio" name="type" value="社团竞赛" />社团竞赛 <input type="radio" name="type" value="野外采风" />野外采风 <input type="radio" name="type" value="校内集会" />校内集会 <input type="radio" name="type" value="社团纳新" />社团纳新 <br> <br>活动时间<input type = "text" name = "time" /><br> <br>活动地点<input type = "text" name = "address" /><br> <br><input type="checkbox" name="people" >社团成员 <input type="checkbox" name="people" >全体学生 <br> <br>活动内容<input type = "text" name = "content" maxlength = "500" /><br> <br>活动日程安排<input type = "text" name = "plan" maxlength = "500" /><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>
<%@page import="java.sql.PreparedStatement"%> <%@page import="java.sql.Connection"%> <%@ 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> <body> <jsp:useBean id="util" class="jdbc.tools" scope="page" /> <% String zhuti = request.getParameter("zhuti"); String mudi = request.getParameter("mudi"); String type = request.getParameter("type"); String time = request.getParameter("time"); String address = request.getParameter("address"); String people = (String)request.getParameter("people"); String content = request.getParameter("content"); String plan = request.getParameter("plan"); if(util.isEmpty(zhuti, mudi, type, time, address, people, content, plan)){ out.print("<script language='javaScript'> alert('输入为空');window.history.back(-1);</script>"); } else{ util.adddata(zhuti,mudi,type,time,address,people,content,plan); out.print("<script language='javaScript'> alert('添加成功');</script>"); response.setHeader("refresh", "0;url=show.jsp"); } %> </body> </html>
删除
delete.jsp
deletejudge1.jsp
deletejudge2.jsp
<%@ 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> <body> <form action="deletejudge1.jsp" method="get"> <p style="text-align:center;color: black; font-family: 宋体; font-size: 20px"> 删除页面 <br> 要删除的活动主题 <input type="text" name="zhuti" /> <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>
<%@page import="java.sql.ResultSet"%> <%@page import="java.sql.PreparedStatement"%> <%@page import="java.sql.Connection"%> <%@ 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> <body> <jsp:useBean id="util" class="jdbc.tools" scope="page" /> <% String zhuti = request.getParameter("zhuti"); %> <table border="1"style="text-align:center;"> <tr> <td align = "center">活动主题</td> <td align = "center">活动目的</td> <td align = "center">活动类型</td> <td align = "center">活动时间</td> <td align = "center">活动地点</td> <td align = "center">活动对象</td> <td align = "center">活动内容</td> <td align = "center">活动日程安排</td> <td align = "center">删除</td> </tr> <% Connection conn = util.getConnection(); PreparedStatement pstmt = null; ResultSet rs = null; String sql = "select * from huodong"; pstmt = conn.prepareStatement(sql); rs = pstmt.executeQuery(); while(rs.next()){ if(zhuti.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"><%=rs.getObject(6) %></td> <td align = "center"><%=rs.getObject(7) %></td> <td align = "center"><%=rs.getObject(8) %></td> <td align = "center"><a style="color:blue" href='deletejudge2.jsp?zhuti=<%=rs.getObject(1) %>' onclick="javascript:return del()">删除</a></td> </tr> <% } } %> </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>
<%@ 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> <body> <input type="button" onclick="openResult()" value="删除"/> <jsp:useBean id="util" class="jdbc.tools" scope="page" /> <% String zhuti = request.getParameter("zhuti"); util.deletedata(zhuti); out.print("<script language='javaScript'> alert('删除成功');</script>"); response.setHeader("refresh", "0;url=delete.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>
修改
updata.jsp
updatajudge1.jsp
updatajudge2.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="updatajudge1.jsp" method="get"> <p style="text-align:center;color: black; font-family: 宋体; font-size: 20px"> <br> 输入要修改的活动主题 <input type="text" name="zhuti" /><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>
<%@page import="java.sql.ResultSet"%> <%@page import="java.sql.PreparedStatement"%> <%@page import="java.sql.Connection"%> <%@ 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> <body> <jsp:useBean id="util" class="jdbc.tools" scope="page" /> <% String zhuti = request.getParameter("zhuti"); session.setAttribute("zhuti",zhuti); if(!util.isSame(zhuti)){ out.print("<script language='javaScript'> alert('该活动主题不存在'); window.history.back(-1); </script>"); } %> <table border="1"style="text-align:center;"> <tr> <td align = "center">活动主题</td> <td align = "center">活动目的</td> <td align = "center">活动类型</td> <td align = "center">活动时间</td> <td align = "center">活动地点</td> <td align = "center">活动对象</td> <td align = "center">活动内容</td> <td align = "center">活动日程安排</td> </tr> <% Connection conn = util.getConnection(); PreparedStatement pstmt = null; ResultSet rs = null; String sql = "select * from huodong"; pstmt = conn.prepareStatement(sql); rs = pstmt.executeQuery(); while(rs.next()){ if(zhuti.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"><%=rs.getObject(6) %></td> <td align = "center"><%=rs.getObject(7) %></td> <td align = "center"><%=rs.getObject(8) %></td> </tr> <% } } %> </table> <form action="updatajudge2.jsp" method="get"> <p style="text-align:center;color: black; font-family: 宋体; font-size: 20px"> <br> <br>活动目的<input type = "text" name = "mudi" maxlength = "50"/><br> <br>活动类型 <input type="radio" name="type" value="社团竞赛" />社团竞赛 <input type="radio" name="type" value="野外采风" />野外采风 <input type="radio" name="type" value="校内集会" />校内集会 <input type="radio" name="type" value="社团纳新" />社团纳新 <br> <br>活动时间<input type = "text" name = "time" /><br> <br>活动地点<input type = "text" name = "address" /><br> <br><input type="checkbox" name="people" >社团成员 <input type="checkbox" name="people" >全体学生 <br> <br>活动内容<input type = "text" name = "content" maxlength = "500" /><br> <br>活动日程安排<input type = "text" name = "plan" maxlength = "500" /><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>
<%@ 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> <body> <jsp:useBean id="util" class="jdbc.tools" scope="page" /> <% String zhuti = (String)session.getAttribute("zhuti"); String mudi = request.getParameter("mudi"); String type = request.getParameter("type"); String time = request.getParameter("time"); String address = request.getParameter("address"); String people = request.getParameter("people"); String content = request.getParameter("content"); String plan = request.getParameter("plan"); util.updata(zhuti, mudi, type, time, address, people, content, plan); out.print("<script language='javaScript'> alert('修改成功');</script>"); response.setHeader("refresh", "0;url=updata.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
searchjudge.jsp
searchAll.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"></p> 输入要查询的活动主题/活动时间/活动类型/活动地点(支持模糊查询) <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> </form> </body> </html>
<%@page import="java.sql.ResultSet"%> <%@page import="java.sql.PreparedStatement"%> <%@page import="java.sql.Connection"%> <%@ 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> <body> <jsp:useBean id="util" class="jdbc.tools" scope="page" /> <% String data = request.getParameter("data"); %> <table border="1"style="text-align:center;"> <tr> <td align = "center">活动主题</td> <td align = "center">活动类型</td> <td align = "center">活动时间</td> <td align = "center">活动对象</td> </tr> <% int i = 0; Connection conn = util.getConnection(); PreparedStatement pstmt = null; ResultSet rs = null; String sql = "select * from huodong where 活动主题 like? or 活动时间 like ? or 活动类型 like ? or 活动地点 like ?"; pstmt = conn.prepareStatement(sql); pstmt.setString(1, "%" + data + "%"); pstmt.setString(2, "%" + data + "%"); pstmt.setString(3, "%" + data + "%"); pstmt.setString(4, "%" + data + "%"); rs = pstmt.executeQuery(); while(rs.next()){ i++; %> <tr> <td align = "center"><a style="color:blue" href='searchAll.jsp?zhuti=<%=rs.getObject(1) %>'><%=rs.getObject(1) %></a></td> <td align = "center"><%=rs.getObject(3) %></td> <td align = "center"><%=rs.getObject(4) %></td> <td align = "center"><%=rs.getObject(6) %></td> </tr> <% } %> </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>
<%@page import="java.sql.ResultSet"%> <%@page import="java.sql.PreparedStatement"%> <%@page import="java.sql.Connection"%> <%@ 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> <body> <jsp:useBean id="util" class="jdbc.tools" scope="page" /> <table border="1"style="text-align:center;"> <tr> <td align = "center">活动主题</td> <td align = "center">活动目的</td> <td align = "center">活动类型</td> <td align = "center">活动时间</td> <td align = "center">活动地点</td> <td align = "center">活动对象</td> <td align = "center">活动内容</td> <td align = "center">活动日程安排</td> </tr> <% String zhuti = request.getParameter("zhuti"); Connection conn = util.getConnection(); PreparedStatement pstmt = null; ResultSet rs = null; String sql = "select * from huodong"; pstmt = conn.prepareStatement(sql); rs = pstmt.executeQuery(); while(rs.next()){ if(zhuti.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"><%=rs.getObject(6) %></td> <td align = "center"><%=rs.getObject(7) %></td> <td align = "center"><%=rs.getObject(8) %></td> </tr> <% } } %> </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>
<%@page import="java.sql.ResultSet"%> <%@page import="java.sql.PreparedStatement"%> <%@page import="java.sql.Connection"%> <%@ 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="util" class="jdbc.tools" scope="page" /> <table border="1"style="text-align:center;"> <tr> <td align = "center">活动主题</td> <td align = "center">活动类型</td> <td align = "center">活动时间</td> <td align = "center">活动对象</td> </tr> <% Connection conn = util.getConnection(); PreparedStatement pstmt = null; ResultSet rs = null; String sql = "select * from huodong"; pstmt = conn.prepareStatement(sql); rs = pstmt.executeQuery(); while(rs.next()){ %> <tr> <td align = "center"><a style="color:blue" href='showAll.jsp?zhuti=<%=rs.getObject(1) %>'><%=rs.getObject(1) %></a></td> <td align = "center"><%=rs.getObject(3) %></td> <td align = "center"><%=rs.getObject(4) %></td> <td align = "center"><%=rs.getObject(6) %></td> </tr> <% } %> </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>
<%@page import="java.sql.ResultSet"%> <%@page import="java.sql.PreparedStatement"%> <%@page import="java.sql.Connection"%> <%@ 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> <body> <jsp:useBean id="util" class="jdbc.tools" scope="page" /> <table border="1"style="text-align:center;"> <tr> <td align = "center">活动主题</td> <td align = "center">活动目的</td> <td align = "center">活动类型</td> <td align = "center">活动时间</td> <td align = "center">活动地点</td> <td align = "center">活动对象</td> <td align = "center">活动内容</td> <td align = "center">活动日程安排</td> </tr> <% String zhuti = request.getParameter("zhuti"); Connection conn = util.getConnection(); PreparedStatement pstmt = null; ResultSet rs = null; String sql = "select * from huodong"; pstmt = conn.prepareStatement(sql); rs = pstmt.executeQuery(); while(rs.next()){ if(zhuti.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"><%=rs.getObject(6) %></td> <td align = "center"><%=rs.getObject(7) %></td> <td align = "center"><%=rs.getObject(8) %></td> </tr> <% } } %> </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>标签:return,String,期中考试,setString,活动,public,pstmt From: https://www.cnblogs.com/baijingyun/p/16855631.html