javaweb(servlet)+jsp+Mysql实现的简单相册管理系统(功能包含登录、管理首页、添加图片、分类管理、修改密码、图片详情等)
博客目录
- javaweb(servlet)+jsp+Mysql实现的简单相册管理系统
- 实现功能截图
- 系统功能
- 使用技术
- 代码
- 完整源码
javaweb(servlet)+jsp+Mysql实现的简单相册管理系统
本系统是一个简单的相册管理系统,可以在线管理本地相册,实现图片的预览和管理。
(文末查看完整源码)
实现功能截图
登录
添加图片
添加分类
首页
图片详情
系统功能
本系统实现了以下功能:
1、登录
2、管理首页
3、添加图片
4、分类管理
5、修改密码
6、图片详情
7、退出登录
等
使用技术
数据库:mysql
开发工具:Idea(Myeclispe、Eclipse也可以)
知识点:javaweb(servlet+jsp)
项目结构
代码
java端
实体类
Photo.java
package com.fsq.beans;public class Photo {private int id = 0;private String name;private String path = "";private int dianji ;private String contentTime = "";private String shuoming;private int lid; private int count;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPath() {return path;}public void setPath(String path) {this.path = path;}public int getDianji() {return dianji;}public void setDianji(int dianji) {this.dianji = dianji;}public String getContentTime() {return contentTime;}public void setContentTime(String contentTime) {this.contentTime = contentTime;}public String getShuoming() {return shuoming;}public void setShuoming(String shuoming) {this.shuoming = shuoming;}public int getLid() {return lid;}public void setLid(int lid) {this.lid = lid;}public int getCount() {return count;}public void setCount(int count) {this.count = count;}}
Dao层
PhotoDAO.java
package com.fsq.dao;import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;import com.fsq.beans.Photo;
import com.fsq.util.JdbcUtil;public class PhotoDAO {public List<Photo> getAllPhotos() throws SQLException {List<Photo> photoList = new ArrayList<Photo>();String sql = "select * from photo";Connection conn = JdbcUtil.getConnection();Statement stmt = null;ResultSet rs = null;try {stmt = conn.createStatement();rs = stmt.executeQuery(sql);while (rs.next()) {Photo photo = new Photo();photo.setId(rs.getInt("id"));photo.setName(rs.getString("name"));photo.setPath(rs.getString("path"));photo.setDianji(rs.getInt("dianji"));photo.setContentTime(rs.getString("contenttime"));photo.setShuoming(rs.getString("shuoming"));photo.setLid(rs.getInt("lid"));photoList.add(photo);}} catch (SQLException e) {e.printStackTrace();throw e;} finally {JdbcUtil.close(rs, null);JdbcUtil.close();}return photoList;}public void delete(Photo photo) throws SQLException {Connection conn = JdbcUtil.getConnection();PreparedStatement ps = null;String sql = "delete from photo where id=?";try {ps = conn.prepareStatement(sql);ps.setInt(1, photo.getId());ps.executeUpdate();} catch (SQLException e) {e.printStackTrace();throw e;} finally {JdbcUtil.close(null, ps);JdbcUtil.close();}}public boolean updateDianJi(Photo photo) throws SQLException {boolean flag = false;PreparedStatement ps = null;String sql = "UPDATE photo SET dianji=dianji+1 where id=?";try {Connection conn = JdbcUtil.getConnection();ps = conn.prepareStatement(sql);ps.setInt(1, photo.getId());int n = ps.executeUpdate();if (n != 0) {flag = true;} else {flag = false;}} catch (SQLException e) {e.printStackTrace();throw e;} finally {JdbcUtil.close(null, ps);JdbcUtil.close();}return flag;}public void update(Photo photo) throws SQLException {Connection conn = JdbcUtil.getConnection();PreparedStatement ps = null;String sql = "update photo set name=?,shuoming=?,lid=? where id=?";try {ps = conn.prepareStatement(sql);ps.setString(1, photo.getName());ps.setString(2, photo.getShuoming());ps.setInt(3, photo.getLid());ps.setInt(4, photo.getId());ps.executeUpdate();} catch (SQLException e) {e.printStackTrace();throw e;} finally {JdbcUtil.close(null, ps);JdbcUtil.close();}}public List<Photo> getAllPhotosByClassId(int id) throws SQLException {List<Photo> photoList = new ArrayList<Photo>();String sql = "select id,name,path,dianji,shuoming from photo where lid=?";PreparedStatement ps = null;ResultSet rs = null;try {Connection conn = JdbcUtil.getConnection();ps = conn.prepareStatement(sql);ps.setInt(1, id);rs = ps.executeQuery();while (rs.next()) {Photo photo = new Photo();photo.setId(rs.getInt(1));photo.setName(rs.getString(2));photo.setPath(rs.getString(3));photo.setDianji(rs.getInt(4));photo.setShuoming(rs.getString(5));photoList.add(photo);}} catch (SQLException e) {e.printStackTrace();throw e;} finally {JdbcUtil.close(rs, ps);JdbcUtil.close();}return photoList;}public Photo getPhotoById(int id) throws SQLException {String sql = "select id,name,path,contentTime,shuoming,lid from photo where id=?";PreparedStatement ps = null;ResultSet rs = null;try {Connection conn = JdbcUtil.getConnection();ps = conn.prepareStatement(sql);ps.setInt(1, id);rs = ps.executeQuery();while (rs.next()) {Photo photo = new Photo();photo.setId(rs.getInt(1));photo.setName(rs.getString(2));photo.setPath(rs.getString(3));photo.setContentTime(rs.getString(4));photo.setShuoming(rs.getString(5));photo.setLid(rs.getInt(6));return photo;}} catch (SQLException e) {e.printStackTrace();} finally {JdbcUtil.close(rs, ps);JdbcUtil.close();}return null;}public boolean insert(Photo photo) throws SQLException {Connection conn = null;PreparedStatement ps = null;try {String sql = "insert into photo (name,path,shuoming,contentTime,lid,dianji) values(?,?,?,?,?,0)";conn = JdbcUtil.getConnection();ps = conn.prepareStatement(sql);ps.setString(1, photo.getName());ps.setString(2, photo.getPath());ps.setString(3, photo.getShuoming());ps.setString(4, photo.getContentTime());ps.setInt(5, photo.getLid());int num = ps.executeUpdate();if (num != 0) {return true;}return false;} catch (SQLException e) {e.printStackTrace();throw e;} finally {JdbcUtil.close(null, ps);JdbcUtil.close();}}}
servlet类
PhotoServlet.java
package com.fsq.servlet;import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;import com.fsq.beans.Photo;
import com.fsq.beans.PhotoClass;
import com.fsq.beans.PingLun;
import com.fsq.biz.PhotoBiz;
import com.fsq.dao.PhotoClassDAO;
import com.fsq.dao.PhotoDAO;
import com.fsq.dao.PingLunDAO;
import com.fsq.util.ImageUtils;public class PhotoServlet extends HttpServlet {private static final long serialVersionUID = 1L;public PhotoServlet() {super();}public void destroy() {super.destroy();}@SuppressWarnings("unused")public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {request.setCharacterEncoding("utf-8");response.setCharacterEncoding("utf-8");String toUrl = "";String action = request.getParameter("action");if ("getAll".equals(action)) { // admin的管理主页面 amain.jspPhotoBiz photoBiz = new PhotoBiz();List<Photo> list = null;HttpSession session = request.getSession();try {list = photoBiz.getAllPhotos();request.setAttribute("selAllList", list);request.getRequestDispatcher("/admin/amain.jsp").forward(request, response);} catch (Exception e) {e.printStackTrace();}}if ("addPhoto".equals(action)) {List<PhotoClass> list = new PhotoClassDAO().getAllClass();request.setAttribute("list", list);request.getRequestDispatcher("admin/photo_add.jsp").forward(request, response);}if ("getAllClient".equals(action)) { // 客户的登录页面 main.jspPhotoBiz photoBiz = new PhotoBiz();List<Photo> list = null;HttpSession session = request.getSession();try {list = photoBiz.getAllPhotos();request.setAttribute("selAllList", list);request.getRequestDispatcher("main.jsp").forward(request,response);} catch (Exception e) {e.printStackTrace();}}if ("selOne".equals(action)) {try {String id = request.getParameter("id");String str = request.getParameter("str");if ("client".equals(str)) {Photo photo = new PhotoDAO().getPhotoById(Integer.parseInt(id));PhotoDAO photoDAO = new PhotoDAO();photoDAO.updateDianJi(photo);String pathMax = photo.getPath();String s[] = pathMax.split("_min");photo.setPath("uploadimg/" + s[0] + s[1]);request.setAttribute("photo", photo);List<PingLun> pingLunList = new PingLunDAO().searchByPhotoId(Integer.parseInt(id));request.setAttribute("pingLunList", pingLunList);request.getRequestDispatcher("/admin/photo_one.jsp").forward(request, response);} else if ("admin".equals(str)) {Photo photo = new PhotoDAO().getPhotoById(Integer.parseInt(id));String pathMax = photo.getPath();String s[] = pathMax.split("_min");photo.setPath("uploadimg/" + s[0] + s[1]);request.setAttribute("photo", photo);List<PingLun> pingLunList = new PingLunDAO().searchByPhotoId(Integer.parseInt(id));request.setAttribute("pingLunList", pingLunList);request.getRequestDispatcher("/admin/photo_one_pinglun.jsp").forward(request, response);}} catch (Exception e) {e.printStackTrace();}}if ("toupdate".equals(action)) {String id = request.getParameter("id");try {Photo photo = new PhotoDAO().getPhotoById(Integer.parseInt(id));request.setAttribute("photo", photo);List<PhotoClass> list = new PhotoClassDAO().getAllClass();request.setAttribute("list", list);request.getRequestDispatcher("/admin/photo_manager.jsp").forward(request, response);} catch (SQLException e) {request.setAttribute("msg", "数据库错误");request.getRequestDispatcher("/admin/result.jsp").forward(request, response);}}if ("update1".equals(action)) {String id = request.getParameter("id");String name = request.getParameter("name");String shuoming = request.getParameter("shuoming");String lid = request.getParameter("lid");try {Photo photo = new PhotoDAO().getPhotoById(Integer.parseInt(id));photo.setName(name);photo.setShuoming(shuoming);photo.setLid(Integer.parseInt(lid));new PhotoDAO().update(photo);request.setAttribute("msg", "恭喜您,图片信息修改成功");request.getRequestDispatcher("/admin/result.jsp").forward(request, response);} catch (NumberFormatException e) {request.setAttribute("msg", "数据格式错误");request.getRequestDispatcher("/admin/result.jsp").forward(request, response);e.printStackTrace();} catch (SQLException e) {request.setAttribute("msg", "数据库错误");request.getRequestDispatcher("/admin/result.jsp").forward(request, response);e.printStackTrace();}}if ("delete".equals(action)) {String id = request.getParameter("id");try {Photo photo = new PhotoDAO().getPhotoById(Integer.parseInt(id));new PhotoDAO().delete(photo);request.setAttribute("msg", "恭喜您,图片已成功删除");request.getRequestDispatcher("/admin/result.jsp").forward(request, response);} catch (NumberFormatException e) {request.setAttribute("msg", "数据格式错误");request.getRequestDispatcher("/admin/result.jsp").forward(request, response);e.printStackTrace();} catch (SQLException e) {request.setAttribute("msg", "数据库错误");request.getRequestDispatcher("/admin/result.jsp").forward(request, response);e.printStackTrace();}}if ("seeAllPhotosByClassId".equals(action)) { // 分类查看是 photoa_all.jspString id = request.getParameter("id");String num = request.getParameter("num");if (Integer.parseInt(num) == 0) {request.setAttribute("msg", "对不起该分类下没有文件");request.getRequestDispatcher("/admin/result.jsp").forward(request, response);} else {try {List<Photo> photoList = new ArrayList<Photo>();PhotoDAO photoDAO = new PhotoDAO();photoList = photoDAO.getAllPhotosByClassId(Integer.parseInt(id)); request.setAttribute("photoList", photoList);request.getRequestDispatcher("/admin/photo_all.jsp").forward(request, response);} catch (NumberFormatException e) {request.setAttribute("msg", "数据格式错误");request.getRequestDispatcher("/admin/result.jsp").forward(request, response);e.printStackTrace();} catch (SQLException e) {request.setAttribute("msg", "数据库错误");request.getRequestDispatcher("/admin/result.jsp").forward(request, response);e.printStackTrace();}}}if ("add".equals(action)) {boolean flag = false;Photo photo = new Photo();PhotoDAO photoDAO = new PhotoDAO();String msg = "";try {Calendar calendar = Calendar.getInstance();String newfilename = String.valueOf(calendar.getTimeInMillis());DiskFileItemFactory factory = new DiskFileItemFactory();ServletFileUpload upload = new ServletFileUpload(factory);List<?> items = upload.parseRequest(request);Iterator<?> iter = items.iterator();while (iter.hasNext()) {FileItem item = (FileItem) iter.next();if (item.isFormField()) {String fieldName = item.getFieldName();String fieldValue = new String(item.getString("utf-8"));if ("shuoming".equals(fieldName)) {photo.setShuoming(fieldValue);} else if ("name".equals(fieldName)) {photo.setName(fieldValue);} else if ("lid".equals(fieldName)) {photo.setLid(Integer.parseInt(fieldValue));}} else {String fieldName = item.getFieldName();String fileName = item.getName();String ext = fileName.substring(fileName.lastIndexOf('.') + 1);String contentType = item.getContentType();boolean isInMemory = item.isInMemory();long sizeInBytes = item.getSize();String saveurl = request.getSession().getServletContext().getRealPath("/")+ "uploadimg\\";String s = this.getServletContext().getRealPath("/");String newfilenamewithext = newfilename + "." + ext;File uploadedFile = new File(saveurl+ newfilenamewithext);item.write(uploadedFile);String newfilename_min = ImageUtils.createMinPic(saveurl, newfilename, ext);photo.setPath(newfilename_min);String contenttime = "";Date dt = new Date();SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");contenttime = sdf.format(dt);photo.setContentTime(contenttime);}}flag = photoDAO.insert(photo);if (flag) {msg = "恭喜,图片添加成功!";} else {msg = "对不起,图片插入失败!";}} catch (Exception e) {e.printStackTrace();msg = "图片插入失败!";}request.setAttribute("msg", msg);request.getRequestDispatcher("/admin/result.jsp").forward(request,response);}}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {this.doGet(request, response);}public void init() throws ServletException {}}
完整源码
觉得有用,记得一键三连哦!
javaweb(servlet)+jsp+Mysql实现的简单相册管理系统(功能包含登录、管理首页、添加图片、分类管理、修改密码、图片详情等)相关推荐
- javaweb(servlet)+jsp+Mysql实现的酒店客房管理系统(功能包含登录、用户管理、住客信息管理、房型管理、房间管理、会员星级管理、订单管理等)
博客目录 javaweb(servlet)+jsp+Mysql实现的酒店客房管理系统 实现功能截图 系统功能 使用技术 代码 完整源码 javaweb(servlet)+jsp+Mysql实现的酒店客 ...
- 基于JAVA+Servlet+JSP+MYSQL的学生卡消费统计管理系统
项目简介:主要记录学生的消费情况,统计消费水平,图表展示出消费水平. 项目技术:jsp,servlet 运行环境:jdk1.8,tomcat7.0,mysql 功能演示:
- 基于JAVA+Servlet+JSP+MYSQL的实验室机房预约管理系统
项目功能: 学生端: 登录注册 个人资料管理 预约添加 预约查询 教师端: 登录注册 个人资料管理 教师预约添加 教师预约查询 管理员端: 系统登录 系统用户管理 学生管理 教师管理 实验室管理 学生 ...
- 基于JAVA+Servlet+JSP+MYSQL的客户充值缴费管理系统
项目功能: 系统包括用户登录注册,用户缴费,查看当前余额,管理员登录,用户充值 页面效果:
- 基于JAVA+Servlet+JSP+MYSQL的校园门户信息管理系统
项目功能: 系统功能模块:管理员管理.学生用户管理.新闻管理.公告管理.友情链接管理.评论管理和关键字搜索 页面效果:
- 简单日程管理系统,包含用户管理以及多线程定时提醒等功能
用户日程管理 工作大致流程图 #mermaid-svg-Qod5MxXaY8rTdkqK {font-family:"trebuchet ms",verdana,arial,san ...
- 基于Servlet+jsp+mysql开发javaWeb学生管理系统(学生信息、学生选课、学生成绩、学生签到考勤)
你知道的越多,你不知道的越多 点赞再看,养成习惯 如果您有疑问或者见解,或者没有积分想获取项目,欢迎指教: 企鹅:869192208 文章目录 一.开发背景 二. 需求分析 三.开发环境 四.运行效果 ...
- 基于Servlet+jsp+mysql开发javaWeb学生成绩管理系统
你知道的越多,你不知道的越多 点赞再看,养成习惯 如果您有疑问或者见解,或者没有积分想获取项目,欢迎指教: 企鹅:869192208 文章目录 一.开发背景 二. 需求分析 三.开发环境 四.运行效果 ...
- 基于 Servlet+jsp+mysql 开发 javaWeb 学生网络考试系统
你知道的越多,你不知道的越多 点赞再看,养成习惯 如果您有疑问或者见解,或者没有积分想获取项目,欢迎指教: 企鹅:869192208 文章目录 一.开发背景 二. 需求分析 三.开发环境 四.运行效果 ...
最新文章
- 阿里云代码超限2040M remote: error: hook declined to update refs/heads
- SQL语句 - 嵌套查询
- Gentoo下keepalived+LVS实验
- 模块化数据中心成未来互联网企业首选
- 什么是TCP和UDP?—Vecloud微云
- Android获取存储和打印输出Logcat日志
- ie浏览器里面无法输入文字:
- Python中如何查看模块的源码内容
- 计算机学业水平测试题及答案初中,初中信息技术学业水平考试试题30号试题.doc...
- 洛谷 - P2617 Dynamic Rankings(树状数组套主席树)
- 深度学习《CGAN模型》
- ENVI入门系列教程---一、数据预处理---7.图像镶嵌
- 快速排序_two-powinters思想
- linux设计论文题目,计算机linux本科毕业论文题目
- 单元测试——junit4入门例子
- 论文阅读-MLPD:Multi-Label Pedestrian Detector in Multispectral Domain(海康威视研究院实习项目)
- XJTU大学计算机编程作业题 第9周
- iOS静态库SDK制作(包含第三方静态库)
- 有什么软件可以编译汇编程序?
- 《学习opencv》笔记——矩阵和图像操作——cvCrossProduct and cvCvtColor