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;
	}
}
最近下载更多
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日
taoshen95  LV14 2023年5月29日
fq70112  LV3 2023年5月18日
喜喜喜夕夕夕  LV1 2023年3月27日
1430074044aaa  LV5 2022年12月15日
最近浏览更多
刘守曜 昨天
暂无贡献等级
gdt234  LV3 昨天
烟橙沐雨  LV1 3月21日
杨豫川  LV12 3月14日
xiaojie93  LV2 3月1日
yunYUN123  LV1 2月27日
ljhgff  LV1 2月9日
limin123  LV6 2月9日
344741626 1月10日
暂无贡献等级
Liang朝伟  LV1 1月6日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友