SQL登录注册练习

/class User

package com.neusoft.bean;

public class User {
   private  int password;
   private  String name;

	public User() {
		super();
		// TODO Auto-generated constructor stub
	}
	public User(int password, String name) {
		super();
		this.password = password;
		this.name = name;
	}

	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getPassword() {
		return password;
	}
	public void setPassword(int password) {
		this.password = password;
	}
	@Override
	public String toString() {
		return "password=" + password + ", name=" + name;
	}
   
	
}

  /class LoginDao

package com.neusoft.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.neusoft.bean.User;
import com.neusoft.bean.UserRegist;
import com.neusoft.utils.JDBCUtil;

public class LoginDao {
     public User getUser(String name,String password){
    	 User user=null;    	
    	 PreparedStatement ps=null;
    	 ResultSet rs =null;
    	 Connection conn=JDBCUtil.getConnection();
    	 String sql="select * from t_user where password= ? and name =?";
    	 try {
    		  ps = conn.prepareStatement(sql);
              ps.setString(1, password);
              ps.setString(2, name);
              rs = ps.executeQuery();
			if (rs.next()) {
				String username = rs.getString("name");
				int password2 = rs.getInt("password");
				user=new User(password2,username);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {			
			JDBCUtil.close(ps, rs, conn);
		}
		return user;
    	 
     }
     public void Add(String name,String password,String email){

    	 PreparedStatement ps=null;
    	 Connection conn=JDBCUtil.getConnection();
    	 String sql="insert into t_user values(?,?,?,?)";
    	 try {
    		  ps = conn.prepareStatement(sql);
              ps.setString(1, null);              
              ps.setString(2, name);
              ps.setString(3, password);
              ps.setString(4, email);
               ps.executeUpdate();
			System.out.println(ps.toString());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {			
			
			if (ps !=null) {
				try {
					ps.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (conn !=null) {
				 try {
					conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}	 
     }
     public void Update(String name,String password,String email){

    	 PreparedStatement ps=null;
    	 Connection conn=JDBCUtil.getConnection();
    	 String sql="update t_user set name=?,pasword=?,mail=? where id=?";
    	 try {
    		  ps = conn.prepareStatement(sql);
              ps.setString(1, null);              
              ps.setString(2, name);
              ps.setString(3, password);
              ps.setString(4, email);
               ps.executeUpdate();
			System.out.println(ps.toString());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {			
			
			if (ps !=null) {
				try {
					ps.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (conn !=null) {
				 try {
					conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}	 
     }
}

  /class LoginServlet

package com.neusoft.servlet;

import java.io.IOException;
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 com.neusoft.bean.User;
import com.neusoft.dao.LoginDao;

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L; 
	
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String name = request.getParameter("username");
		String psd = request.getParameter("pwd");
		User user=new LoginDao().getUser(name, psd);
		if (user !=null) {
			List<User>List=new ArrayList<User>();
			List.add(user);
			request.setAttribute("stuList", List);
			//response.sendRedirect("Login-yes.html");//重定向
			request.getRequestDispatcher("/login-success.jsp").forward(request, response);//转发
		}else {
			request.getRequestDispatcher("/Login-no.html").forward(request, response);//转发
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doGet(request, response);
//		request.setCharacterEncoding("utf-8");//第一次传参之前设置编码格式
//		response.setContentType("text/html;charset=utf-8");
//		response.setHeader("Content-Type", "text/html;charset=utf-8");
//		response.getWriter().println("");
	}

}

  /class RegistServlet

package com.neusoft.servlet;

import java.io.IOException;
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 com.neusoft.bean.User;
import com.neusoft.dao.LoginDao;
@WebServlet("/RegistServlet")
public class RegistServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

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

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String name = request.getParameter("username");
		String psd = request.getParameter("pwd");
		String psd2 = request.getParameter("pwd2");
		String email = request.getParameter("email");
		User user=new LoginDao().getUser(name, psd);
		if (user != null) {
			request.getRequestDispatcher("/Regist-error.html").forward(request, response);//转发
		}else if (psd2.equals(psd)) {
			response.setHeader("Content-Type", "text/html;charset=utf-8");
			new LoginDao().Add( name, psd, email);			
			response.sendRedirect("/Regist-success.html");//重定向
		}
		
	}

}

  /class JDBCUtil

package com.neusoft.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCUtil {
	private static String driver="com.mysql.jdbc.Driver";
	private static String url="jdbc:mysql://localhost:3306/demo";
	private static String username="root";
	private static String password="123456";
	static{
		
		try {
			Class.forName(driver);			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	public static Connection getConnection(){
			try {
				return DriverManager.getConnection(url, username, password);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				return null;
			}
		}
	
	public static void close(Statement st,ResultSet rs,Connection conn){
		if (conn !=null) {
			 try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (rs !=null) {
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (st !=null) {
			try {
				st.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}

  Login.html

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <meta charset="UTF-8">
 5 <title>Insert title here</title>
 6 </head>
 7 <body>
 8    <form action="/TestLogin/LoginServlet" method="get">
 9                    用户名:<input type="text" name="username"/>
10                    密码:<input type="password" name="pwd"/>
11        <input type="submit" value="提交"/>
12    </form>
13    <a href="Regist.html">去注册</a>
14 </body>
15 </html>
View Code

/login-success.jsp

 1 <%@page import="com.neusoft.bean.User"%>
 2 <%@page import="java.util.ArrayList"%>
 3 <%@page import="java.util.List"%>
 4 <%@ page language="java" contentType="text/html; charset=UTF-8"
 5     pageEncoding="UTF-8"%>
 6 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 7 <html>
 8 <head>
 9 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
10 <title>Insert title here</title>
11 </head>
12 <body>
13    <table border="1" align="center" width="50%">
14       <tr>
15          <th>姓名 </th>
16          <th>密码 </th>
17          <th colspan="2"> </th>
18       </tr>
19       <% 
20       List<User>stuList=new ArrayList<User>();
21       stuList=(List<User>)request.getAttribute("stuList");
22       for(int i=0;i<stuList.size();i++){
23           User user=stuList.get(i);
24           %>
25             <tr>
26              <td><%=user.getName() %></td>
27              <td><%=user.getPassword() %></td>
28              <td><a href="#">修改</a></td>
29              <td><a href="#">删除</a></td>
30             </tr>
31           <%
32          }
33       %>
34       
35    </table>
36 </body>
37 </html>
View Code

Regist.html

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <meta charset="UTF-8">
 5 <title>Insert title here</title>
 6 </head>
 7 <body>
 8    <form action="/TestLogin/RegistServlet" method="post">
 9                    用户名:<input type="text" name="username"/><br>
10                    密码:<input type="password" name="pwd"/><br>
11                     确认密码:<input type="password" name="pwd2"/><br>
12                      邮箱:<input type="text" name="email"/><br>
13        <input type="submit" value="提交"/>
14    </form>
15 </body>
16 </html>
View Code

其他的就不传了,就一行代码,还有就是导包

这次是在这

转载于:https://www.cnblogs.com/lc-java/p/7400149.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值