picturedao.java

来自「java带进度条上传尽量不要让站长把时间都花费在为您修正说明上」· Java 代码 · 共 287 行

JAVA
287
字号
package com.jmwl.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.jmwl.common.BlogException;
import com.jmwl.dto.PhotoDTO;
import com.jmwl.dto.PictureDTO;
import com.jmwl.vo.PictureVO;


public class PictureDAO extends BasicDAO
{
	private PreparedStatement ps;
	private ResultSet rs;
	/**
	 * 添加照片
	 * @param ct
	 * @param pdto
	 * @return
	 * @throws BlogException 
	 */
	public boolean addPicture(PictureDTO pdto) throws BlogException
	{
		boolean flag=false;
		String sql="insert into pictures (photo_id,picture_uri,picture_description,picture_name,user_id) values(?,?,?,?,?)";
		try {
			ps=this.getConn().prepareStatement(sql);
			ps.setInt(1, pdto.getPhoto_id());
			ps.setString(2,pdto.getPicture_uri());
			ps.setString(3,pdto.getPicture_description());
			ps.setString(4, pdto.getPicture_name());
			ps.setInt(5, pdto.getUser_id());
			int i=ps.executeUpdate();
			if(i>0)
			{
				flag=true;
			}
		} catch (SQLException e) {
			throw new BlogException("添加失败,数据库出现异常");
		}
		return flag;
	}
	/**
	 * 获得某一相册的所有照片,并分页
	 * @param ct
	 * @param pageNow
	 * @param rowCount
	 * @return
	 * @throws BlogException 
	 */
	public List getOneUserPictures(int pageNow,int rowCount,PhotoDTO pdto) throws BlogException
	{
		int start=(pageNow-1)*rowCount;
		List list=new ArrayList();
		String sql="select * from pictures where photo_id=? limit ?,?";
		try {
			ps=this.getConn().prepareStatement(sql);
			ps.setInt(1, pdto.getId());
			ps.setInt(2, start);
			ps.setInt(3, rowCount);
			rs=ps.executeQuery();
			while(rs.next())
			{
				PictureVO pvo=new PictureVO();
				pvo.setId(rs.getInt("id"));
				pvo.setPicture_description(rs.getString("picture_description"));
				pvo.setPicture_uri(rs.getString("picture_uri"));
				pvo.setPhoto_id(rs.getInt("photo_id"));
				pvo.setUser_id(rs.getInt("user_id"));
				pvo.setPicture_name(rs.getString("picture_name"));
				list.add(pvo);
			}
		} catch (SQLException e) {
			throw new BlogException("无法显示相片,数据库出现异常");
		}
		return list;
	}
	
