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

📄 usersdao.java

📁 Java购物车及JSTL的应用
💻 JAVA
字号:
package org.qhit.li.store.dao;

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

import org.qhit.li.store.dbmade.DBMade;
import org.qhit.li.store.dto.UsersDTO;

public class UsersDAO {
	
	/**
	 * 验证用户登录
	 * @param name
	 * @param psd
	 * @return
	 */
	public int checkUser(String name,String psd){
		int uid=0;
		String sql="select uId from Users where uName=? and uPassword=?";
		Connection con=null;
		PreparedStatement pds=null;
		ResultSet rs=null;
		
		try {
			con=DBMade.getCon();
			pds=DBMade.getPds(con, sql);
			pds.setString(1, name);
			pds.setString(2, psd);
			rs=pds.executeQuery();
			if(rs.next()){
				uid=rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DBMade.close(rs, pds, con);
		}
		
		return uid;
	}
	
	/**
	 * 添加用户
	 * @param info
	 * @return
	 */
	public int insertUser(UsersDTO info){
		int result=0;
		String sql="insert into Users values(?,?,?,?,?,?,?,?,?)";
		Connection con=null;
		PreparedStatement pds=null;
		
		try {
			con=DBMade.getCon();
			pds=DBMade.getPds(con, sql);
			pds.setString(1, info.getUname());
			pds.setString(2, info.getUpassword());
			pds.setString(3, info.getUtname());
			pds.setString(4, info.getUphone());
			pds.setString(5, info.getUemaile());
			pds.setString(6, info.getUzip());
			pds.setString(7, info.getUaddress());
			pds.setString(8, info.getUquestion());
			pds.setString(9, info.getUanswer());
			result=pds.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DBMade.close(null, pds, con);
		}
		
		return result;
	}
	
	/**
	 * 
	 * 查询单个用户信息
	 * @param uid
	 * @return
	 */
	public UsersDTO getUser(int uid) {
		UsersDTO info=null;
		String sql="select  uName,uTname,uPhone,uEmail,uZip,uAddress from Users where uId=?";
		Connection con=null;
		PreparedStatement pds=null;
		ResultSet rs=null;
		
		try {
			con=DBMade.getCon();
			pds=DBMade.getPds(con, sql);
			pds.setInt(1, uid);
			rs=pds.executeQuery();
			if(rs.next()){
				info=new UsersDTO();
				info.setUname(rs.getString(1));
				info.setUtname(rs.getString(2));
				info.setUphone(rs.getString(3));
				info.setUemaile(rs.getString(4));
				info.setUzip(rs.getString(5));
				info.setUaddress(rs.getString(6));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DBMade.close(rs, pds, con);
		}
		return info;
	}
	
	/*修改用户信息*/
	public int  updateUser(UsersDTO info) {
		int result=0;
		String sql="update Users set uName=?,uPassword=?,uTname=?,uPhone=?,uEmail=?,uZip=?,uAddress=? where uId=?";
		Connection con=null;
		PreparedStatement pds=null;
		try {
			con=DBMade.getCon();
			pds=DBMade.getPds(con, sql);
			pds.setString(1, info.getUname());
			pds.setString(2, info.getUpassword());
			pds.setString(3, info.getUtname());
			pds.setString(4, info.getUphone());
			pds.setString(5, info.getUemaile());
			pds.setString(6, info.getUzip());
			pds.setString(7, info.getUaddress());
			pds.setInt(8, info.getUid());
			result=pds.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DBMade.close(null, pds, con);
		}
		return result;
	}
	
}

⌨️ 快捷键说明

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