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日