工具: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;
}
}
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