> 文档中心 > eclipse+jsp+servlet+sqlserver+tomcat实现增删改查的一个小项目(超详细教程)

eclipse+jsp+servlet+sqlserver+tomcat实现增删改查的一个小项目(超详细教程)

哈喽,大家好,我是冷风,,拖延了一天,实在是太抱歉(虽然也没多少人看我的博客,哈哈)

这个小项目我一共花了五天的时间才把它的功能给完成,因为个人原因,学过的知识已经丢了两年,不过我会在最短的时间学习到足以通过8k的java程序员面试。

eclipseWeb项目结构

第一步下载需要使用的工具

eclipse( 版本为2019-06 (4.12.0)),下载链接Eclipse Downloads | The Eclipse Foundation

tomcat(版本为9.0),下载链接Apache Tomcat® - Apache Tomcat 9 Software Downloads

jdk(版本为jre1.8.0_152),下载链接Java Downloads | Oracle

sqlserver(版本为2019),下载链接SQL Server 下载 | Microsoft

eclipse为开发工具,jdk为java的开发环境以及运行环境,sqlserver为数据库,tomcat为服务器

第二步设置以及配置

将jdk下载安装后,进行环境变量的配置(不配置的话eclipse打不开的哦),sqlserver数据库下载后创建个用户用来登陆,tomcat下载好后导入eclipse里就行了。

第三步开始使用css,html,js,jquey,bootstrap画web页面(我做的是一个记事管理系统,页面截图如下)

登陆页面(不足的功能:登陆要做一个验证图形码,以及还有忘记密码按钮(使用密保来修改密码),登录按钮配色不行)

login.jsp页面代码

  登录与注册                 

记事管理系统(Login)

+
if($("#checklogin").val()=="error"){alert("您的账号或密码有误!请重新输入");$(this).val("qwer");};$("#registration").click(function(){window.location.href="${pageContext.request.contextPath}/register";});

注册页面(不足的功能:密保也需要做出来)

register.jsp页面代码

  登录与注册                 

记事管理系统(Register)

+
$(".login").click(function(){window.location.href="${pageContext.request.contextPath}/login";});if($("#register").val()!=$("#register_a").val()){if($("#register").val()=="OK"){alert("注册成功,点击确认返回登陆页面");window.location.href="${pageContext.request.contextPath}/login";}else{alert("注册失败");}}

输入账号密码登陆进去后的主界面(不足的地方:界面丑)

index.jsp页面代码