	/**
	 * 获得某一相册的所有照片,不分页
	 * @param ct
	 * @param pageNow
	 * @param rowCount
	 * @return
	 * @throws BlogException 
	 */
	public List getOneUserPictures(PhotoDTO pdto) throws BlogException
	{
	
		List list=new ArrayList();
		String sql="select * from pictures where photo_id=?";
		try {
			ps=this.getConn().prepareStatement(sql);
			ps.setInt(1, pdto.getId());
			rs=ps.executeQuery();
			while(rs.next())
			{
				PictureVO pvo=new PictureVO();
				pvo.setId(rs.getInt("id"));
				pvo.setPicture_description(rs.getString("picture_description"));
				pvo.setPicture_uri(rs.getString("picture_uri"));
				pvo.setPhoto_id(rs.getInt("photo_id"));
				pvo.setPicture_name(rs.getString("picture_name"));
				list.add(pvo);
			}
		} catch (SQLException e) {
			throw new BlogException("无法显示相片,数据库出现异常");
		}
		return list;
	}
	/**
	 * 获得某一相册里照片的总数,以分页时用
	 * @param ct
	 * @return
	 * @throws BlogException 
	 */
	public int getAllCount(PhotoDTO pdto) throws BlogException
	{
		int allCount=0;
		String sql="select count(*) from pictures where photo_id=?";
		try {
			ps=this.getConn().prepareStatement(sql);
			ps.setInt(1, pdto.getId());
			rs=ps.executeQuery();
			rs.next();
			allCount=rs.getInt(1);
		} catch (SQLException e) {
			throw new BlogException("无法显示相片,数据库出现异常");
		}
		return allCount;
	}
	/**
	 * 删除照片
	 * @param ct
	 * @param ptdto
	 * @return
	 * @throws BlogException 
	 */
	public boolean delPicture(PictureDTO ptdto) throws BlogException
	{
		boolean flag=false;
		String sql="delete from pictures where id=?";
		try {
			ps=this.getConn().prepareStatement(sql);
			ps.setInt(1, ptdto.getId());
			if(ps.executeUpdate()==1)
				flag=true;
		} catch (SQLException e) {
			throw new BlogException("删除相片失败,数据库出现异常");
		}
		return flag;
	}
	/**
	 * 修改照片描述
	 * @param ct
	 * @param ptdto
	 * @return
	 * @throws BlogException 
	 */
	public boolean editPicture(PictureDTO ptdto) throws BlogException
	{
		boolean flag=false;
		String sql="update pictures set picture_description=?,picture_name=? where id=?";
		try {
			ps=this.getConn().prepareStatement(sql);
			ps.setString(1, ptdto.getPicture_description());
			ps.setString(2, ptdto.getPicture_name());
			ps.setInt(3, ptdto.getId());
			if(ps.executeUpdate()==1)
				flag=true;
		} catch (SQLException e) {
			throw new BlogException("修改失败,数据库出现异常");
		}
		return flag;
	}
	/**
	 * 删除某一相册的全部照片
	 * @param ct
	 * @param pdto
	 * @return
	 * @throws BlogException 
	 */
	public boolean delOnePhotoPictures(PhotoDTO pdto) throws BlogException
	{
		boolean flag=false;
		String sql="delete from pictures where photo_id=?";
		System.out.println(pdto.getId());
		try {
			ps=this.getConn().prepareStatement(sql);
			ps.setInt(1, pdto.getId());
			if(ps.executeUpdate()>=0)
				flag=true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return flag;
	}
	/**
	 * 删除某一用户的全部照片
	 * @param user_id
	 * @return
	 * @throws BlogException
	 */
	public boolean delOneUserPictures(int user_id) throws BlogException
	{
		boolean flag=false;
		String sql="delete from pictures where user_id=?";
		try {
			ps=this.getConn().prepareStatement(sql);
			ps.setInt(1, user_id);
			if(ps.executeUpdate()>0)
				flag=true;
		} catch (SQLException e) {
			throw new BlogException("删除失败,数据库出现异常");
		}
		return flag;
	}
	
	public PictureVO getOnePicture(int id) throws BlogException
	{
		String sql="select * from pictures where id=?";
		String picture_uri="";
		PictureVO ptvo=new PictureVO();
		try {
			ps=this.getConn().prepareStatement(sql);
			ps.setInt(1, id);
			rs=ps.executeQuery();
			while(rs.next())
			{
				ptvo.setId(id);
				ptvo.setPhoto_id(rs.getInt("photo_id"));
				ptvo.setPicture_description(rs.getString("picture_description"));
				ptvo.setPicture_name(rs.getString("picture_name"));
				ptvo.setPicture_uri(rs.getString("picture_uri"));
				ptvo.setUser_id(rs.getInt("user_id"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return ptvo;
	}
	
	public String getLastPictureUri(int photo_id) throws BlogException
	{
		String sql="select picture_uri from pictures where photo_id=? order by id desc";
		String uri="";
		try {
			ps=this.getConn().prepareStatement(sql);
			ps.setInt(1, photo_id);
			rs=ps.executeQuery();
			while(rs.next())
			{
				uri=rs.getString("picture_uri");
				break;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return uri;
		
	}
	
	public boolean checkPictureName(String picture_name,int photo_id) throws BlogException
	{
		String sql="select count(*) from pictures where picture_name=? and photo_id=?";
		boolean b=false;
		try {
			ps=this.getConn().prepareStatement(sql);
			ps.setString(1, picture_name);
			ps.setInt(2, photo_id);
			rs=ps.executeQuery();
			rs.next();
			if(rs.getInt(1)==0)
				b=true;
		} catch (SQLException e) {
			throw new BlogException("查找照片名失败,数据库异常");
		}
		return b;
	}

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?