⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 userdao.java

📁 北大青鸟的租房网练习文件
💻 JAVA
字号:
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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -