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

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

回顾下以前学过的知识这个小项目我一共花了五天的时间才把它的功能给完成。

eclipseWeb项目结构

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

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

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页面(我做的是一个记事管理系统,页面截图如下)

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

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

login.jsp页面代码

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%@ page import="java.sql.*"%>

 

        登录与注册 
        
        
        

          
        
        
        

        
        


    
        

记事管理系统(Login)

+

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

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

register.jsp页面代码

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%@ page import="java.sql.*"%>

 

        登录与注册 
        
        
        

          
        
        
        

        
        


    
        

记事管理系统(Register)

+

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

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

index.jsp页面代码

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




左侧导航








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

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

UserInformation.jsp页面代码

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<%@ page import="java.sql.*" %>
<%@ page import="javax.servlet.http.HttpServletRequest" %>
<%@ page import="javax.servlet.http.HttpSession" %>



		用户详情
		
		
	
	
		
用户名:
密 码:
密 保:

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

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

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

查询事件信息原型图

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

QueryEvents.jsp页面代码

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>




		查询事件
		
		
	
	
	
		

查询事件

编号 标题 事件内容 相关人员 事件类型 重要级别 记录时间 事件发生时间 操作
${l.id} ${l.title} ${l.eventcontent} ${l.personnel} ${l.eventtype} ${l.evenlevel} ${l.eventtime} ${l.timeOfOccurrence}

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

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

updatee.jsp页面代码

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>




		添加事件
		
		
	
	
	
		

添加事件

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

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

AddEvent.jsp页面代码

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>




		添加事件
		
		
	
	
	
		

添加事件

以上就是前端所有代码了,还有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{
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
		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 block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.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{
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			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

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

 一起学习

嗯嗯嗯......终于到了激动人心的时候了,兄弟扫描下面这个小码,我来帮你搞定一切,帝王般的服务你值得拥有,免费的哟,记得备注下:csdn来源比特星球,微信:BitPlanet

最后附上

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

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

你可能感兴趣的:(Java后端学习,web,sqlserver,java)