jsp实现仿QQ空间新建多个相册名称,向相册中添加照片

工具:Eclipse,Oracle,smartupload.jar;语言:jsp,Java;数据存储:Oracle。

实现功能介绍:

主要是新建相册,可以建多个相册,在相册中添加多张照片,删除照片,删除相册,当相册下有照片时先删除照片才能删除相册。

因为每个相册和照片要有所属人,所以顺带有登录功能。

声明:只是后端实现代码,前台无任何样式,代码测试可行,仅供参考。

代码:

数据库连接帮助类:

public class JDBCHelper {
	public static final String DRIVER = "oracle.jdbc.driver.OracleDriver";
	public static final String URL = "jdbc:oracle:thin:@localhost:1521:xxxx";
	public static final String DBNAME = "scott";
	public static final String PASSWORD = "xxxx";
	public static Connection getConn() throws Exception{
		Class.forName(DRIVER);
		Connection conn = DriverManager.getConnection(URL, DBNAME, PASSWORD);
		return conn;
	}
}


图片上传时,要修改图片名称,防止上传重名图片将上一张覆盖,这里的做法是将图片名改为由用户ID和精确到毫秒的时间组成,修改图片名的帮助类:

public class PhotoName {
	private String ip;

	public PhotoName(String ip) {
		super();
		this.ip = ip;
	}

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}
	public String getTime(){
		Date date = new Date();
		DateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		return df.format(date);
	}
	public String getPhotoName(){
		return this.ip + this.getTime();
	}

}
实现所有这些的接口类:

public interface UpDAO {
	/**
	 * 创建相册名称
	 * 
	 */
	public int creAlbum(AlbumPOJO ap);
	/**
	 *显示所创建的所有相册名称
	 */
	public List<AlbumPOJO> findAllAlbum(int id);
	public List<PhotoPOJO> findAllPhoto(int id);
	/**
	 * 上传照片
	 */
	public int upPhoto(PhotoPOJO pp);
	/**
	 * 删除相册
	 * @param id 相册id
	 * @return
	 */
	public int delAlbum(int id);
	/**
	 * 删除照片
	 * @param id 照片id
	 * @return
	 */
	public int delPhoto(int id);
	/**
	 * 登录
	 * @param username
	 * @param password
	 * @return
	 */
	public UserPOJO login(String username,String password);
}

接口的具体实现类:

public class UpDAOImpl implements UpDAO {