左侧导航body{margin:0;padding:0;overflow-x:hidden;}html, body{height:100%;}img{border:none;}*{font-family:'微软雅黑';font-size:12px;color:#626262;}dl,dt,dd{display:block;margin:0;}a{text-decoration:none;}#bg{background-image:url(images/content/dotted.png);}.container{width:100%;height:100%;margin:auto;}/*left*/.leftsidebar_box{width:160px;height:auto !important;overflow:visible !important;position:fixed;height:100% !important;background-color:#3992d0;}.line{height:2px;width:100%;background-image:url(images/left/line_bg.png);background-repeat:repeat-x;}.leftsidebar_box dt{padding-left:40px;padding-right:10px;background-repeat:no-repeat;background-position:10px center;color:#f5f5f5;font-size:14px;position:relative;line-height:48px;cursor:pointer;}.leftsidebar_box dd{background-color:#317eb4;padding-left:40px;}.leftsidebar_box dd a{color:#f5f5f5;line-height:20px;}.leftsidebar_box dt img{position:absolute;right:10px;top:20px;}.system_log dt{background-image:url(images/left/system.png)}.custom dt{background-image:url(images/left/custom.png)}.channel dt{background-image:url(images/left/channel.png)}.app dt{background-image:url(images/left/app.png)}.cloud dt{background-image:url(images/left/cloud.png)}.syetem_management dt{background-image:url(images/left/syetem_management.png)}.source dt{background-image:url(images/left/source.png)}.statistics dt{background-image:url(images/left/statistics.png)}.leftsidebar_box dl dd:last-child{padding-bottom:10px;}if($("#checklogin").val()=="OK"){alert("欢迎使用记事系统!");$(this).val("qwer");}$(".leftsidebar_box dt").css({"background-color":"#3992d0"});$(".leftsidebar_box dt img").attr("src","images/left/select_xl01.png");$(function(){$(".leftsidebar_box dd").hide();$(".leftsidebar_box dt").click(function(){$(".leftsidebar_box dt").css({"background-color":"#3992d0"})$(this).css({"background-color": "#317eb4"});$(this).parent().find('dd').removeClass("menu_chioce");$(".leftsidebar_box dt img").attr("src","images/left/select_xl01.png");$(this).parent().find('img').attr("src","images/left/select_xl.png");$(".menu_chioce").slideUp(); $(this).parent().find('dd').slideToggle();$(this).parent().find('dd').addClass("menu_chioce");});})  function qiehuan(obj){  if($(obj).text()=="查询事件"){  $("#dd").attr("src","${pageContext.request.contextPath}/deleteEven?userid="+$("#userid").val());  }else if($(obj).text()=="新增事件"){  $("#dd").attr("src","${pageContext.request.contextPath}/addEvent");  }else if($(obj).text()=="用户信息"){  $("#dd").attr("src","${pageContext.request.contextPath}/user/query?userid="+$("#userid").val());   }    }

查询登陆后用户信息功能(不足的地方:界面丑,以及密码不应该直接显示出来,还有密保应该用多个,不单单是一个,头像没与sqlserver数据库进行交互)

UserInformation.jsp页面代码

用户详情
eclipse+jsp+servlet+sqlserver+tomcat实现增删改查的一个小项目(超详细教程)
用户名:
密 码:
密 保:
$(".update").click(function(){window.location.href="${pageContext.request.contextPath}/user/query?userid="+$("#userid").val()+"&name="+$(".name").val()+"&password="+$(".password").val()+"&secretguard="+$(".secretguard").val()+"&updates=updateuser";});if($("#update").val()!=$("#update_a").val()){if($("#update").val()=="ok"){alert("修改成功!");}else{alert("修改失败!");}}

查询当前登录用户的事件信息(不足的地方:界面丑,以及记录时间与事件发生时间的时间未转换为我们常见的格式,重要事件置顶功能没做,查询栏不应该只有标题的模糊查询,还有很多,大家可以看下我使用Axure RP8画的原型图)

删除的话点击一下按钮这条信息就没了

查询事件信息原型图

QueryEvents.jsp页面代码

查询事件.search{float: left;}

查询事件

<!-- --><!-- -->
<!--
指定 date标记
选择开始时间:
--><!--
选择结束时间:
-->
编号标题事件内容相关人员事件类型重要级别记录时间事件发生时间操作
${l.id}${l.title}${l.eventcontent}${l.personnel}${l.eventtype}${l.evenlevel}${l.eventtime}${l.timeOfOccurrence}
$(".del").click(function(){var eventid = $(this).attr("eventid");window.location.href="deleteEven?eventid="+eventid+"&deleteevent=qwee&userid="+$("#userid").val();});$(".updateevent").click(function(){var eventid = $(this).attr("eventid"); window.location.href="updateEvent?eventid="+eventid;});$(".query").click(function(){window.location.href="deleteEven?title="+$(this).prev().val()+"&userid="+$("#userid").val();});

点击查询事件页面的修改按钮打开修改事件页面(不足的地方:界面丑,添加事件字没改成修改事件,记录时间,发生时间未精确到时分秒,相关人员应用按钮实现,可以点击删除,也可以点击添加。重要级别与事件类型要用下拉框并且用户可以添加自定义)

updatee.jsp页面代码

添加事件.search{}div{}

添加事件

if($("#update").val()!=$("#update_a").val()){if($("#update").val()=="ok"){alert("修改成功!");}}$(".timeOfOccurrence").prev().val(timeConversion($(".timeOfOccurrence").val()));$(".eventtime").prev().val(timeConversion($(".eventtime").val()));function timeConversion(date){var d = new Date(date);var y = d.getFullYear(); var m = d.getMonth() + 1; m = m < 10 ? ('0' + m) : m; var d = d.getDate(); d = d < 10 ? ('0' + d) : d; return y + '-' + m + '-' + d;}

接下来就是添加事件(不足的地方:与修改事件页面差不多)

AddEvent.jsp页面代码

添加事件.search{}div{}

添加事件

if($("#insert").val()!=$("#insert_a").val()){if($("#insert").val()=="ok"){alert("新增成功!");}else{alert("新增失败!");}}

以上就是前端所有代码了,还有js,css,img的包都打包如下

链接:百度网盘 请输入提取码 提取码:r4ze

接下来就是后端(不足的地方:代码太多重复严重影响性能)

jdbc(用来连接数据库)jar包如下

链接:百度网盘 请输入提取码 提取码:adf5

com.accp.pojo

Event.java

package com.accp.pojo;import java.util.Date;public class Event {private int id;private String  title;private String  eventcontent;private String  personnel;private String  eventtype;private String  evenlevel;private Date  eventtime;private Date  timeOfOccurrence;private int userid;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String getEventcontent() {return eventcontent;}public void setEventcontent(String eventcontent) {this.eventcontent = eventcontent;}public String getPersonnel() {return personnel;}public void setPersonnel(String personnel) {this.personnel = personnel;}public String getEventtype() {return eventtype;}public void setEventtype(String eventtype) {this.eventtype = eventtype;}public String getEvenlevel() {return evenlevel;}public void setEvenlevel(String evenlevel) {this.evenlevel = evenlevel;}public Date getEventtime() {return eventtime;}public void setEventtime(Date eventtime) {this.eventtime = eventtime;}public Date getTimeOfOccurrence() {return timeOfOccurrence;}public void setTimeOfOccurrence(Date timeOfOccurrence) {this.timeOfOccurrence = timeOfOccurrence;}public int getUserid() {return userid;}public void setUserid(int userid) {this.userid = userid;}}

com.accp.test

CheckLogin.java

package com.accp.test;import javax.servlet.RequestDispatcher;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 javax.servlet.http.HttpSession;import java.io.IOException;import java.sql.*;@WebServlet("/user/userlogin")public class CheckLogin extends HttpServlet {    @Override    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp);    }    @Override    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {    HttpSession session =req.getSession();;    req.setCharacterEncoding("UTF-8"); String name = req.getParameter("username"); String password = req.getParameter("password"); RequestDispatcher dis =null; System.out.println(name+password); try {     try {     Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); String URL = "jdbc:sqlserver://localhost\\SQLEXPRESS:1433;DatabaseName=Demo"; Connection comm = DriverManager.getConnection(URL, "sa", "123456");//userName是你数据库的用户名如sa,  String SQL = "SELECT * FROM [users] where(name='"+name+"' and password ='"+password+"');";//注意:表名一定要带[]  //结果集  PreparedStatement pstmt = comm.prepareStatement(SQL);  ResultSet rs = pstmt.executeQuery();  if(rs.next()){      System.out.println(rs.getInt("id"));      session.setAttribute("userid", rs.getInt("id"));      if(rs!=null){   rs.close();      }      if(pstmt!=null){   pstmt.close();      }      if(comm!=null){   comm.close();      }//      dis = req.getRequestDispatcher("http://localhost:8080/javaWeb_war_exploded/hello.jsp");//      dis.forward(req,resp);      session.setAttribute("checklogin", "OK");      resp.sendRedirect("http://localhost:8080/Record/index");      System.out.println("hah");  }else{      if(rs!=null){   rs.close();      }      if(pstmt!=null){   pstmt.close();      }      if(comm!=null){   comm.close();      }//      dis = req.getRequestDispatcher("http://localhost:8080/javaWeb_war_exploded/fruit.jsp");//      dis.forward(req,resp);      session.setAttribute("checklogin", "error");      resp.sendRedirect("http://localhost:8080/Record/login");  }     } catch (ClassNotFoundException e) {  e.printStackTrace();     } } catch (Exception e) {     e.printStackTrace(); }    }}

DeleteEvent.Java

package com.accp.test;import java.io.IOException;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.List;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 javax.servlet.http.HttpSession;import com.accp.pojo.Event;@WebServlet("/deleteEven")public class DeleteEvent extends HttpServlet{  @Override    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp);    }  @Override    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {    req.setCharacterEncoding("UTF-8"); String id = req.getParameter("eventid"); String userid = req.getParameter("userid"); System.out.println("eventid为"+id); String title = req.getParameter("title"); try {     try {     HttpSession session = req.getSession();     Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); String URL = "jdbc:sqlserver://localhost\\SQLEXPRESS:1433;DatabaseName=Demo"; Connection comm = DriverManager.getConnection(URL, "sa", "123456");//userName是你数据库的用户名如sa,  String SQL = "delete from event where id="+id;//注意:表名一定要带[]  //结果集 PreparedStatement pstmt = comm.prepareStatement(SQL);  int rs = pstmt.executeUpdate();  if(rs>0) {  String SQLl = "select * from event where userid="+userid;  PreparedStatement pstmtq = comm.prepareStatement(SQLl);  ResultSet rss = pstmtq.executeQuery();  List list = new ArrayList();  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  while(rss.next()) {  Event e = new Event();  e.setId(Integer.parseInt(rss.getString("id")));  e.setTitle(rss.getString("title"));  e.setEventcontent(rss.getString("eventcontent"));  e.setPersonnel(rss.getString("personnel"));  e.setEventtype(rss.getString("eventtype"));  e.setEvenlevel(rss.getString("evenlevel"));  e.setEventtime(formatter.parse(rss.getString("eventtime")));  e.setTimeOfOccurrence(formatter.parse(rss.getString("timeOfOccurrence")));  list.add(e);  }  session.setAttribute("list",list);    }else {  String SQLl = "select * from event where userid="+userid;  if(title!=null&&id==null) {  SQLl +=" and title like '%"+title+"%'";  }  PreparedStatement pstmtt = comm.prepareStatement(SQLl);  ResultSet rss = pstmtt.executeQuery();  List list = new ArrayList();  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  while(rss.next()) {    Event e = new Event();  e.setId(Integer.parseInt(rss.getString("id")));  e.setTitle(rss.getString("title"));  e.setEventcontent(rss.getString("eventcontent"));  e.setPersonnel(rss.getString("personnel"));  e.setEventtype(rss.getString("eventtype"));  e.setEvenlevel(rss.getString("evenlevel"));  e.setEventtime(formatter.parse(rss.getString("eventtime")));  e.setTimeOfOccurrence(formatter.parse(rss.getString("timeOfOccurrence")));  list.add(e);  }  session.setAttribute("list",list);  }   req.getRequestDispatcher("/queryEvents").forward(req, resp);      } catch (ClassNotFoundException e) {  e.printStackTrace();     } } catch (Exception e) {     e.printStackTrace(); }    }}

Insert.java

package com.accp.test;import java.io.IOException;import java.sql.Connection;import java.sql.Date;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.SQLException;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 javax.servlet.http.HttpSession;@WebServlet("/insert")public class Insert extends HttpServlet{@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// TODO Auto-generated method stubdoPost(req, resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// TODO Auto-generated method stubtry {req.setCharacterEncoding("UTF-8");String userid = req.getParameter("userid");System.out.println("userid为"+userid);String title =req.getParameter("title");String eventcontent =req.getParameter("eventcontent");String personnel =req.getParameter("personnel");String eventtype =req.getParameter("eventtype");String evenlevel =req.getParameter("evenlevel");String eventtime = req.getParameter("eventtime");String timeOfOccurrence = req.getParameter("timeOfOccurrence");Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");String URL = "jdbc:sqlserver://localhost\\SQLEXPRESS:1433;DatabaseName=Demo";Connection comm = DriverManager.getConnection(URL, "sa", "123456");//userName是你数据库的用户名如sa, String SQL = "insert into event(title,eventcontent,personnel,eventtype,evenlevel,eventtime,timeOfOccurrence,userid) values('"+title+"','"+eventcontent+"','"+personnel+"','"+eventtype+"','"+evenlevel+"','"+eventtime+"','"+timeOfOccurrence+"','"+userid+"')";      //结果集 PreparedStatement pstmt = comm.prepareStatement(SQL); int rs = pstmt.executeUpdate(); if(rs>0) { req.setAttribute("insert","ok"); req.getRequestDispatcher("/addEvent").forward(req, resp); } if(pstmt!=null){     pstmt.close(); } if(comm!=null){     comm.close(); }} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}

QueryEvent.java

package com.accp.test;import java.io.IOException;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.text.ParsePosition;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.List;import javax.servlet.RequestDispatcher;import javax.servlet.ServletException;import javax.servlet.ServletRequest;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import com.accp.pojo.Event;@WebServlet("/queryevent")public class QueryEvent extends HttpServlet{  @Override    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp);    }  @Override    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {  HttpSession session =req.getSession();    req.setCharacterEncoding("UTF-8"); String id = req.getParameter("userid"); String deleteevent = req.getParameter("deleteevent"); System.out.println("userid为"+id); System.out.println("deleteevent为"+deleteevent); try {     try {   Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); String URL = "jdbc:sqlserver://localhost\\SQLEXPRESS:1433;DatabaseName=Demo"; Connection comm = DriverManager.getConnection(URL, "sa", "123456");//userName是你数据库的用户名如sa,  PreparedStatement pstmt=null;    String SQL = "select * from event where userid="+id;//注意:表名一定要带[]  //结果集  pstmt = comm.prepareStatement(SQL);  ResultSet rs = pstmt.executeQuery();  List list = new ArrayList();  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  while(rs.next()) {    Event e = new Event();  e.setId(Integer.parseInt(rs.getString("id")));  e.setTitle(rs.getString("title"));  e.setEventcontent(rs.getString("eventcontent"));  e.setPersonnel(rs.getString("personnel"));  e.setEventtype(rs.getString("eventtype"));  e.setEvenlevel(rs.getString("evenlevel"));  e.setEventtime(formatter.parse(rs.getString("eventtime")));  e.setTimeOfOccurrence(formatter.parse(rs.getString("timeOfOccurrence")));  list.add(e);  }  req.setAttribute("list",list);   if(deleteevent!=null) {    if(deleteevent.equals("qwee")) {    System.out.println("删除");    }String eventid = req.getParameter("eventid");    System.out.println("eventid为"+eventid);    String delete = "delete from event where id="+eventid;//注意:表名一定要带[]  //结果集  pstmt = comm.prepareStatement(delete);  int rss = pstmt.executeUpdate();  if(rss>0) {  //结果集  req.setAttribute("deleteevent",null);  }    }    if(pstmt!=null){  pstmt.close();     }      if(comm!=null){  comm.close();     }     if(rs!=null) {     rs.close();     } req.getRequestDispatcher("/queryEvents").forward(req, resp);     } catch (ClassNotFoundException e) {  e.printStackTrace();     } } catch (Exception e) {     e.printStackTrace(); }    }}

QueryUser.java

package com.accp.test;import java.io.IOException;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import javax.servlet.RequestDispatcher;import javax.servlet.ServletException;import javax.servlet.ServletRequest;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;@WebServlet("/user/query")public class QueryUser extends HttpServlet{  @Override    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp);    }  @Override    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {  HttpSession session =req.getSession();    req.setCharacterEncoding("UTF-8"); String id = req.getParameter("userid"); String updates = req.getParameter("updates"); try {     try {   Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); String URL = "jdbc:sqlserver://localhost\\SQLEXPRESS:1433;DatabaseName=Demo"; Connection comm = DriverManager.getConnection(URL, "sa", "123456");//userName是你数据库的用户名如sa,  PreparedStatement pstmt=null;  String SQL = "select * from users where id="+id;//注意:表名一定要带[]  //结果集  pstmt = comm.prepareStatement(SQL);  ResultSet rs = pstmt.executeQuery();  if(rs.next()) {  session.setAttribute("username", rs.getString("name"));  session.setAttribute("password", rs.getString("password"));  session.setAttribute("secretguard", rs.getString("secretguard"));  }  System.out.println(req.getParameter("update"));     if(updates!=null) {  if(updates.equals("updateuser")) {     String name = req.getParameter("name");    String password = req.getParameter("password");    String secretguard = req.getParameter("secretguard"); String SQLU = "update users set name='"+name+"',password='"+password+"',secretguard='"+secretguard+"' where id="+id;//注意:表名一定要带[]     //结果集     pstmt = comm.prepareStatement(SQLU);     int rss = pstmt.executeUpdate();     if(rss>0) {      session.setAttribute("username", name);  session.setAttribute("password", password);  session.setAttribute("secretguard", secretguard);     req.setAttribute("update","ok");     }  }else {  req.setAttribute("update","no");  }     }     if(pstmt!=null){   pstmt.close();      }if(comm!=null){   comm.close();      }      if(rs!=null) {      rs.close();      } req.getRequestDispatcher("/UserInformation").forward(req, resp);     } catch (ClassNotFoundException e) {  e.printStackTrace();     } } catch (Exception e) {     e.printStackTrace(); }    }}

Register.java

package com.accp.test;import java.io.IOException;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import javax.servlet.RequestDispatcher;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 javax.servlet.http.HttpSession;@WebServlet("/user/register")public class Register extends HttpServlet{  @Override    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp);    }  @Override    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {    HttpSession session =req.getSession();;    req.setCharacterEncoding("UTF-8"); String name = req.getParameter("username"); String password = req.getParameter("password"); System.out.println(name+password); try {     try {     Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); String URL = "jdbc:sqlserver://localhost\\SQLEXPRESS:1433;DatabaseName=Demo"; Connection comm = DriverManager.getConnection(URL, "sa", "123456");//userName是你数据库的用户名如sa,  String SQL = "insert into users(name,password) values('"+name+"','"+password+"')";//注意:表名一定要带[]  //结果集  PreparedStatement pstmt = comm.prepareStatement(SQL);  int rs = pstmt.executeUpdate();  if(rs>0){      if(pstmt!=null){   pstmt.close();      }      if(comm!=null){   comm.close();      }//      dis = req.getRequestDispatcher("http://localhost:8080/javaWeb_war_exploded/hello.jsp");//      dis.forward(req,resp);      session.setAttribute("register", "OK");      resp.sendRedirect("http://localhost:8080/Record/register");  }else{      if(pstmt!=null){   pstmt.close();      }      if(comm!=null){   comm.close();      }//      dis = req.getRequestDispatcher("http://localhost:8080/javaWeb_war_exploded/fruit.jsp");//      dis.forward(req,resp);      session.setAttribute("register", "error");      resp.sendRedirect("http://localhost:8080/Record/register");  }     } catch (ClassNotFoundException e) {  e.printStackTrace();     } } catch (Exception e) {     e.printStackTrace(); }    }}

UpdateEvent.java

package com.accp.test;import java.io.IOException;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.List;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 javax.servlet.http.HttpSession;import com.accp.pojo.Event;@WebServlet("/updateEvent")public class UpdateEvent extends HttpServlet{  @Override    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp);    }  @Override    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {    req.setCharacterEncoding("UTF-8"); String eventid = req.getParameter("eventid"); System.out.println("eventid为"+eventid); String title = req.getParameter("title");   String eventcontent = req.getParameter("eventcontent");   String personnel = req.getParameter("personnel");   String eventtype = req.getParameter("eventtype");   String evenlevel = req.getParameter("evenlevel");   String eventtime = req.getParameter("eventtime");   String timeOfOccurrence = req.getParameter("timeOfOccurrence");   String first = req.getParameter("first");   System.out.println("修改"+first);   if(first==null) {   first="no";   } try {     try {     HttpSession session = req.getSession();     Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); String URL = "jdbc:sqlserver://localhost\\SQLEXPRESS:1433;DatabaseName=Demo"; Connection comm = DriverManager.getConnection(URL, "sa", "123456");//userName是你数据库的用户名如sa,  if(first.equals("ok")) { String SQL = "update event set title='"+title+"',eventcontent='"+eventcontent+"',personnel='"+personnel+"',eventtype='"+eventtype+"',evenlevel='"+evenlevel+"',eventtime='"+eventtime+"',timeOfOccurrence='"+timeOfOccurrence+"'  where id="+eventid;//注意:表名一定要带[]  //结果集 PreparedStatement pstmt = comm.prepareStatement(SQL);  int rs = pstmt.executeUpdate();  if(rs>0) {  String SQLl = "select * from event where id="+eventid;  PreparedStatement pstmtq = comm.prepareStatement(SQLl);  ResultSet rss = pstmtq.executeQuery();  List list = new ArrayList();  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  while(rss.next()) {  Event e = new Event();  e.setId(Integer.parseInt(rss.getString("id")));  e.setTitle(rss.getString("title"));  e.setEventcontent(rss.getString("eventcontent"));  e.setPersonnel(rss.getString("personnel"));  e.setEventtype(rss.getString("eventtype"));  e.setEvenlevel(rss.getString("evenlevel"));  e.setEventtime(formatter.parse(rss.getString("eventtime")));  e.setTimeOfOccurrence(formatter.parse(rss.getString("timeOfOccurrence")));  list.add(e);  }  session.setAttribute("list",list);  session.setAttribute("updatee", "ok");  }else {  String SQLl = "select * from event where id="+eventid;  PreparedStatement pstmtq = comm.prepareStatement(SQLl);  ResultSet rss = pstmtq.executeQuery();  List list = new ArrayList();  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  while(rss.next()) {  Event e = new Event();  e.setId(Integer.parseInt(rss.getString("id")));  e.setTitle(rss.getString("title"));  e.setEventcontent(rss.getString("eventcontent"));  e.setPersonnel(rss.getString("personnel"));  e.setEventtype(rss.getString("eventtype"));  e.setEvenlevel(rss.getString("evenlevel"));  e.setEventtime(formatter.parse(rss.getString("eventtime")));  e.setTimeOfOccurrence(formatter.parse(rss.getString("timeOfOccurrence")));  list.add(e);  }  session.setAttribute("updatee", "no");  session.setAttribute("list",list);  } }else { String SQLl = "select * from event where id="+eventid;  PreparedStatement pstmtq = comm.prepareStatement(SQLl);  ResultSet rss = pstmtq.executeQuery();  List list = new ArrayList();  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  while(rss.next()) {  Event e = new Event();  e.setId(Integer.parseInt(rss.getString("id")));  e.setTitle(rss.getString("title"));  e.setEventcontent(rss.getString("eventcontent"));  e.setPersonnel(rss.getString("personnel"));  e.setEventtype(rss.getString("eventtype"));  e.setEvenlevel(rss.getString("evenlevel"));  e.setEventtime(formatter.parse(rss.getString("eventtime")));  System.out.println(formatter.parse(rss.getString("eventtime")));  e.setTimeOfOccurrence(formatter.parse(rss.getString("timeOfOccurrence")));  System.out.println(formatter.parse(rss.getString("timeOfOccurrence")));  list.add(e);  }  session.setAttribute("updatee", "no");  session.setAttribute("list",list); }  session.setAttribute("updatee", "no");  req.getRequestDispatcher("/updatee").forward(req, resp);     } catch (ClassNotFoundException e) {  e.printStackTrace();     } } catch (Exception e) {     e.printStackTrace(); }    }}

UpdateUser.java

package com.accp.test;import java.io.IOException;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;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 javax.servlet.http.HttpSession;@WebServlet("/user/update")public class UpdateUser extends HttpServlet{@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// TODO Auto-generated method stubdoPost(req, resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// TODO Auto-generated method stubtry {HttpSession s = req.getSession();req.setCharacterEncoding("UTF-8");String id = req.getParameter("id");String name = req.getParameter("name");String password = req.getParameter("password");String secretguard = req.getParameter("secretguard");Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");    String URL = "jdbc:sqlserver://localhost\\SQLEXPRESS:1433;DatabaseName=Demo";    Connection comm = DriverManager.getConnection(URL, "sa", "123456");//userName是你数据库的用户名如sa,     String SQL = "update users set name='"+name+"',password='"+password+"',secretguard='"+secretguard+"' where id="+id;//注意:表名一定要带[]     //结果集     PreparedStatement pstmt = comm.prepareStatement(SQL);     int rs = pstmt.executeUpdate();     if(rs>0) {     req.setAttribute("update","ok");     }     if(pstmt!=null){  pstmt.close();     }     if(comm!=null){  comm.close();     }} catch (Exception e) {// TODO: handle exception}}}

以上就是后端的所有代码啦

这是整个项目的压缩包

链接:百度网盘 请输入提取码 提取码:zu7k

各位有什么不懂得或者有什么不足的地方请留言或者扣我微信NIKE2022888

这个只是第一个版本,以后的版本一定会做到比这个好十倍不止

 一起学习

嗯嗯嗯......终于到了激动人心的时候了,兄弟扫描下面这个小码,我来帮你搞定一切,帝王般的服务你值得拥有,免费的哟,记得备注下:曹家的小仙女 / 小哥哥,微信:NIKE2022888

最后附上

一寸光阴一寸金,寸金难买寸光阴。请珍惜现在美好的青春,咱们一起努力奋斗,创造美好未来

拜托拜托!!!拜托拜托!!!拜托拜托!!!