package org.microblog.dao; import java.sql.ResultSet; import java.sql.SQLException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Iterator; import java.util.List; import org.microblog.po.Favorite; import org.microblog.po.MicroBlog; import org.microblog.util.DBConn; public class MicroBlogDao{ public boolean post(final MicroBlog microBlog){ String sql_postBlog = "insert into microblog (m_id,u_id,m_content,m_releasetime,m_transfrom,m_transamount,m_image) values(null,?,?,?,?,?,?)"; DBConn dbConn = new DBConn(); int u_id = microBlog.getU_id(); String m_content = microBlog.getM_content(); String m_releasetime = microBlog.getM_releasetime(); String m_transfrom = microBlog.getM_transfrom(); long m_transamount = microBlog.getM_transamount(); String m_image = microBlog.getM_image(); int affectedRows = dbConn.execOther(sql_postBlog, new Object[]{u_id,m_content,m_releasetime,m_transfrom,m_transamount,m_image}); dbConn.closeConn(); return (affectedRows>0)?true:false; } public boolean transmit(final int m_id_in,final int u_id_in){ String sql_tansmit = "insert into microblog (u_id,m_content,m_releasetime,m_transfrom,m_transamount,m_image) values(?,?,?,?,?,?)"; String sql_gettransmit = "select * from microblog where m_id = ?"; String sql_updateold = "update microblog set m_transamount=m_transamount+1 where m_id = ?"; int u_id = 0; String m_content =null; String m_releasetime = null; String m_image = null; String m_transfrom = null; long m_transamount = 0; DBConn dbConn = new DBConn(); ResultSet rs = dbConn.execQuery(sql_gettransmit, new Object[]{m_id_in}); try { while(rs.next()){ u_id = rs.getInt("u_id"); m_content = rs.getString("m_content"); Calendar c = Calendar.getInstance(); SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); m_releasetime = f.format(c.getTime()); m_image = rs.getString("m_image"); UserDao userDao = new UserDao(); String u_nickname = userDao.getUserNameById(u_id); m_transfrom = rs.getString("m_transfrom")+"@"+u_nickname; m_transamount = rs.getLong("m_transamount")+1; } } catch (SQLException e) { e.printStackTrace(); }finally{ try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } int affectedRows2= dbConn.execOther(sql_updateold, new Object[]{m_id_in}); int affectedRows = dbConn.execOther(sql_tansmit, new Object[]{u_id_in,m_content,m_releasetime,m_transfrom,m_transamount,m_image}); dbConn.closeConn(); return (affectedRows>0&&affectedRows2>0)?true:false; } public MicroBlog getMicroBlogById(final int m_id){ String sql_getMicroBlogById = "select * from microblog where m_id = ?"; DBConn dbConn = new DBConn(); ResultSet rs = dbConn.execQuery(sql_getMicroBlogById, new Object[]{m_id}); try { while(rs.next()){ MicroBlog microBlog = new MicroBlog(); microBlog.setM_id(rs.getInt("m_id")); microBlog.setU_id(rs.getInt("u_id")); microBlog.setM_content(rs.getString("m_content")); microBlog.setM_releasetime(rs.getString("m_releasetime")); if(rs.getString("m_transfrom")!=null) microBlog.setM_transfrom(rs.getString("m_transfrom")); microBlog.setM_transamount(rs.getLong("m_transamount")); if(rs.getString("m_image")!=null) microBlog.setM_image(rs.getString("m_image")); return microBlog; } } catch (SQLException e) { e.printStackTrace(); }finally{ try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } dbConn.closeConn(); } return null; } public List<MicroBlog> getMicroBlogByUserId(final int u_id){//测试中 String sql_getMicroBlogByUserId="select * from microblog where u_id = ?"; List<MicroBlog> microBlogShow = new ArrayList<MicroBlog>(); DBConn dbConn = new DBConn(); ResultSet rs = dbConn.execQuery(sql_getMicroBlogByUserId, new Object[] {u_id}); try { while(rs.next()){ MicroBlog microblog = new MicroBlog(); microblog.setM_id(rs.getInt(1)); microblog.setU_id(rs.getInt(2)); microblog.setM_content(rs.getString(3)); microblog.setM_releasetime(rs.getString(4)); microblog.setM_transfrom(rs.getString(5)); microblog.setM_transamount(rs.getLong(6)); microblog.setM_image(rs.getString(7)); microBlogShow.add(microblog); } return microBlogShow; } catch (SQLException e) { e.printStackTrace(); return null; }finally{ try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } dbConn.closeConn(); } } public int getAuthorById(final int m_id){ String sql_getMicroBlogAmountByUserId="select u_id from microblog where m_id = ?"; DBConn dbConn = new DBConn(); ResultSet rs = dbConn.execQuery(sql_getMicroBlogAmountByUserId, new Object[]{m_id}); int u_id = 0; try { while(rs.next()){ u_id=rs.getInt("u_id"); return u_id; } } catch (Exception e) { e.printStackTrace(); }finally{ try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } dbConn.closeConn(); } return u_id; } public String getImageById(final int m_id){ return null; } public long getMicroBlogAmountByUserId(final int u_id){ String sql_getMicroBlogAmountByUserId="select count(*) from microblog where u_id = ?"; DBConn dbConn = new DBConn(); ResultSet rs = dbConn.execQuery(sql_getMicroBlogAmountByUserId, new Object[]{u_id}); long amount = 0; try { while(rs.next()){ amount=rs.getLong("count(*)"); return amount; } } catch (Exception e) { e.printStackTrace(); }finally{ try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } dbConn.closeConn(); } return amount; } public List<MicroBlog> microBlogQuery(final String key){ List<MicroBlog> microBlogs = new ArrayList<MicroBlog>(); String strSQL = "SELECT * FROM `microblog` WHERE CONCAT(`m_content`) LIKE '%"+key+"%'"; DBConn dbConn = new DBConn(); ResultSet rs = dbConn.execQuery(strSQL, new Object[] {}); try { while(rs.next()){ MicroBlog microblog = new MicroBlog(); microblog.setM_id(rs.getInt(1)); microblog.setU_id(rs.getInt(2)); microblog.setM_content(rs.getString(3)); microblog.setM_releasetime(rs.getString(4)); microblog.setM_transfrom(rs.getString(5)); microblog.setM_transamount(rs.getLong(6)); microblog.setM_image(rs.getString(7)); microBlogs.add(microblog); } if(microBlogs.isEmpty()){ return null; } return microBlogs; } catch (SQLException e) { e.printStackTrace(); return null; }finally{ try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } dbConn.closeConn(); } } public List<MicroBlog> getBlogListByUserList(final List<Integer> userList){ Iterator<Integer> iterator = userList.iterator(); List<MicroBlog> microBlogList = new ArrayList<MicroBlog>(); while(iterator.hasNext()){ List<MicroBlog> microBlogListBack = getMicroBlogByUserId(iterator.next()); Iterator<MicroBlog> iteratorBack = microBlogListBack.iterator(); while(iteratorBack.hasNext()){ microBlogList.add(iteratorBack.next()); } } return microBlogList; } public List<MicroBlog> getRandomBlogList(){ List<MicroBlog> microBlogs = new ArrayList<MicroBlog>(); String strSQL = "SELECT * FROM `microblog` ORDER BY rand() LIMIT 20"; DBConn dbConn = new DBConn(); ResultSet rs = dbConn.execQuery(strSQL, new Object[] {}); try { while(rs.next()){ MicroBlog microblog = new MicroBlog(); microblog.setM_id(rs.getInt(1)); microblog.setU_id(rs.getInt(2)); microblog.setM_content(rs.getString(3)); microblog.setM_releasetime(rs.getString(4)); microblog.setM_transfrom(rs.getString(5)); microblog.setM_transamount(rs.getLong(6)); microblog.setM_image(rs.getString(7)); microBlogs.add(microblog); } if(microBlogs.isEmpty()){ return null; } return microBlogs; } catch (SQLException e) { e.printStackTrace(); return null; }finally{ try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } dbConn.closeConn(); } } public List<MicroBlog> getBlogListByFavoriteList(final List<Favorite> favoriteList){ Iterator<Favorite> iteratorFavorite = favoriteList.iterator(); List<MicroBlog> blogList = new ArrayList<MicroBlog>(); MicroBlogDao microBlogDao = new MicroBlogDao(); while(iteratorFavorite.hasNext()){ blogList.add(microBlogDao.getMicroBlogById(iteratorFavorite.next().getM_id())); } return blogList; } }
最近下载更多
guzhoumingyue LV1
2023年11月15日
543539666 LV7
2023年6月19日
彭星旭 LV1
2023年6月12日
qingyu1 LV1
2023年2月11日
Yingxuan LV1
2023年1月16日
森屿elf LV2
2022年5月29日
微信网友_5952843817488384 LV2
2022年5月10日
葡萄树下酒庄 LV9
2022年2月16日
酸欠hhhh LV1
2021年12月22日
lk030111 LV1
2021年12月16日
最近浏览更多
goccgoccgocc LV4
5月9日
pangzhihui LV14
2月28日
ccccccc1 LV1
2023年12月26日
k666k6 LV1
2023年11月24日
guzhoumingyue LV1
2023年11月15日
weiyoung
2023年8月6日
暂无贡献等级
三十一 LV2
2023年6月28日
罗清晨 LV13
2023年6月20日
彭星旭 LV1
2023年6月12日
543539666 LV7
2023年5月30日