Tools: Eclipse, Oracle,; Language: jsp, Java; Data storage: Oracle.
Implementation function introduction:
It is mainly about creating a new album. You can create multiple albums, add multiple photos to the album, delete photos, delete albums, and delete photos. When there are photos under the album, you can delete the photo first before you can delete the album.
Because each album and photo must have someone, there is a login function.
Statement: It is just the backend implementation code, there is no style in the front desk, the code test is feasible, for reference only.
Code:
Database connection help class:
public class JDBCHelper { public static final String DRIVER = ""; 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{ (DRIVER); Connection conn = (URL, DBNAME, PASSWORD); return conn; } }
When uploading the picture, you need to modify the image name to prevent uploading a duplicate image from overwriting the previous picture. The method here is to change the image name to consist of the user ID and the time to be accurate to milliseconds, and modify the image name help class:
public class PhotoName { private String ip; public PhotoName(String ip) { super(); = ip; } public String getIp() { return ip; } public void setIp(String ip) { = ip; } public String getTime(){ Date date = new Date(); DateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS"); return (date); } public String getPhotoName(){ return + (); } }
Interface classes that implement all of these:
public interface UpDAO { /** * Create an album name * */ public int creAlbum(AlbumPOJO ap); /** *Show all album names created */ public List<AlbumPOJO> findAllAlbum(int id); public List<PhotoPOJO> findAllPhoto(int id); /** * Upload photos */ public int upPhoto(PhotoPOJO pp); /** * Delete album * @param id Album id * @return */ public int delAlbum(int id); /** * Delete photos * @param id Photo id * @return */ public int delPhoto(int id); /** * Log in * @param username * @param password * @return */ public UserPOJO login(String username,String password); }
Specific implementation classes of interfaces:
public class UpDAOImpl implements UpDAO { /* (non-Javadoc) * @see #creAlbum() * Create an album name */ public int creAlbum(AlbumPOJO ap) { int albumNum=(); Connection conn = null; PreparedStatement pstate = null; try { conn=(); (false); String sql="insert into album(id,a_name,user_id)values(?,?,?)"; pstate = (sql); (1, albumNum); (2,ap.getA_name()); (3, ap.getUser_id()); (); (); } catch (Exception e) { (); try { ();// If there is any problem, withdraw it and will not submit it } catch (SQLException e1) { (); } }finally{ try { (); (); } catch (SQLException e) { (); } } return albumNum; } /* (non-Javadoc) * @see #upPhoto(, , int) * Upload photos */ public int upPhoto(PhotoPOJO pp) { int pNum=(); Connection conn = null; PreparedStatement pstate = null; try { conn=(); (false); String sql="insert into photo(id,p_name,p_url,p_albumid)values(?,?,?,?)"; pstate = (sql); (1, pNum); (2,pp.getP_name()); (3, pp.getP_url()); (4, pp.getP_albumId()); (); (); } catch (Exception e) { (); try { ();// If there is any problem, withdraw it and will not submit it } catch (SQLException e1) { (); } }finally{ try { (); (); } catch (SQLException e) { (); } } return pNum; } /* (non-Javadoc) * @see #delAlbum(int) * Delete album */ public int delAlbum(int id) { int result=0; Connection conn = null; PreparedStatement pstate = null; String sql="delete from album where "; try { conn=(); pstate = (sql); result=(); } catch (SQLException e) { (); } catch (Exception e) { // TODO Auto-generated catch block (); }finally{ try { (); (); } catch (SQLException e) { // TODO Auto-generated catch block (); } } return result; } /* (non-Javadoc) * @see #delPhoto(int) * Delete photos */ public int delPhoto(int id) { int result=0; Connection conn = null; PreparedStatement pstate = null; String sql="delete from photo where "; try { conn=(); pstate = (sql); result=(); } catch (SQLException e) { (); } catch (Exception e) { // TODO Auto-generated catch block (); }finally{ try { (); (); } catch (SQLException e) { // TODO Auto-generated catch block (); } } return result; } /* (non-Javadoc) * @see #login(, ) * User login */ public UserPOJO login(String username, String password) { UserPOJO user=null; Connection conn = null; PreparedStatement pstate = null; ResultSet res = null; try { conn=(); String sql="select id,username from userinfo where username=? and password=?"; pstate = (sql); (1, username); (2, password); res = (); while(()){ user=new UserPOJO((1),username,null); } } catch (Exception e) { (); }finally{ try { (); (); (); } catch (SQLException e) { (); } } return user; } /** * Album serial number */ public int getAlbumNum(){ int albumNum=-1; Connection conn = null; PreparedStatement pstate = null; ResultSet res = null; try { conn=(); String sql="select from dual"; pstate=(sql); res=(); while(()){ albumNum=(1); } } catch (Exception e) { (); }finally{ try { (); (); (); } catch (SQLException e) { (); } } return albumNum; } /** *Photo serial number */ public int getPhotoNum(){ int photoNum=-1; Connection conn = null; PreparedStatement pstate = null; ResultSet res = null; try { conn=(); String sql="select from dual"; pstate=(sql); res=(); while(()){ photoNum=(1); } } catch (Exception e) { (); }finally{ try { (); (); (); } catch (SQLException e) { (); } } return photoNum; } /* (non-Javadoc) * @see #findAll() * Show the created album name */ public List<AlbumPOJO> findAllAlbum(int id) { List<AlbumPOJO> list= new ArrayList<AlbumPOJO>(); Connection conn = null; PreparedStatement pstate = null; ResultSet res = null; try { conn=(); String sql="select id,a_name,user_id from album where user_id=?"; pstate = (sql); (1, id); res = (); while(()){ AlbumPOJO ap=new AlbumPOJO((1),(2),(3)); (ap); } } catch (Exception e) { (); }finally{ try { (); (); (); } catch (SQLException e) { (); } } return list; } /* (non-Javadoc) * @see #findAllPhoto(int) * Show photos */ public List<PhotoPOJO> findAllPhoto(int aid) { List<PhotoPOJO> list= new ArrayList<PhotoPOJO>(); Connection conn = null; PreparedStatement pstate = null; ResultSet res = null; try { conn=(); String sql="select id,p_name,p_url from photo where P_ALBUMID=?"; pstate = (sql); (1, aid); res = (); while(()){ PhotoPOJO pojo=new PhotoPOJO((1),(2),(3), aid); (pojo); } } catch (Exception e) { (); }finally{ try { (); (); (); } catch (SQLException e) { (); } } return list; } }
User, photo album, and photo three POJO categories:
/** * User Entity Class * */ public class UserPOJO implements Serializable{ private static final long serialVersionUID = 7554548269035753256L; private int id; private String username; private String password; public int getId() { return id; } public void setId(int id) { = id; } public String getUsername() { return username; } public void setUsername(String username) { = username; } public String getPassword() { return password; } public void setPassword(String password) { = password; } public UserPOJO(int id, String username, String password) { super(); = id; = username; = password; } public UserPOJO(String username, String password) { = username; = password; } public UserPOJO() { super(); // TODO Auto-generated constructor stub } }
/** * Album entity category * */ public class AlbumPOJO implements Serializable{ private int id; private String a_name; private int user_id; public int getId() { return id; } public void setId(int id) { = id; } public String getA_name() { return a_name; } public void setA_name(String a_name) { this.a_name = a_name; } public int getUser_id() { return user_id; } public void setUser_id(int user_id) { this.user_id = user_id; } public AlbumPOJO(int id, String a_name, int user_id) { super(); = id; this.a_name = a_name; this.user_id = user_id; } public AlbumPOJO(String a_name, int user_id) { this.a_name = a_name; this.user_id = user_id; } public AlbumPOJO() { super(); // TODO Auto-generated constructor stub } }
/** *Photo entity category * */ public class PhotoPOJO implements Serializable{ private static final long serialVersionUID = 5937149639009957458L; private int id; private String p_name; private String p_url; private int p_albumId; public int getId() { return id; } public void setId(int id) { = id; } public String getP_name() { return p_name; } public void setP_name(String p_name) { this.p_name = p_name; } public String getP_url() { return p_url; } public void setP_url(String p_url) { this.p_url = p_url; } public int getP_albumId() { return p_albumId; } public void setP_albumId(int p_albumId) { this.p_albumId = p_albumId; } public PhotoPOJO(int id, String p_name, String p_url, int p_albumId) { super(); = id; this.p_name = p_name; this.p_url = p_url; this.p_albumId = p_albumId; } public PhotoPOJO(String p_name, String p_url, int p_albumId) { this.p_name = p_name; this.p_url = p_url; this.p_albumId = p_albumId; } public PhotoPOJO() { super(); // TODO Auto-generated constructor stub } }
Realize login
<%@ page language="java" import=".*" pageEncoding="UTF-8"%> <%@ page import=".*" %> <%@ page import=".*" %> <%@ page import=".*" %> <% String path = (); String basePath = ()+"://"+()+":"+()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <title>login</title> </head> <body> <% ("utf-8"); String action=("action"); UpDAO ud=new UpDAOImpl(); String username=("username"); String password=("password"); UserPOJO pojo=(username, password); if("log".equals(action)){ if(pojo==null){ %> <h1>Login failed</h1> <% }else{ ().setAttribute("username", username); ().setAttribute("userid", ()); (""); } } %> <form action="?action=log" method="post"> <input type="text" name="username" placeholder="Please enter a username"/> <input type="password" name="password" placeholder="Please enter your password"/> <input type="submit"/> </form> </body> </html>
Implement display of photo albums
The code is as follows:
<%@ page language="java" import=".*" pageEncoding="UTF-8"%> <%@ page import=".*" %> <%@ page import=".*" %> <%@ page import=".*" %> <% String path = (); String basePath = ()+"://"+()+":"+()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <title>person message</title> </head> <body> <center>Photo Album Interface</center> Current user:<%=().getAttribute("username")%> <br> <a href="" rel="external nofollow" >Go to create an album</a><br> All my albums:<br> <% int userid=(Integer)().getAttribute("userid"); UpDAO dao=new UpDAOImpl(); List<AlbumPOJO> list=(userid); for(AlbumPOJO pojo:list){ %> <tr> <a>Photo Albumid:</a><td><%=() %></td> <a>Photo Album名称:</a><td><%=pojo.getA_name() %></td> <a>Creatorid:</a><td><%=pojo.getUser_id() %></td> <td><a href="?aid=<%=() %>" rel="external nofollow" >Add a photo</a></td> <td><a href="?phid=<%=() %>" rel="external nofollow" >View photos</a></td> <td><a href="?aid=<%=() %>" rel="external nofollow" >删除Photo Album</a></td> </tr><br> <% } %> </body> </html>
Create album
<%@ page language="java" import=".*" pageEncoding="UTF-8"%> <%@ page import=".*" %> <%@ page import=".*" %> <%@ page import=".*" %> <% String path = (); String basePath = ()+"://"+()+":"+()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <title>up photo</title> </head> <body> <% ("utf-8"); String action=("action"); UpDAO ud=new UpDAOImpl(); String toCre=("cre"); int userId=(Integer)().getAttribute("userid"); if("cre".equals(action)){ AlbumPOJO ap=new AlbumPOJO(toCre,userId); int aNum=(ap); if(aNum!=-1){ (""); }else{ %> <h1>Failed to create an album</h1> <% } } %> <form action="?action=cre" method="post"> <input type="text" name="cre" placeholder="Please enter the name of the album you want to create"/> <input type="submit" value="Sure"> </form> </body> </html>
Upload photos
<%@ page language="java" import=".*" pageEncoding="UTF-8"%> <%@ page import=".*" %> <%@ page import=".*" %> <%@ page import=".*" %> <%@ page import=".*" %> <%@page import=".*" %> <% String path = (); String basePath = ()+"://"+()+":"+()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <title>Upload photos</title> </head> <body> <% int aid=(("aid")); %> <form action="" method="post" enctype="multipart/form-data"> <input type="hidden" name="aid" value="<%=aid %>"/> <input type="file" name="photo"/> <input type="submit" value="Confirm Upload"/> </form> </body> </html>
Upload photo processing page
<%@ page language="java" import=".*" pageEncoding="UTF-8"%> <%@ page import=".*" %> <%@ page import=".*" %> <%@ page import=".*" %> <%@ page import=".*" %> <%@page import=".*" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title></title> </head> <body> <% String ip = (); ip = (":",""); PhotoName pn=new PhotoName(ip); String pName = ();//The photo name is composed of IP plus current time SmartUpload smartupload = new SmartUpload();//Instantiate the object of upload operation //Initialize upload file (pageContext); //Prepare to upload (); int albumId=(().getParameter("aid")); //Get the file suffix String endName = ().getFile(0).getFileExt(); //The path to save the file /*String p_url = getServletContext().getRealPath("/")+ "file/"+pName+"."+endName;*/ String p_url="K:/workspace/Xiangce/WebRoot/file/"+pName+"."+endName; //Save the file ().getFile(0).saveAs(p_url); UpDAO ad=new UpDAOImpl(); PhotoPOJO pojo=new PhotoPOJO(pName+"."+endName,p_url,albumId); int photoNum=(pojo); if(photoNum != -1){ ().setAttribute("phid", albumId); (""); } else { %> Upload failed <% } %> </body> </html>
Show photos and information pages:
The code is as follows:
<%@ page language="java" import=".*" pageEncoding="UTF-8"%> <%@ page import=".*" %> <%@ page import=".*" %> <%@ page import=".*" %> <% String path = (); String basePath = ()+"://"+()+":"+()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <title>My JSP '' starting page</title> </head> <body> <center>Photo Album Interface</center> Current user:<%=().getAttribute("username")%> <br> <% int phid=(Integer)().getAttribute("phid"); UpDAO dao=new UpDAOImpl(); List<PhotoPOJO> list=(phid); for(PhotoPOJO pojo:list){ %> <tr> <a>photoid:</a><td><%=() %></td><br> <a>photo名称:</a><td><%=pojo.getP_name() %></td><br> <a>photo路径:</a><td><%=pojo.getP_url() %></td><br> <a>photo所属相册名称:</a><td><%=pojo.getP_albumId() %></td><br> <td><img src="<%=path%>/file/<%=pojo.getP_name() %>" width="100" height="100"/></td> <a href="photo_del.jsp?pid=<%=() %>" rel="external nofollow" >删除photo:</a></td><br> </tr><br> <%} %> </body> </html>
photo_del.jsp delete photos
<%@ page language="java" import=".*" pageEncoding="UTF-8"%> <%@ page import=".*" %> <%@ page import=".*" %> <%@ page import=".*" %> <% String path = (); String basePath = ()+"://"+()+":"+()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <title>del</title> </head> <body> <% int pid=(("pid")); int result=0; UpDAO dao=new UpDAOImpl(); result=(pid); if(result==1){ ("<script>alert('Delete successfully');('');</script>"); }else{ ("<script>alert('What happened?');('');</script>"); } %> </body> </html>
Delete album
<%@ page language="java" import=".*" pageEncoding="UTF-8"%> <%@ page import=".*" %> <%@ page import=".*" %> <%@ page import=".*" %> <% String path = (); String basePath = ()+"://"+()+":"+()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <title>del</title> </head> <body> <% int aid=(("aid")); int result=0; UpDAO dao=new UpDAOImpl(); result=(aid); if(result==1){ ("<script>alert('Delete successfully');('');</script>"); }else{ ("<script>alert('Deletion failed, please delete the photos in the album first');('');</script>"); } %> </body> </html>
Database table creation statement:
-- Create table create table USERINFO ( ID NUMBER, USERNAME VARCHAR2(30), PASSWORD VARCHAR2(30) ) tablespace USERS pctfree 10 initrans 1 maxtrans 255 storage ( initial 64 minextents 1 maxextents unlimited ); -- Create/Recreate primary, unique and foreign key constraints alter table USERINFO add constraint PID primary key (ID) disable; --Uploader +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -- Create table create table ALBUM ( ID NUMBER not null, A_NAME VARCHAR2(30), USER_ID NUMBER ) tablespace USERS pctfree 10 initrans 1 maxtrans 255 storage ( initial 64 minextents 1 maxextents unlimited ); -- Create/Recreate primary, unique and foreign key constraints alter table ALBUM add constraint AL_PID primary key (ID) using index tablespace USERS pctfree 10 initrans 2 maxtrans 255 storage ( initial 64K minextents 1 maxextents unlimited ); alter table ALBUM add constraint USERID foreign key (USER_ID) references USERINFO (ID) disable; --Album list ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -- Create table create table PHOTO ( ID NUMBER, P_NAME VARCHAR2(30), P_URL VARCHAR2(50), P_ALBUMID NUMBER(30) ) tablespace USERS pctfree 10 initrans 1 maxtrans 255 storage ( initial 64 minextents 1 maxextents unlimited ); -- Create/Recreate primary, unique and foreign key constraints alter table PHOTO add constraint ALB_ID foreign key (P_ALBUMID) references ALBUM (ID); --Photo list
OK, all the code is finished. Remember, you need a package. You can download the children's shoes that don't have:
smartuploadjar package
The above is the jsp that the editor introduced to you to create multiple album names imitating QQ space and add photo functions to the album. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!