userdao.java

来自「北大青鸟的租房网练习文件」· Java 代码 · 共 80 行

JAVA
80
字号
package com.newer.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.newer.common.DBUtil;
import com.newer.common.MD5Code;
import com.newer.entity.UserVO;

public class UserDao {
	//用户注册时将用户信息录入到数据库
	public boolean addUserInfo(UserVO vo){
		boolean res=false;
		Connection conn=DBUtil.getConn();
		PreparedStatement pstmt=null;
		String sql="insert into tbl_user(uname,upass) values(?,?)";
		try {
			MD5Code md5=new MD5Code();
			String pwd=md5.getMD5ofStr(vo.getUpass());
			pstmt=conn.prepareStatement(sql);
			pstmt.setString(1, vo.getUname());
			pstmt.setString(2, pwd);
			int count=pstmt.executeUpdate();
			if(count>0){
				res=true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return res;
	}
	//登陆时检验用户名和密码是否正确
	public boolean checkUser(UserVO vo){
		boolean res=false;
		String sql="select upass from tbl_user where uname=?";
		Connection conn=DBUtil.getConn();
		PreparedStatement pstmt=null;
		try {
			pstmt=conn.prepareStatement(sql);
			pstmt.setString(1, vo.getUname());
			ResultSet rs=pstmt.executeQuery();
			while(rs.next()){
				String pwd=rs.getString("upass");
				MD5Code md5=new MD5Code();
				String userpwd=md5.getMD5ofStr(vo.getUpass());
				if(pwd.equals(userpwd)){
					res=true;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return res;
	}
	public UserVO getUserInfoByName(String uname){
		UserVO vo=new UserVO();
		String sql="select * from tbl_user where uname=?";
		Connection conn=DBUtil.getConn();
		PreparedStatement pstmt=null;
		try {
			pstmt=conn.prepareStatement(sql);
			pstmt.setString(1, uname);
			ResultSet rs=pstmt.executeQuery();
			while(rs.next()){
				vo.setUid(rs.getInt("uid"));
				vo.setUname(rs.getString("uname"));
				vo.setUpass(rs.getString("upass"));
			}
			rs.close();
			pstmt.close();
			conn.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return vo;
	}

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?