package com.friends.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.friends.DBUtil.DBUtil;
import com.friends.entity.FriendUser;
import com.mysql.jdbc.PreparedStatement;

/**
 * @author xrhou
 *
 */
public class FriendUserDAO {
	private Connection conn = null;
	private PreparedStatement pstmt = null;
	
	public long save(FriendUser friendUser) throws Exception{
		conn = DBUtil.getConnection();
		String sql = "insert into friend_user(username,password,name,age,gendar,phone) values(?,?,?,?,?,?)";
		pstmt=(PreparedStatement) conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
		pstmt.setString(1,friendUser.getUsername());
		pstmt.setString(2,friendUser.getPassword());
		pstmt.setString(3,friendUser.getName());
		pstmt.setInt(4, friendUser.getAge());
		pstmt.setString(5, friendUser.getGendar());
		pstmt.setString(6, friendUser.getPhone());
		pstmt.executeUpdate();
		
		ResultSet rst=pstmt.getGeneratedKeys();
		long id=-1L;
		if(rst.next()){
			id=rst.getLong(1);
		}
		DBUtil.close(conn);
		return id;
	}
	
	public FriendUser findByUserName(String username) throws Exception{
		FriendUser friendUser=null;
		conn = DBUtil.getConnection();
		String sql = "select * from friend_user where username=?";
		pstmt=(PreparedStatement) conn.prepareStatement(sql);
		pstmt.setString(1,username);
		ResultSet rst=pstmt.executeQuery();
		
		if(rst.next()){
			friendUser=new FriendUser();
			friendUser.setId(rst.getLong("id"));
			friendUser.setUsername(username);
			friendUser.setName(rst.getString("name"));
			friendUser.setGendar(rst.getString("gendar"));
			friendUser.setPassword(rst.getString("password"));
			friendUser.setAge(rst.getInt("age"));
			friendUser.setPhone(rst.getString("phone"));
		}		
		DBUtil.close(conn);
		return friendUser;
	}
	
	public List<FriendUser> findByUserId(Long id) throws Exception{
		 List<FriendUser> fUsers=new ArrayList<FriendUser>();
		conn = DBUtil.getConnection();
		String sql = "select * from friend_user where id=?";
		pstmt=(PreparedStatement) conn.prepareStatement(sql);
		pstmt.setLong(1,id);
		ResultSet rst=pstmt.executeQuery();
		
		if(rst.next()){
			FriendUser friendUser=new FriendUser();
			friendUser.setId(id);
			friendUser.setName(rst.getString("name"));
			friendUser.setUsername(rst.getString("username"));
			friendUser.setGendar(rst.getString("gendar"));
			friendUser.setPassword(rst.getString("password"));
			friendUser.setAge(rst.getInt("age"));
			friendUser.setPhone(rst.getString("phone"));
			fUsers.add(friendUser);
		}		
		DBUtil.close(conn);
		return fUsers;
	}
	
	
	public List<FriendUser> flist() throws Exception{
		List<FriendUser> friendUsers=new ArrayList<FriendUser>();
		
		conn=DBUtil.getConnection();
		Statement stmt=conn.createStatement();
		ResultSet rs=stmt.executeQuery("select * from friend_user");
	
		while(rs.next()){
			FriendUser fUser=new FriendUser();
			fUser.setId(rs.getInt("id"));
			fUser.setUsername(rs.getString("username"));
			fUser.setAge(rs.getInt("age"));
			fUser.setGendar(rs.getString("gendar"));
			friendUsers.add(fUser);
		}
		DBUtil.close(conn);
		return friendUsers;
	}
	
}
最近下载更多
568885778  LV12 2024年12月30日
SQ2930501923  LV14 2022年10月13日
2691533321  LV23 2022年4月12日
wanglinddad  LV55 2022年3月25日
ღ᭄ꦿ剑起苍瀾  LV5 2021年12月16日
www236421  LV1 2021年5月24日
微笑刺客  LV21 2020年11月23日
kelvin007008  LV8 2020年11月19日
马力奥  LV2 2020年10月29日
js0314  LV1 2020年6月14日
最近浏览更多
Dominick  LV14 2月21日
dongandmin  LV8 2月20日
568885778  LV12 2024年12月30日
shuai66  LV2 2024年12月11日
rain112  LV31 2024年6月18日
仙女山的月亮  LV2 2024年5月22日
zolscy  LV24 2024年3月30日
janly989713  LV3 2024年3月20日
weiwei_2  LV2 2024年1月29日
李朝磊  LV18 2024年1月3日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友