package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import model.User;
import db.DBManager;

public class UserDAO {
	
	public boolean insert(User u) {
		boolean result = false;

		Connection conn = DBManager.getConn();
		PreparedStatement pstmt = null;
		int count = 0;
		
				String sql = "insert into users value (null,?,?)";
				try {
					pstmt = conn.prepareStatement(sql);
					pstmt.setString(1, u.getUname());
					pstmt.setString(2, u.getUpwd());
					count = pstmt.executeUpdate();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
		if (count > 0) {
			result = true;
		} else {
			result = false;
		}

		return result;


	}
	
	
	public List<User> select(User u) {
		Connection conn = DBManager.getConn();
		PreparedStatement pstmt = null;
		List<User> at = new ArrayList<User>();
		try {
			try {
				String sql = null;
				if(u.getUid()!=0){
					sql = " select * from users where uid=?";
					pstmt = conn.prepareStatement(sql);
					pstmt.setInt(u.getUid(), 1);
				}else if(u.getUpwd()!=null){
					sql = " select * from users where uname=? and upwd=?";
					pstmt = conn.prepareStatement(sql);
					pstmt.setString(1, u.getUname());
					pstmt.setString(2, u.getUpwd());
				}else{
					sql = " select * from users where uname=?";
					pstmt = conn.prepareStatement(sql);
					pstmt.setString(1, u.getUname());
				}
				
				ResultSet rs = pstmt.executeQuery();
				if (rs.next()) {
					User te = new User();
					te.setUid(rs.getInt("uid"));
					te.setUname(rs.getString("uname"));
					te.setUpwd(rs.getString("upwd"));
					at.add(te);
				}
			} finally {
				conn.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return at;
	}
}
最近下载更多
微信网友_7294439741132800  LV1 2024年12月18日
krispeng  LV13 2024年10月28日
www的行为  LV2 2024年6月20日
sdfddd  LV4 2024年5月16日
abandan  LV4 2023年11月6日
微信网友_6699076084797440  LV7 2023年11月2日
微信网友_6680567232876544  LV8 2023年10月10日
zhuorui  LV3 2023年10月7日
936684178  LV2 2023年6月26日
768881787  LV7 2023年6月26日
最近浏览更多
1125742469 2024年12月27日
暂无贡献等级
打死王二狗  LV1 2024年12月18日
hbyl123 2024年12月15日
暂无贡献等级
我做梦  LV2 2024年12月14日
微信网友_7294439741132800  LV1 2024年12月13日
huangzy  LV12 2024年12月13日
韩同学 2024年11月23日
暂无贡献等级
lqzixi  LV4 2024年11月6日
xxm111  LV1 2024年10月29日
krispeng  LV13 2024年10月28日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友