	/* (non-Javadoc)
	 * @see cn.jvsun.DAO.UpDAO#creAlbum(cn.jvsun.POJO.AlbumPOJO)
	 * 创建相册名称
	 */
	public int creAlbum(AlbumPOJO ap) {
		int albumNum=this.getAlbumNum();
		Connection conn = null;
		PreparedStatement pstate = null;
		try {
			conn=JDBCHelper.getConn();
			conn.setAutoCommit(false);
			String sql="insert into album(id,a_name,user_id)values(?,?,?)";
			pstate = conn.prepareStatement(sql);
			pstate.setInt(1, albumNum);
			pstate.setString(2,ap.getA_name());
			pstate.setInt(3, ap.getUser_id());
			pstate.execute();
			conn.commit();

		} catch (Exception e) {
			e.printStackTrace();
			try {
				conn.rollback();//出问题就撤回,全不提交
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}finally{
			try {
				pstate.close();
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return albumNum;
	}

	/* (non-Javadoc)
	 * @see cn.jvsun.DAO.UpDAO#upPhoto(java.lang.String, java.lang.String, int)
	 * 上传照片
	 */
	public int upPhoto(PhotoPOJO pp) {
		int pNum=this.getPhotoNum();
		Connection conn = null;
		PreparedStatement pstate = null;
		try {
			conn=JDBCHelper.getConn();
			conn.setAutoCommit(false);
			String sql="insert into photo(id,p_name,p_url,p_albumid)values(?,?,?,?)";
			pstate = conn.prepareStatement(sql);
			pstate.setInt(1, pNum);
			pstate.setString(2,pp.getP_name());
			pstate.setString(3, pp.getP_url());
			pstate.setInt(4, pp.getP_albumId());
			pstate.execute();
			conn.commit();

		} catch (Exception e) {
			e.printStackTrace();
			try {
				conn.rollback();//出问题就撤回,全不提交
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}finally{
			try {
				pstate.close();
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return pNum;
	}

	/* (non-Javadoc)
	 * @see cn.jvsun.DAO.UpDAO#delAlbum(int)
	 * 删除相册
	 */
	public int delAlbum(int id) {
		int result=0; 
		Connection conn = null;
		PreparedStatement pstate = null;
		String sql="delete from album where id="+id+""; 
		try {
			conn=JDBCHelper.getConn();
			pstate = conn.prepareStatement(sql);
			result=pstate.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				pstate.close();
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return result;
         
        
	}

	/* (non-Javadoc)
	 * @see cn.jvsun.DAO.UpDAO#delPhoto(int)
	 * 删除照片
	 */
	public int delPhoto(int id) {
		int result=0; 
		Connection conn = null;
		PreparedStatement pstate = null;
		String sql="delete from photo where id="+id+""; 
		try {
			conn=JDBCHelper.getConn();
			pstate = conn.prepareStatement(sql);
			result=pstate.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				pstate.close();
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return result;
	}
	/* (non-Javadoc)
	 * @see cn.jvsun.DAO.UpDAO#login(java.lang.String, java.lang.String)
	 * 用户登录
	 */
	public UserP
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta content="IE=7.0000" http-equiv="X-UA-Compatible"> <title>jquery 访QQ相册</title> <meta content="text/html; charset=utf-8" http-equiv=content-type> <script type=text/javascript src="js/jquery.js"></script> <script type=text/javascript src="js/images.js"></script> <style type="text/css"> * {line-height: 150%} .image {text-align: center; line-height: 590px; margin: 30px auto 0px; width: 850px; height: 620px} .image img { overflow:hidden} img {border:0px} #photo_content {text-align: center} .container {margin: 20px 0 0 0; width: 960px} div#container {z-index: -2; background: #fff; border-top: #fff 1px solid} body {margen: 0px; padding: 0px; } ul {margin: 0px; padding:0px;} li {margin: 0px; padding:0px;} .wrap { text-align: left; margin: 0px auto} #wrap {padding-bottom: 10px; background-color: #fff; min-height: 450px; clear: both; } .wrap {width: 960px} .switch {margin: 0px auto; width: 708px; background: url(images/switch_link_11.jpg) no-repeat center 0px; height: 87px} .clear {clear: both} .icon1 {text-indent: -9999px;padding: 30px 0 0 18px; width: 15px; display: inline; float: left;} .icon1 a {width: 15px; display: block; background: url(images/sprite.gif) no-repeat -693px -92px; height: 30px} .icon1 a:hover {background: url(images/sprite.gif) no-repeat -693px -132px} .icon2 {text-indent: -9999px; width: 15px; float: right; padding:30px 15px 0 0;} .icon2 a {width: 15px; display: block; background: url(images/sprite.gif) no-repeat -670px -92px; height: 30px} .icon2 a:hover {background: url(images/sprite.gif) no-repeat -670px -132px} .switch_center {width: 585px; float: left; height: 83px; margin-left: 28px; overflow: hidden} .switch_center ul {} .switch_center li {width: 66px; display: inline; float: left; height: 66px; margin:0 20px 0 0; padding: 10px 0 0 0} .switch_center li a {border: #ccc 1px solid;width: 60px; display: block; height: 60px;} .switch_center li a img {width: 60px; height: 60px} .switch_center li a.on {border: #ff9900 1px solid} .switch_center li a:hover {border: #ff9900 1px solid;} .clear {clear: both} .loading {line-height:520px; width:850px; background: url(images/loading.gif) #333 no-repeat center center; height: 520px} </style> </head> <body> <div id="wrap" class="wrap"> <div id="page3"> <div id="photo_content"> <div class="container"> <div class="switch"> <div class="icon1"> <a onFocus="this.blur();" title="上一个" href="javascript:void(0);">上一个</a> </div> <div id="pics" class="switch_center"> <ul> <li><a title="" href="javascript:void(0);"><img alt="" src="pics/1.jpg"></a></li> <li><a title="" href="javascript:void(0);"><img alt="" src="pics/2.jpg"></a></li> <li><a title="" href="javascript:void(0);"><img alt="" src="pics/3.jpg"></a></li> <li><a title="" href="javascript:void(0);"><img alt="" src="pics/4.jpg"></a></li> <li><a title="" href="javascript:void(0);"><img alt="" src="pics/5.jpg"></a></li> <li><a title="" href="javascript:void(0);"><img alt="" src="pics/6.jpg"></a></li> <li><a title="" href="javascript:void(0);"><img alt="" src="pics/7.jpg"></a></li> <li><a title="" href="javascript:void(0);"><img alt="" src="pics/8.jpg"></a></li> <li><a title="" href="javascript:void(0);"><img alt="" src="pics/9.jpg"></a></li> <li><a title="" href="javascript:void(0);"><img alt="" src="pics/10.jpg"></a></li> <li><a title="" href="javascript:void(0);"><img alt="" src="pics/11.jpg"></a></li> <li><a title="" href="javascript:void(0);"><img alt="" src="pics/12.jpg"></a></li> <li><a title="" href="javascript:void(0);"><img alt="" src="pics/13.jpg"></a></li> <li><a title="" href="javascript:void(0);"><img alt="" src="pics/14.jpg"></a></li> <li><a title="" href="javascript:void(0);"><img alt="" src="pics/15.jpg"></a></li> <li><a title="" href="javascript:void(0);"><img alt="" src="pics/16.jpg"></a></li> <li><a title="" href="javascript:void(0);"><img alt="" src="pics/17.jpg"></a></li> <li><a title="" href="javascript:void(0);"><img alt="" src="pics/18.jpg"></a></li> <li><a title="" href="javascript:void(0);"><img alt="" src="pics/19.jpg"></a></li> </ul> </div> <div class="icon2"><a onFocus="this.blur();" title="下一个" href="javascript:void(0);">下一个</a></div> <div class="clear"></div> </div> <div id="bigpics" class="image" title=""><IMG id=scollimg src="pics/1.jpg"></div> </div> </div> </div> </div> </body> </html>
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值