引言:最近在学习MVC、JDBC部分,完成了一个简单的商品管理系统,整理一下,写下来,
也给各位一个参考。有不正确、不恰当的地方,还望指正!
备注:项目在MyEclipse下运行通过,可完成简单的商品增、删、改、查操作。
文章已经粘贴了全部代码,另附上完整项目包。包含数据库脚本和数据库驱动jar包。
百度云: https://pan.baidu.com/s/1qocTdlj_ofKMQobl0oYisQ
密码: 435a
2019年6月20日 17:40:25
1、读者反馈无法登录,问题原因是,文章6.1章节,登录页面使用的伪代码,页面属性和java bean不对应,无法登录。
现已修改。
注意:本文基于mysql 5.0开发,jdbc驱动包版本要对应。
2019年6月12日 20:52:26
2、针对读者的反馈,重新修改了源码。
2019年6月20日 17:41:53
此简易的商品管理系统,包括前端、后台和数据库三部分。前端使用jsp页面(使用div+css
布局),后端使用MVC模式,数据库为简单的两张表,用户表和商品表。
前台布局包括:
top(首栏)、banner(横幅部分)、left(左侧导航栏)、right(内容部分,使用了frame
框架)。
系统描述:
用户登录后跳转至商品管理页面(图1-1),banner显示当前用户,点击用户名(超链接)
可进入用户管理页,可对用户进行增(注册用户)、删、改(修改用户名、密码等)操作。导
航栏有显示产品和添加产品。显示产品页显示全部商品,操作栏可进行更新、删除操作。
系统前台效果:
图1-1
系统演示:
图1-2
数据库表结构:
图1-3
项目文件夹组织结构:
图1-4
后台java代码部分:
1、javabean:
1.1、Product.java
package com.zjl.bean;
public class Product {
private int id;
private String name;
private String addr;
private double price;
public Product() {
super();
// TODO Auto-generated constructor stub
}
public Product(int id, String name, String addr, double price) {
super();
this.id = id;
this.name = name;
this.addr = addr;
this.price = price;
}
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 getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
1.2、User.java
package com.zjl.bean;
public class User {
private int id;
private String name;
private String password;
private String type;
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(int id, String name, String password, String type) {
super();
this.id = id;
this.name = name;
this.password = password;
this.type = type;
}
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 getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", password=" + password
+ ", type=" + type + "]";
}
}
2、conn
2.1 ConnectDatabase.java
package com.zjl.conn;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class ConnectDatabase {
private static final String DRIVER_STRING="com.mysql.jdbc.Driver";
private static final String URL_STRING="jdbc:mysql://localhost:3306/dbms";
private static final String USER_STRING="root";
private static final String PASS_STRING="111";
public static Connection getConnection(){
Connection connection=null;
try {
Class.forName(DRIVER_STRING);
connection=DriverManager.getConnection(URL_STRING, USER_STRING, PASS_STRING);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return connection;
}
public void realse(Connection conn,PreparedStatement ps,ResultSet rs){
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(ps!=null){
try {
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
3、dao层
3.1、DaoFactory.java
package com.zjl.dao;
import com.zjl.face.IProductDao;
import com.zjl.face.IUserDao;
public class DaoFactory {
public static IUserDao getUserDao(){
return new UserDaoImp();
}
public static IProductDao getProductDao(){
return new ProductImp();
}
}
3.2、ProductImp.java
package com.zjl.dao;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.zjl.bean.Product;
import com.zjl.bean.User;
import com.zjl.conn.ConnectDatabase;
import com.zjl.face.IProductDao;
public class ProductImp implements IProductDao {
PreparedStatement ps = null;
public boolean insert(Product p) {
// TODO Auto-generated method stub
String sql ="insert into product(id,name,addr,price) values(?,?,?,?)";
int n =0;
try{
ps = ConnectDatabase.getConnection().prepareStatement(sql);
ps.setInt(1,p.getId());
ps.setString(2, p.getName());
ps.setString(3, p.getAddr());
ps.setDouble(4, p.getPrice());
n=ps.executeUpdate();
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return n>0;
}
public boolean delete(Product p) {
// TODO Auto-generated method stub
String sql ="delete from product where id="+p.getId();
int n=0;
try{
ps=ConnectDatabase.getConnection().prepareStatement(sql);
n=ps.executeUpdate();
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return n>0;
}
public boolean update(Product p) {
// TODO Auto-generated method stub
String sql = "update product set name=?,addr=?,price=? where id=?";
int n = 0;
try{
ps=ConnectDatabase.getConnection().prepareStatement(sql);
ps.setString(1, p.getName());
ps.setString(2, p.getAddr());
ps.setDouble(3, p.getPrice());
ps.setInt(4, p.getId());
n=ps.executeUpdate();
}
catch(Exception e){
e.printStackTrace();
}
return n>0;
}
public List queryProduct(Map map) {
StringBuffer sql= new StringBuffer("select * from product where 1 =1");
List list = new ArrayList();
try{
ps = ConnectDatabase.getConnection().prepareStatement(sql.toString());
ResultSet rs = ps.executeQuery();
while(rs.next()){
Product p = new Product();
p.setId(rs.getInt("id"));
p.setName(rs.getString("name"));
p.setAddr(rs.getString("addr"));
p.setPrice(rs.getDouble("price"));
list.add(p);
}
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return list;
}
public Product queryById(Product p) {
// TODO Auto-generated method stub
Product product = null;
String sql = "select * from product where id="+p.getId();
try{
ps = ConnectDatabase.getConnection().prepareStatement(sql);
ResultSet rs = ps.executeQuery();
if(rs.next()){
product = new Product();
product.setId(rs.getInt("id"));
product.setName(rs.getString("name"));
product.setAddr(rs.getString("addr"));
product.setPrice(rs.getDouble("price"));
}
}catch(Exception e){
e.printStackTrace();
}
return product;
}
}
3.3、UserImp.java
package com.zjl.dao;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import com.zjl.bean.User;
import com.zjl.conn.ConnectDatabase;
import com.zjl.face.IUserDao;
public class UserDaoImp implements IUserDao {
PreparedStatement ps = null;
public boolean insert(User u) {
// TODO Auto-generated method stub
int n=0;
String sql="insert into user(id,name,password,type) values(null,?,?,null)";
try{
ps=ConnectDatabase.getConnection().prepareStatement(sql);
ps.setString(1, u.getName());
ps.setString(2, u.getPassword());
n = ps.executeUpdate();
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return n>0;
}
public boolean delete(User u) {
// TODO Auto-generated method stub
String sql="delete from user where id=?";
int n = 0;
try{
ps=ConnectDatabase.getConnection().prepareStatement(sql);
ps.setInt(1, u.getId());
n=ps.executeUpdate();
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return n>0;
}
public boolean update(User u) {
// TODO Auto-generated method stub
return false;
}
public User queryById(User u) {
User user =null;
String sql="select * from user where name=? and password=?";
try{
ps = ConnectDatabase.getConnection().prepareStatement(sql);
ps.setString(1,u.getName());
ps.setString(2,u.getPassword());
ResultSet rs = ps.executeQuery();
if(rs.next()){
user=new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setPassword(rs.getString("password"));
user.setType(rs.getString("type"));
}
}
catch(Exception e){
e.printStackTrace();
}
return user;
}
}
4、接口
4.1、IProductDao.java
package com.zjl.face;
import java.util.List;
import java.util.Map;
import com.zjl.bean.Product;
import com.zjl.bean.User;
public interface IProductDao {
public boolean insert(Product p);
public boolean delete(Product p);
public boolean update(Product p);
public List queryProduct(Map map);
public Product queryById(Product p);
}
4.2、IUserDao.java
package com.zjl.face;
import com.zjl.bean.User;
public interface IUserDao {
public boolean insert(User u);
public boolean delete(User u);
public boolean update(User u);
public User queryById(User u);
}
5、Servlet
5.1、登录servlet
package com.zjl.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.zjl.bean.User;
import com.zjl.dao.DaoFactory;
import com.zjl.face.IUserDao;
public class LoginServlet extends HttpServlet {
public LoginServlet() {
super();
}
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String name = request.getParameter("userName");
String password = request.getParameter("userPass");
User user = new User();
user.setName(name);
user.setPassword(password);
IUserDao dao = DaoFactory.getUserDao();
if(dao.queryById(user)!=null){
//请求转发,可以传递数据
request.getRequestDispatcher("main.jsp").forward(request, response);
}else{
//重定向
response.sendRedirect("login.jsp");
}
}
public void init() throws ServletException {
// Put your code here
}
}
5.2、商品管理servlet
package com.zjl.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sun.corba.se.impl.protocol.giopmsgheaders.Message;
import com.zjl.bean.Product;
import com.zjl.dao.DaoFactory;
import com.zjl.face.IProductDao;
public class OperatorServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public OperatorServlet() {
super();
}
/**
* Destruction of the servlet.
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String op = request.getParameter("op");
if("update".equals(op)){
updateService(request,response);
}else if("del".equals(op)){
deleteService(request,response);
}else if("insert".equals(op)){
insertService(request,response);
}else if("select".equals(op)){
selectService(request,response);
}
}
//显示全部商品信息
private void selectService(HttpServletRequest request,
HttpServletResponse response) {
// TODO Auto-generated method stub
IProductDao dao = DaoFactory.getProductDao();
Map map = null;
try{
if(dao.queryProduct(map)!=null){
request.getRequestDispatcher("product.jsp").forward(request, response);
}else{
response.sendRedirect("main.jsp");
}
}
catch(Exception e){
e.printStackTrace();
}
}
//添加商品信息
private void insertService(HttpServletRequest request,
HttpServletResponse response) {
// TODO Auto-generated method stub
int id = Integer.parseInt(request.getParameter("id"));
String name=request.getParameter("name");
String addr=request.getParameter("addr");
double price = Double.parseDouble(request.getParameter("price"));
Product p = new Product(id, name, addr, price);
IProductDao dao = DaoFactory.getProductDao();
try{
if(dao.insert(p)){
request.getRequestDispatcher("product.jsp").forward(request, response);
}else{
response.sendRedirect("insert.jsp");
}}
catch(Exception e){
e.printStackTrace();
}
}
//删除指定的产品信息
private void deleteService(HttpServletRequest request,
HttpServletResponse response) {
int id =Integer.parseInt(request.getParameter("id"));
Product p = new Product();
p.setId(id);
IProductDao dao = DaoFactory.getProductDao();
try{
if(dao.delete(p)){
request.getRequestDispatcher("product.jsp").forward(request, response);
}else{
response.sendRedirect("product.jsp");
}
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
//更新商品信息
private void updateService(HttpServletRequest request,
HttpServletResponse response) {
// TODO Auto-generated method stub
int id = Integer.parseInt(request.getParameter("id"));
String name =request.getParameter("name");
String addr = request.getParameter("addr");
double price = Double.parseDouble(request.getParameter("price"));
Product p = new Product(id, name, addr, price);
IProductDao dao = DaoFactory.getProductDao();
try{
if(dao.update(p)){
request.getRequestDispatcher("product.jsp").forward(request, response);
}else{
response.sendRedirect("update.jsp");
}
}
catch(Exception e){
e.printStackTrace();
}
}
public void init() throws ServletException {
// Put your code here
}
}
6、前台页面
6.1、用户登录页
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"
+request.getServerPort()+path+"/";
%>
用户登录
6.2、主页
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"
+request.getServerPort()+path+"/";
%>
主页
6.3、商品展示页
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="com.zjl.dao.DaoFactory"%>
<%@page import="com.zjl.face.IProductDao"%>
<%@page import="com.zjl.bean.Product" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()
+":"+request.getServerPort()+path+"/";
%>
显示产品信息
编号
名称
产地
价格
操作
<%
IProductDao dao = DaoFactory.getProductDao();
Map map = null;
List list = dao.queryProduct(map);
for(Product p:list){
%>
<%=p.getId() %>
<%=p.getName() %>
<%=p.getAddr() %>
<%=p.getPrice() %>
更新
)">删除
<%} %>
6.4、商品更新页
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="com.zjl.bean.Product"%>
<%@page import="com.zjl.dao.DaoFactory"%>
<%@page import="com.zjl.face.IProductDao"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"
+request.getServerPort()
+path+"/";
%>
更新产品
<%
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
int id =Integer.parseInt(request.getParameter("id"));
Product p = new Product();
p.setId(id);
IProductDao dao = DaoFactory.getProductDao();
p=dao.queryById(p);
%>
更新产品信息
6.5、商品添加页
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+
":"+request.getServerPort()+path+"/";
%>
添加商品
添加商品
7、xml配置文件
LoginServlet
com.zjl.servlet.LoginServlet
OperatorServlet
com.zjl.servlet.OperatorServlet
LoginServlet
/Login
OperatorServlet
/OperatorServlet
login.jsp
时间仓促,系统很简单,主要功能涉及到了数据库的增、删、改、查等操作,Servlet的配置及
使用,MVC思想的应用,DIV+CSS简单布局等。可以给初学者一个简单的参考,也是对自己
学习的一个记录。欢迎各位浏览,不吝赐教!