javaWeb实现登录注册的增删改查功能

1.包结构

javaWeb实现登录注册的增删改查功能_第1张图片

2.数据库表字段

javaWeb实现登录注册的增删改查功能_第2张图片数据库名:MyDB
表名:acca
字段: id int
name varchar(64)
pwd varchar(64)

3.工具类DBUtil

public class DBUtil {
     
	public  static Connection getConn() {
     
		String url="jdbc:sqlserver://localhost:1433;databaseName=MyDB";
		String user="sa";//数据库登录名
		String pwd="1";//数据库登录密码
		Connection conn=null;
		
		try {
     
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			conn=DriverManager.getConnection(url, user, pwd);
		} catch (ClassNotFoundException e) {
     
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
     
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return conn;
		
	}
	
		public static void closeConn(Connection conn,PreparedStatement ps,ResultSet rs) {
     
			try {
     
				if(conn!=null){
     
				conn.close();
				}
			} catch (SQLException e) {
     
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			try {
     
				if(ps!=null){
     
				ps.close();
				}
			} catch (SQLException e) {
     
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			try {
     
				if(rs!=null){
     
				rs.close();
				}
			} catch (SQLException e) {
     
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}	
	
}

4.1登录

<style type="text/css">
    .input1 {
     
      border: 1px solid #CDC28D; /* 输入框表框颜色*/
      background-color: #F2F9FD; /* 输入框背景颜色*/
      height:40px; /*输入框高度*/
      width: 200px; /*输入框宽度*/
      padding-top: 4px; /*输入框里的文字和输入框上边之间的距离
      font-family: Arial, Helvetica, sans-serif; /*输入框文字类型*/
      font-size: 12px; /*输入框内文字大小*/
      padding-left: 10px; /*输入框里的文字和输入框左边之间的距离*/
   	 }
    .input2 {
     
      border: 1px solid #1E1E1E; /* 输入框表框颜色*/
      background-color: #F2F9FD; /* 输入框背景颜色*/
      height: 30px; /*输入框高度*/
      width: 80px; /*输入框宽度*/
  	  }
  </style>

	<body>
		<form action="index" method="post">
		<table align="center">
			<tr height="150dp">
				<td></td>
			</tr>
			<tr align="center">
			<td colspan="2"><h1>用户登录</h1></td>
			</tr>
			<tr>
				<td>用户名:</td>
				<td><input class="input1" type="text" name="name" /></td>
			</tr>
			<tr>
				<td>密码:</td>
				<td><input class="input1"  type="password" name="pwd" /></td>
			</tr>
			<tr align="center">
				<td colspan="2"><input class="input2"  type="submit" value="登录" /></td>
			</tr>
			<tr align="right">
				<td colspan="2"><a href="register.jsp">注册</a></td>
			</tr>
		</table>
		</form>
	</body>
public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
     
		//执行dopost方法
		doPost(request, response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
     
		//乱码问题的处理
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		//接受前端传递进来的用户名和密码
		String name=request.getParameter("name");
		String pwd=request.getParameter("pwd");
		//数据库的操作
		Connection conn=DBUtil.getConn();
		String sql="select * from acca where name=? and pwd=?";
		PreparedStatement ps=null;
		ResultSet rs=null;
		//定义一个flag,默认为false
		boolean flag=false;
		
		try{
     
		ps=conn.prepareStatement(sql);
		ps.setString(1, name);
		ps.setString(2, pwd);
		rs=ps.executeQuery();
		if(rs.next()){
     
			flag=true;//如果存在
		}else{
     
			flag=false;//如果不存在
		}
		
		}catch (SQLException e) {
     
			e.printStackTrace();
		}finally{
     
			DBUtil.closeConn(conn,ps,rs);
		}
		if(flag){
     //如果存在,则重定向到我们要访问的Servlet
			response.sendRedirect("UserListServlet");
		}else{
     //不存在,则返回登录页
			response.sendRedirect("index.jsp");
		}
	}

4.2注册

<style type="text/css">
    .input1 {
     
      border: 1px solid #CDC28D; /* 输入框表框颜色*/
      background-color: #F2F9FD; /* 输入框背景颜色*/
      height:40px; /*输入框高度*/
      width: 200px; /*输入框宽度*/
      padding-top: 4px; /*输入框里的文字和输入框上边之间的距离
      font-family: Arial, Helvetica, sans-serif; /*输入框文字类型*/
      font-size: 12px; /*输入框内文字大小*/
      padding-left: 10px; /*输入框里的文字和输入框左边之间的距离*/
   	 }
    .input2 {
     
      border: 1px solid #1E1E1E; /* 输入框表框颜色*/
      background-color: #F2F9FD; /* 输入框背景颜色*/
      height: 30px; /*输入框高度*/
      width: 80px; /*输入框宽度*/
  	  }
  	</style>



	<body>
		<form action="register" method="post">
		<table align="center">
			<tr height="150dp">
				<td></td>
			</tr>
			<tr align="center">
			<td colspan="2"><h1>用户注册</h1></td>
			</tr>
			<tr>
				<td>用户名:</td>
				<td><input class="input1" type="text" name="name" /></td>
			</tr>
			<tr>
				<td>密码:</td>
				<td><input class="input1"  type="password" name="pwd" /></td>
			</tr>
			<tr align="center">
				<td colspan="2"><input class="input2"  type="submit" value="注册" /></td>
			</tr>
		</table>
		</form>
	</body>

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
     
		//执行dopost方法
		doPost(request, response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
     

		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		//接收到的注册内容
		String name=request.getParameter("name");
		String pwd=request.getParameter("pwd");
		//判断是否为空,这里可以在前端部分加上正则表达式
		if(name!=null && !"".equals(name) && pwd!=null && !"".equals(pwd)){
     
			
			Connection conn=DBUtil.getConn();
			PreparedStatement ps=null;
			ResultSet rs=null;
			//数据库插入
			String sql="insert into acca (name,pwd) values (?,?)";
			
			try{
     
			ps=conn.prepareStatement(sql);
			ps.setString(1, name);
			ps.setString(2, pwd);
			int r=ps.executeUpdate();
			if(r>0){
     //如果数据库插入成功,则跳转到登录页面
				response.sendRedirect("index.jsp");
			}else{
     //否则注册失败,返回注册页面
				response.sendRedirect("register.jsp");
			}
			}catch (SQLException e) {
     
				e.printStackTrace();
			}finally{
     
				DBUtil.closeConn(conn, ps, rs);
			}
		}else{
     //传入数据为空,注册失败
			response.sendRedirect("register.jsp");
		}
	
	}

4.3查询输出数据库内容

	been类
public class User {
     
	private Integer id;
	private String name;
	private String pwd;
	public Integer getId() {
     
		return id;
	}
	public void setId(Integer id) {
     
		this.id = id;
	}
	public String getName() {
     
		return name;
	}
	public void setName(String name) {
     
		this.name = name;
	}
	public String getPwd() {
     
		return pwd;
	}
	public void setPwd(String pwd) {
     
		this.pwd = pwd;
	}
	
	public User(Integer id,String name,String pwd){
     
		super();
		this.id=id;
		this.name=name;
		this.pwd=pwd;
	}
	public User() {
     
		super();
	}
}

4.1中登录成功后,首先访问一个Servlet,在这个Servlet中将表中的数据查询,再传递到登录成功后的页面输出显示

public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
     

		doPost(request, response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
     

		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		
		Connection conn=DBUtil.getConn();
		PreparedStatement ps=null;
		ResultSet rs=null;
		String sql="select * from acca";
		
		List<User> users=new ArrayList<User>();
		try{
     
		ps=conn.prepareStatement(sql);
		rs=ps.executeQuery();
		while(rs.next()){
     //循环我们查询的表
			User user=new User(rs.getInt(1),rs.getString(2),rs.getString(3));
			users.add(user);//将数据装入users
		}
		}catch (Exception e) {
     
			e.printStackTrace();
		}finally{
     
			DBUtil.closeConn(conn, ps, rs);
		}
		HttpSession session=request.getSession();//建立一个session
		session.setAttribute("userList", users);
		response.sendRedirect("login.jsp");//登录后的页面
	}

}
<body>
		<table border="1" cellspacing="0" align="center" width="1000dp"
			height="500dp">
			<thead>
				<tr>
					<td>
						编号
					</td>
					<td>
						姓名
					</td>
					<td>
						密码
					</td>
					<td>
						操作
					</td>
				</tr>
			</thead>
			<tbody>
				<%
					List<User> users = (List) session.getAttribute("userList");
					for (User user : users) {
     
				%>
				<tr>
					<td><%=user.getId()%></td>
					<td><%=user.getName()%></td>
					<td><%=user.getPwd()%></td>
					<td>	//获取到要删除的数据的id,并将数据传输到一个servlet
						<a href="deleteServlet?id=<%=user.getId() %>">删除</a>
						//获取到要更新的数据的id,并将数据传输到一个servlet
						<a href="updateShowServlet?id=<%=user.getId() %>">更新</a>
					</td>
				</tr>
				<%
					}
				%>
			</tbody>
		</table>
	</body>

显示结果如下,可对页面进行更新或者删除操作
javaWeb实现登录注册的增删改查功能_第3张图片

4.4删除数据

在4.3中,登录成功显示页面,将获取到要删除的数据的id传到servlet

public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
     

		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
     

		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		//接受到将要删除的id
		String id = request.getParameter("id");
		
		Connection conn=DBUtil.getConn();
		PreparedStatement ps=null;
		ResultSet rs=null;
		String sql="delete acca where id = ?";
		
		try{
     
		ps=conn.prepareStatement(sql);
		ps.setString(1, id);
		
		int r=ps.executeUpdate();
		if(r>0){
     //不论删除成功还是失败都会返回到登录成功时的servlet,通过该servlet进入登录成功页面
			response.sendRedirect("UserListServlet");
		}else{
     
			response.sendRedirect("UserListServlet");
		}
		}catch (SQLException e) {
     
			e.printStackTrace();
		}finally{
     
			DBUtil.closeConn(conn, ps, rs);
		}
	}

javaWeb实现登录注册的增删改查功能_第4张图片

4.5数据库更新 修改

4.3中登录成功页面,在点击更新时,页面会先获取到要删除的数据的id,发送给一个servlet进行处理。

public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
     

		doPost(request, response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
     

		response.setContentType("text/html");
		String id = request.getParameter("id");
		//获取要删除数据的id
		Connection conn=DBUtil.getConn();
		String sql="select * from acca where id=?";//查询出该id的所有字段内容
		PreparedStatement ps=null;
		ResultSet rs=null;
			
		try{
     
		ps=conn.prepareStatement(sql);
		ps.setString(1, id);
		rs=ps.executeQuery();
		if(rs.next()){
     
			request.setAttribute("id", rs.getInt(1));
			request.setAttribute("name", rs.getString(2));
			request.setAttribute("pwd", rs.getString(3));
			/*
			*将数据发送到一个jsp页面,在该页面进行人工的数据的修改
			*/
			request.getRequestDispatcher("update.jsp").forward(request, response);
		}
		}catch (SQLException e) {
     
			e.printStackTrace();
		}finally{
     
			DBUtil.closeConn(conn,ps,rs);
		}

	}
<style type="text/css">
    .input1 {
     
      border: 1px solid #CDC28D; /* 输入框表框颜色*/
      background-color: #F2F9FD; /* 输入框背景颜色*/
      height:40px; /*输入框高度*/
      width: 200px; /*输入框宽度*/
      padding-top: 4px; /*输入框里的文字和输入框上边之间的距离
      font-family: Arial, Helvetica, sans-serif; /*输入框文字类型*/
      font-size: 12px; /*输入框内文字大小*/
      padding-left: 10px; /*输入框里的文字和输入框左边之间的距离*/
   	 }
    .input2 {
     
      border: 1px solid #1E1E1E; /* 输入框表框颜色*/
      background-color: #F2F9FD; /* 输入框背景颜色*/
      height: 30px; /*输入框高度*/
      width: 80px; /*输入框宽度*/
  	  }
  </style>
  
  <body>
  	<form action="updateServlet" method="post">
  	<table align="center">
  		<tr height="150dp">
  			<td></td>
  		</tr>
  		<tr>
  			<td colspan="2"><h1>用户更新</h1></td>
  		</tr>
  		<tr>
  			<td>ID:</td>
  			<td><input class="input1" type="text" name="id" readonly="readonly" value="<%=request.getAttribute("id") %>"/></td>
  		</tr>
  		<tr>
  			<td>用户名:</td>
  			<td><input class="input1" type="text" name="name" value="<%=request.getAttribute("name") %>"/></td>
  		</tr>
  		<tr>
  			<td>密码:</td>
  			<td><input class="input1" type="text" name="pwd" value="<%=request.getAttribute("pwd") %>"/></td>
  		</tr>
  		<tr align="center">
  			<td colspan="2"> <input class="input2" type="submit" value="更新"/></td>
  		</tr>
  	</table>
   </form>
  </body>

javaWeb实现登录注册的增删改查功能_第5张图片
在该jsp页面,将要更新的数据进行修改,点击更新按钮后,数据将被发送到一个servlet页面进行处理,处理后会返回到登录成功时的servlet,通过该servlet进入登录成功时的页面。


	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
     

		doPost(request, response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
     

		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		
		String id = request.getParameter("id");
		String name=request.getParameter("name");
		String pwd=request.getParameter("pwd");
		//获取到要更新的数据

		Connection conn=DBUtil.getConn();
		PreparedStatement ps=null;
		ResultSet rs=null;
		//更新语句
		String sql="update acca set name=?, pwd=? where id=?";
		
		try{
     
		ps=conn.prepareStatement(sql);
		ps.setString(1, name);
		ps.setString(2, pwd);
		ps.setInt(3,Integer.parseInt(id));
		int r=ps.executeUpdate();
		if(r>0){
     	//
			response.sendRedirect("UserListServlet");
		}else{
     
			response.sendRedirect("UserListServlet");
		}
		}catch (SQLException e) {
     
			e.printStackTrace();
		}finally{
     
			DBUtil.closeConn(conn, ps, rs);
		}
	}

如有错误的地方,请联系本人指正:QQ1609279469 备注CSDN 谢谢!

你可能感兴趣的:(Java,javaWeb)