工具: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"; 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"; 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 UserPOJO login(String username, String password) { UserPOJO user=null; Connection conn = null; PreparedStatement pstate = null; ResultSet res = null; try { conn=JDBCHelper.getConn(); String sql="select id,username from userinfo where username=? and password=?"; pstate = conn.prepareStatement(sql); pstate.setString(1, username); pstate.setString(2, password); res = pstate.executeQuery(); while(res.next()){ user=new UserPOJO(res.getInt(1),username,null); } } catch (Exception e) { e.printStackTrace(); }finally{ try { res.close(); pstate.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } return user; } /** * 相册序列号 */ public int getAlbumNum(){ int albumNum=-1; Connection conn = null; PreparedStatement pstate = null; ResultSet res = null; try { conn=JDBCHelper.getConn(); String sql="select aid.nextval from dual"; pstate=conn.prepareStatement(sql); res=pstate.executeQuery(); while(res.next()){ albumNum=res.getInt(1); } } catch (Exception e) { e.printStackTrace(); }finally{ try { res.close(); pstate.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } return albumNum; } /** *照片序列号 */ public int getPhotoNum(){ int photoNum=-1; Connection conn = null; PreparedStatement pstate = null; ResultSet res = null; try { conn=JDBCHelper.getConn(); String sql="select pid.nextval from dual"; pstate=conn.prepareStatement(sql); res=pstate.executeQuery(); while(res.next()){ photoNum=res.getInt(1); } } catch (Exception e) { e.printStackTrace(); }finally{ try { res.close(); pstate.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } return photoNum; } /* (non-Javadoc) * @see cn.jvsun.DAO.UpDAO#findAll() * 显示所创建的相册名 */ public List<AlbumPOJO> findAllAlbum(int id) { List<AlbumPOJO> list= new ArrayList<AlbumPOJO>(); Connection conn = null; PreparedStatement pstate = null; ResultSet res = null; try { conn=JDBCHelper.getConn(); String sql="select id,a_name,user_id from album where user_id=?"; pstate = conn.prepareStatement(sql); pstate.setInt(1, id); res = pstate.executeQuery(); while(res.next()){ AlbumPOJO ap=new AlbumPOJO(res.getInt(1),res.getString(2),res.getInt(3)); list.add(ap); } } catch (Exception e) { e.printStackTrace(); }finally{ try { res.close(); pstate.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } return list; } /* (non-Javadoc) * @see cn.jvsun.DAO.UpDAO#findAllPhoto(int) * 显示照片 */ public List<PhotoPOJO> findAllPhoto(int aid) { List<PhotoPOJO> list= new ArrayList<PhotoPOJO>(); Connection conn = null; PreparedStatement pstate = null; ResultSet res = null; try { conn=JDBCHelper.getConn(); String sql="select id,p_name,p_url from photo where P_ALBUMID=?"; pstate = conn.prepareStatement(sql); pstate.setInt(1, aid); res = pstate.executeQuery(); while(res.next()){ PhotoPOJO pojo=new PhotoPOJO(res.getInt(1),res.getString(2),res.getString(3), aid); list.add(pojo); } } catch (Exception e) { e.printStackTrace(); }finally{ try { res.close(); pstate.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } return list; } }
用户,相册,照片三个POJO类: