public class UpDAOImpl implementsUpDAO {/*(non-Javadoc)
* @see cn.jvsun.DAO.UpDAO#creAlbum(cn.jvsun.POJO.AlbumPOJO)
* 创建相册名称*/
public intcreAlbum(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();
mit();
}catch(Exception e) {
e.printStackTrace();try{
conn.rollback();//出问题就撤回,全不提交
} catch(SQLException e1) {
e1.printStackTrace();
}
}finally{try{
pstate.close();
conn.close();
}catch(SQLException e) {
e.printStackTrace();
}
}returnalbumNum;
}/*(non-Javadoc)
* @see cn.jvsun.DAO.UpDAO#upPhoto(java.lang.String, java.lang.String, int)
* 上传照片*/
public intupPhoto(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();
mit();
}catch(Exception e) {
e.printStackTrace();try{
conn.rollback();//出问题就撤回,全不提交
} catch(SQLException e1) {
e1.printStackTrace();
}
}finally{try{
pstate.close();
conn.close();
}catch(SQLException e) {
e.printStackTrace();
}
}returnpNum;
}/*(non-Javadoc)
* @see cn.jvsun.DAO.UpDAO#delAlbum(int)
* 删除相册*/
public int delAlbum(intid) {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();
}
}returnresult;
}/*(non-Javadoc)
* @see cn.jvsun.DAO.UpDAO#delPhoto(int)
* 删除照片*/
public int delPhoto(intid) {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();
}
}returnresult;
}/*(non-Javadoc)
* @see cn.jvsun.DAO.UpDAO#login(java.lang.String, java.lang.String)
* 用户登录*/
publicUserPOJO 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();
}
}returnuser;
}/*** 相册序列号*/
public intgetAlbumNum(){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();
}
}returnalbumNum;
}/***照片序列号*/
public intgetPhotoNum(){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();
}
}returnphotoNum;
}/*(non-Javadoc)
* @see cn.jvsun.DAO.UpDAO#findAll()
* 显示所创建的相册名*/
public List findAllAlbum(intid) {
List list= new ArrayList();
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();
}
}returnlist;
}/*(non-Javadoc)
* @see cn.jvsun.DAO.UpDAO#findAllPhoto(int)
* 显示照片*/
public List findAllPhoto(intaid) {
List list= new ArrayList();
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();
}
}returnlist;
}
}