当前位置: 首页 > >

JAVA 用于练手的超简单登录注册,jdbc实现【学*笔记】

发布时间:

简单的不能再简单的登录注册,页面简陋,原理在就好!


需要用到的表:



字段其实用不了这么多,为了方便我就这么弄了。


然后jsp页面:


login.jsp



success.jsp


就一句登录成功就OK。


fail.jsp



register.jsp



接下来:


第一步:将字段取出并封装,我们称之为实体类。



package com.tf.entity;

public class User {
private int userId;
private String username;
private int sex;
private String password;
private int flag;

public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getFlag() {
return flag;
}
public void setFlag(int flag) {
this.flag = flag;
}
}


第二步:写dao里面操作数据的方法,顺手将工具类放进util包里。



package com.tf.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.tf.entity.User;
import com.tf.util.DBConnectionUtil;

public class UserDao {
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;

//通过username查询用户(注册前查询是否已有这个username)
public User getUserByUsername(String username){
User user =null;
try {
conn=DBConnectionUtil.getConnection();
String sql="select * from user where username=?";
ps=conn.prepareStatement(sql);
System.out.println(sql);
ps.setString(1, username);
rs=ps.executeQuery();
if(rs.next()){
user=new User();
user.setUserId(rs.getInt("userId"));
user.setUsername(rs.getString(2));
user.setPassword(rs.getString(3));
user.setSex(rs.getInt(4));
user.setFlag(rs.getInt(5));
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBConnectionUtil.closeAll(ps, conn, rs);
}
return user;
}
//通过username与password查询用户(查询到即可登录)
public User getUserByUsernameAndPassword(String username,String password){
User user =null;
try {
conn=DBConnectionUtil.getConnection();
String sql="select * from user where username=? and password=?";
ps=conn.prepareStatement(sql);
System.out.println(sql);
ps.setString(1, username);
ps.setString(2, password);
rs=ps.executeQuery();
if(rs.next()){
user=new User();
user.setUserId(rs.getInt("userId"));
user.setUsername(rs.getString(2));
user.setPassword(rs.getString(3));
user.setSex(rs.getInt(4));
user.setFlag(rs.getInt(5));
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBConnectionUtil.closeAll(ps, conn, rs);
}
return user;
}
//增加(注册进去)
public boolean saveUser(User user){
boolean flag=false;
try {
conn=DBConnectionUtil.getConnection();
String sql="insert into user(username,password) values(?,?)";
ps=conn.prepareStatement(sql);
System.out.println(sql);
ps.setString(1, user.getUsername());
ps.setString(2, user.getPassword());
int rows=ps.executeUpdate();
if(rows>0)
flag=true;
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBConnectionUtil.closeAll(ps, conn, rs);
}
return flag;
}
}


第三步:写处理业务逻辑的service。



package com.tf.service;
import com.tf.dao.UserDao;
import com.tf.entity.User;

public class UserService {
private UserDao userDao=new UserDao();

public User getUserByUsernameAndPassword(String username,String password){
return userDao.getUserByUsernameAndPassword(username, password);
}

public boolean saveUser(User user){
return userDao.saveUser(user);
}

public User getUserByUsername(String username){
return userDao.getUserByUsername(username);
}
}


第四步:写servlet。


登录:



package com.tf.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.tf.entity.User;
import com.tf.service.UserService;

public class LoginServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setCharacterEncoding("UTF-8");
String username=request.getParameter("username");
String password=request.getParameter("password");
System.out.println(username);
System.out.println(password);

UserService userService=new UserService();
User user=userService.getUserByUsernameAndPassword(username, password);
request.setAttribute("user", user);
if (user!=null) {
//请求转发
request.getRequestDispatcher("/success.jsp").forward(request, response);
//请求重定向
//response.sendRedirect("success.jsp");
return;
} else {
request.getRequestDispatcher("/fail.jsp").forward(request, response);
return;
}
}
}


注册:



package com.tf.web.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.tf.entity.User;
import com.tf.service.UserService;

public class RegisterServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setCharacterEncoding("UTF-8");
String username=request.getParameter("username");
String password=request.getParameter("password");
System.out.println(username);
System.out.println(password);

User user = new User();
user.setUsername(username);
user.setPassword(password);
UserService userService=new UserService();
if(userService.getUserByUsername(username)!=null){
request.getRequestDispatcher("/register.jsp").forward(request, response);
return;
}else{
request.getRequestDispatcher("/login.jsp").forward(request, response);
return;
}
}
}


工具类:


package com.tf.util;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

public class DBConnectionUtil {
//加载连接数据库的驱动
//(首先加入jdbc驱动,将解压后的jar包放入lib文件夹中)
private static String jdbcDriver="";
//客户端的IP地址
private static String jdbcURL="";
//客户端用户名
private static String jdbcUser="";
//客户端密码
private static String jdbcPassword="";

static{
InputStream in=
DBConnectionUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
Properties p=new Properties();
try {
p.load(in);
jdbcDriver=p.getProperty("jdbcDriver");
jdbcURL=p.getProperty("jdbcURL");
jdbcUser=p.getProperty("jdbcUser");
jdbcPassword=p.getProperty("jdbcPassword");
} catch (IOException e) {
e.printStackTrace();
}
}

public static Connection getConnection(){
Connection conn=null;
try {
Class.forName(jdbcDriver);
conn=DriverManager.getConnection(jdbcURL, jdbcUser, jdbcPassword);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}


public static void closeAll(PreparedStatement ps,Connection conn,ResultSet rs){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}finally{
if(ps!=null){
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}finally{
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
}
}
}





友情链接: