最代码广告位
首页>分享>java开发微博项目源码>/项目源码与数据库/MicroBlog/src/org/microblog/dao/MicroBlogDao.java
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;
	}
	
	
}
最近下载更多
开心100分 LV16月24日
星星
auvolo LV16月5日
星星
870447899 LV45月26日
月亮
2323552609 LV65月21日
月亮星星星星
能不能不存在 LV114月21日
月亮月亮星星星星星星
Hhhhh12 LV14月15日
星星
muratjan886 LV13月13日
星星
chenchencehn LV13月3日
星星
meiyuanhaotiancai LV32月8日
星星星星星星
qingming258 LV11月22日
星星
最近浏览更多
hostile昨天
暂无贡献等级
kong.yee LV39前天
太阳太阳月亮星星星星星星
kuailexiaoge7月2日
暂无贡献等级
开心100分 LV16月24日
星星
xuanzi6666月22日
暂无贡献等级
sjdhhxg LV56月10日
月亮星星
zmzmzzn LV56月8日
月亮星星
auvolo LV16月5日
星星
乔大帅 LV16月2日
星星
暂无贡献等级
顶部客服微信二维码底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友