DBUtil.java

package com.hjf.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
 * 数据库连接工具
 * @author Hu
 *
 */
public class DBUtil {
 public static String db_url = "jdbc:mysql://localhost:3306/test?&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";
 public static String db_user = "root";
 public static String db_pass = "root";
 
 public static Connection getConn () {
  Connection conn = null;
  
  try {
   Class.forName("com.mysql.cj.jdbc.Driver");//连接数据库
   conn = DriverManager.getConnection(db_url, db_user, db_pass);
  } catch (Exception e) {
   e.printStackTrace();
  }
  
  return conn;
 }
 
 /**
  * 关闭连接
  * @param state
  * @param conn
  */
 public static void close (Statement state, Connection conn) {
  if (state != null) {
   try {
    state.close();
   } catch (SQLException e) {
    e.printStackTrace();
   }
  }
  
  if (conn != null) {
   try {
    conn.close();
   } catch (SQLException e) {
    e.printStackTrace();
   }
  }
 }
 
 public static void close (ResultSet rs, Statement state, Connection conn) {
  if (rs != null) {
   try {
    rs.close();
   } catch (SQLException e) {
    e.printStackTrace();
   }
  }
  
  if (state != null) {
   try {
    state.close();
   } catch (SQLException e) {
    e.printStackTrace();
   }
  }
  
  if (conn != null) {
   try {
    conn.close();
   } catch (SQLException e) {
    e.printStackTrace();
   }
  }
 }
}
 
CourseServlet.java
 
package com.hjf.servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.hjf.entity.Course;
import com.hjf.service.CourseService;
@WebServlet("/CourseServlet")
public class CourseServlet extends HttpServlet {
 
 private static final long serialVersionUID = 1L;
 CourseService service = new CourseService();
 
 /**
  * 方法选择
  */
 protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  req.setCharacterEncoding("utf-8");
  String method = req.getParameter("method");
  if ("add".equals(method)) {
   add(req, resp);
  } else if ("del".equals(method)) {
   del(req, resp);
  } else if ("update".equals(method)) {
   update(req, resp);
  } else if ("search".equals(method)) {
   search(req, resp);
  } else if ("getcoursebyid".equals(method)) {
   getCourseById(req, resp);
  } else if ("getcoursebyname".equals(method)) {
   getCourseByName(req, resp);
  } else if ("list".equals(method)) {
   list(req, resp);
  }
 }
 /**
  * 添加
  * @param req
  * @param resp
  * @throws IOException
  * @throws ServletException
  */
 private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
  req.setCharacterEncoding("utf-8");
  String name = req.getParameter("name");
  String place = req.getParameter("place");
  String guige = req.getParameter("guige");
  String number = req.getParameter("number");
  Course course = new Course(name, place, guige,number);
  
  //添加后消息提示
  if(service.add(course)) {
   req.setAttribute("message", "添加成功");
   req.getRequestDispatcher("add.jsp").forward(req,resp);
  } else {
   req.setAttribute("message", "物资名称重复,请重新录入");
   req.getRequestDispatcher("add.jsp").forward(req,resp);
  }
 }
 
 /**
  * 全部
  * @param req
  * @param resp
  * @throws ServletException
  */
 private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
  req.setCharacterEncoding("utf-8");
  List<Course> courses = service.list();
  req.setAttribute("courses", courses);
  req.getRequestDispatcher("list.jsp").forward(req,resp);
 }
 /**
  * 通过ID得到Course
  * @param req
  * @param resp
  * @throws ServletException
  */
 private void getCourseById(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
  req.setCharacterEncoding("utf-8");
  int id = Integer.parseInt(req.getParameter("id"));
  Course course = service.getCourseById(id);
  req.setAttribute("course", course);
  req.getRequestDispatcher("detail2.jsp").forward(req,resp);
 }
 /**
  * 通过名字查找 
  * 跳转至删除
  * @param req
  * @param resp
  * @throws IOException
  * @throws ServletException
  */
 private void getCourseByName(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
  req.setCharacterEncoding("utf-8");
  String name = req.getParameter("name");
  Course course = service.getCourseByName(name);
  if(course == null) {
   req.setAttribute("message", "查无此物资");
   req.getRequestDispatcher("del.jsp").forward(req,resp);
  } else {
   req.setAttribute("course", course);
   req.getRequestDispatcher("detail.jsp").forward(req,resp);
  }
 }
 
 /**
  * 删除
  * @param req
  * @param resp
  * @throws IOException
  * @throws ServletException
  */
 private void del(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
  req.setCharacterEncoding("utf-8");
  int id = Integer.parseInt(req.getParameter("id"));
  service.del(id);
  req.setAttribute("message", "删除成功");
  req.getRequestDispatcher("del.jsp").forward(req,resp);
 }
 
 /**
  * 修改
  * @param req
  * @param resp
  * @throws IOException
  * @throws ServletException
  */
 private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
  req.setCharacterEncoding("utf-8");
  int id = Integer.parseInt(req.getParameter("id"));
  String name = req.getParameter("name");
  String place = req.getParameter("place");
  String guige = req.getParameter("guige");
  String number = req.getParameter("number");
  Course course = new Course(id, name, place, guige,number);
  
  service.update(course);
  req.setAttribute("message", "修改成功");
  req.getRequestDispatcher("CourseServlet?method=list").forward(req,resp);
 }
 
 /**
  * 查找
  * @param req
  * @param resp
  * @throws ServletException
  */
 private void search(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
  req.setCharacterEncoding("utf-8");
  String name = req.getParameter("name");
  String place = req.getParameter("place");
  String guige = req.getParameter("guige");
  String number = req.getParameter("number");
  List<Course> courses = service.search(name, place, guige,number);
  req.setAttribute("courses", courses);
  req.getRequestDispatcher("searchlist.jsp").forward(req,resp);
 }
}
 
CourseService.java
 
package com.hjf.service;
import java.util.List;
import com.hjf.dao.CourseDao;
import com.hjf.entity.Course;
/**
 * CourseService
 * 服务层
 * @author Hu
 *
 */
public class CourseService {
 CourseDao cDao = new CourseDao();
 
 /**
  * 添加
  * @param course
  * @return
  */
 public boolean add(Course course) {
  boolean f = false;
  if(!cDao.name(course.getName())) {
   cDao.add(course);
   f = true;
  }
  return f;
 }
 
 /**
  * 删除
  */
 public void del(int id) {
  cDao.delete(id);
 }
 
 /**
  * 修改
  * @return
  */
 public void update(Course course) {
  cDao.update(course);
 }
 
 /**
  * 通过ID得到一个Course
  * @return
  */
 public Course getCourseById(int id) {
  return cDao.getCourseById(id);
 }
 /**
  * 通过name得到一个Course
  * @return
  */
 public Course getCourseByName(String name) {
  return cDao.getCourseByName(name);
 }
 
 /**
  * 查找
  * @return
  */
 public List<Course> search(String name, String place, String guige,String number) {
  return cDao.search(name, place, guige,number);
 }
 
 /**
  * 全部数据
  * @return
  */
 public List<Course> list() {
  return cDao.list();
 }
}
 
Course.java
 
package com.hjf.entity;
public class Course {
 private int id;
 private String name;
 private String place;
 private String guige;
 private String number;
 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 getPlace()
 {
  return place;
 }
 public void setPlace(String place)
 {
  this.place = place;
 }
 public String getGuige()
 {
  return guige;
 }
 public void setGuige(String guige)
 {
  this.guige = guige;
 }
 public String getNumber()
 {
  return number;
 }
 public void setNumber(String number)
 {
  this.number = number;
 }
 public Course() {}
 
 public Course(int id, String name, String teacher, String classroom,String number) {
  this.id = id;
  this.name = name;
  this.place = place;
  this.guige = guige;
  this.number=number;
 }
 
 public Course(String name, String teacher, String classroom,String number) {
  this.name = name;
  this.place = place;
  this.guige = guige;
  this.number=number;
 }
 
 CourseDao.java
 
package com.hjf.dao;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.hjf.entity.Course;
import com.hjf.util.DBUtil;
/**
 * 商品Dao
 * Dao层操作数据
 * @author Hu
 *
 */
public class CourseDao {
 /**
  * 添加
  * @param course
  * @return
  */
 public boolean add(Course course) {
  String sql = "insert into course(name, place, guige,number) values('" + course.getName() + "','" + course.getPlace() + "','" + course.getGuige() + "','" + course.getNumber()+"')";
  Connection conn = DBUtil.getConn();
  Statement state = null;
  boolean f = false;
  int a = 0;
  
  try {
   state = conn.createStatement();
   state.executeUpdate(sql);
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   DBUtil.close(state, conn);
  }
  
  if (a > 0) {
   f = true;
  }
  return f;
 }
 /**
  * 删除
  *
  * @param id
  * @return
  */
 public boolean delete (int id) {
  boolean f = false;
  String sql = "delete from course where id='" + id + "'";
  Connection conn = DBUtil.getConn();
  Statement state = null;
  int a = 0;
  
  try {
   state = conn.createStatement();
   state.executeUpdate(sql);
  } catch (SQLException e) {
   e.printStackTrace();
  } finally {
   DBUtil.close(state, conn);
  }
  
  if (a > 0) {
   f = true;
  }
  return f;
 }
 /**
  * 修改
  * @param name
  * @param pass
  */
 public boolean update(Course course) {
  String sql = "update course set name='" + course.getName() + "', place='" + course.getPlace() + "', guige='" + course.getGuige()+"', number='" + course.getNumber()
   + "' where id='" + course.getId() + "'";
  Connection conn = DBUtil.getConn();
  Statement state = null;
  boolean f = false;
  int a = 0;
  try {
   state = conn.createStatement();
   a=state.executeUpdate(sql);
  } catch (SQLException e) {
   e.printStackTrace();
  } finally {
   DBUtil.close(state, conn);
  }
  
  if (a > 0) {
   f = true;
  }
  return f;
 }
 
 /**
  * 验证课程名称是否唯一
  * true --- 不唯一
  * @param name
  * @return
  */
 public boolean name(String name) {
  boolean flag = false;
  String sql = "select name from course where name = '" + name + "'";
  Connection conn = DBUtil.getConn();
  Statement state = null;
  ResultSet rs = null;
  
  try {
   state = conn.createStatement();
   rs = state.executeQuery(sql);
   while (rs.next()) {
    flag = true;
   }
  } catch (SQLException e) {
   e.printStackTrace();
  } finally {
   DBUtil.close(rs, state, conn);
  }
  return flag;
 }
 
 /**
  * 通过ID得到类
  * @param id
  * @return
  */
 public Course getCourseById(int id) {
  String sql = "select * from course where id ='" + id + "'";
  Connection conn = DBUtil.getConn();
  Statement state = null;
  ResultSet rs = null;
  Course course = null;
  
  try {
   state = conn.createStatement();
   rs = state.executeQuery(sql);
   while (rs.next()) {
    String name = rs.getString("name");
    String place = rs.getString("place");
    String guige = rs.getString("guige");
    String number = rs.getString("number");
    course = new Course(id, name, place,guige,number);
   }
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   DBUtil.close(rs, state, conn);
  }
  
  return course;
 }
 
 /**
  * 通过name得到Course
  * @param name
  * @return
  */
 public Course getCourseByName(String name) {
  String sql = "select * from course where name ='" + name + "'";
  Connection conn = DBUtil.getConn();
  Statement state = null;
  ResultSet rs = null;
  Course course = null;
  
  try {
   state = conn.createStatement();
   rs = state.executeQuery(sql);
   while (rs.next()) {
    int id = rs.getInt("id");
    String place = rs.getString("place");
    String guige = rs.getString("guige");
    String number = rs.getString("number");
    course = new Course(id, name, place, guige,number);
   }
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   DBUtil.close(rs, state, conn);
  }
  
  return course;
 }
 
 /**
  * 查找
  * @param name
  * @param place
  * @param guige
  * @param number
  * @return
  */
 public List<Course> search(String name, String place, String guige,String number) {
  String sql = "select * from course where ";
  if (name != "") {
   sql += "name like '%" + name + "%'";
  }
  if (place != "") {
   sql += "place like '%" + place + "%'";
  }
  if (guige != "") {
   sql += "guige like '%" + guige + "%'";
  }
  if (number != "") {
   sql += "name like '%" + number + "%'";
  }
  List<Course> list = new ArrayList<>();
  Connection conn = DBUtil.getConn();
  Statement state = null;
  ResultSet rs = null;
  try {
   state = conn.createStatement();
   rs = state.executeQuery(sql);
   Course bean = null;
   while (rs.next()) {
    int id = rs.getInt("id");
    String name2 = rs.getString("name");
    String place2 = rs.getString("place");
    String guige2 = rs.getString("guige");
    String number2 = rs.getString("number2");
    bean = new Course(id, name2, place2, guige2,number2);
    list.add(bean);
   }
  } catch (SQLException e) {
   e.printStackTrace();
  } finally {
   DBUtil.close(rs, state, conn);
  }
  
  return list;
 }
 
 /**
  * 全部数据
  * @param name
  * @param place
  * @param guige
  * @return
  */
 public List<Course> list() {
  String sql = "select * from course";
  List<Course> list = new ArrayList<>();
  Connection conn = DBUtil.getConn();
  Statement state = null;
  ResultSet rs = null;
  try {
   state = conn.createStatement();
   rs = state.executeQuery(sql);
   Course bean = null;
   while (rs.next()) {
    int id = rs.getInt("id");
    String name2 = rs.getString("name");
    String place2 = rs.getString("place");
    String guige2 = rs.getString("guige");
    String number2 = rs.getString("number2");
    bean = new Course(id, name2, place2, guige2,number2);
    list.add(bean);
   }
  } catch (SQLException e) {
   e.printStackTrace();
  } finally {
   DBUtil.close(rs, state, conn);
  }
  
  return list;
 }
}
 
OK,以上就是java部分
总的来说,现在完成这个所要求的功能还是蛮快的。
 

JavaWeb——库存管理系统(2).java部分---18.12.13的更多相关文章

  1. JavaWeb——库存管理系统(1)jsp部分---18.12.13

    add.jsp <%@ page language="java" contentType="text/html; charset=UTF-8"    pa ...

  2. Java学习笔记7(简易的超市库存管理系统示例)

    用以前学过的知识,可以简单地做一个超市库存管理系统: 定义一个商品类: public class FruitItem { int ID; String name; double price; int ...

  3. 图书管理系统(Java实现,十个数据表,含源码、ER图,超详细报告解释,2020.7.11更新)

    图书管理系统数据库设计实验报告 文章目录 更新日志 1.概述 2.需求分析 2.1需要实现的功能 2.2业务流程图 2.2.1学生流程图 2.2.2管理员流程图 2.2.3超级管理员流程图 2.3功能 ...

  4. 那一夜,我们..奋笔疾书敲出的--->>库存管理系统

    说了会再见,最近好吗?无论你在哪里>也许你在温暖的家,或许你在身在异乡的城市;或许你高高的峰顶放生高歌,或许你还在陡峭的山峰半空努力攀爬.......相信我们都会登上顶峰,"会当凌绝顶 ...

  5. 【Java】-NO.12.Java.2.OCJP.1.001-【Java OCJP】-

    1.0.0 Summary Tittle:[Java]-NO.12.Java.2.OCJP.1.001-[Java OCJP]- Style:Java Series:OCJP Since:2017-0 ...

  6. 2018面向对象程序设计(Java)第12周学习指导及要求

    2018面向对象程序设计(Java)第12周学习指导及要求 (2018.11.15-2018.11.18)   学习目标 (1) 掌握Java GUI中框架创建及属性设置中常用类的API: (2) 掌 ...

  7. Java设计模式(12)迭代模式(Iterator模式)

    上了这么多年学,我发现一个问题,好象老师都很喜欢点名,甚至点名都成了某些老师的嗜好,一日不点名,就饭吃不香,觉睡不好似的,我就觉得很奇怪,你的课要是讲的好,同学又怎么会不来听课呢,殊不知:“误人子弟, ...

  8. JavaSE库存管理系统项目实战

    需求分析 企业库房用于存放成品.半成品.原材料.工具等物资,一般情况下,这些物资统称为物料.库存管理常见业务包括物资的入库.出库.盘点.退货.报废以及财务核算等,业务逻辑比较复杂,库房信息系统项目开发 ...

  9. 面向对象程序设计(JAVA) 第12周学习指导及要求

    2019面向对象程序设计(Java)第12周学习指导及要求 (2019.11.15-2019.11.18)   学习目标 (1) 掌握Vetor.Stack.Hashtable三个类的用途及常用API ...

随机推荐

  1. php安装扩展redis淌过的坑

    php安装扩展redis淌过的坑 php扩展 redis cd /usr/local/src/ wget https://github.com/nicolasff/phpredis/archive/2 ...

  2. Win10安装msi程序报错2503和2502错误解决方案

    刚升级了系统到win10,重新搭建开发环境,在安装scala的时候一直报2503.2502错误,如图 试了好几种办法都不好使,现在罗列依次用到的三种方法: 一.命令提示符(管理员)启动 "w ...

  3. [USACO07DEC]Sightseeing Cows

    嘟嘟嘟 这题好像属于01分数规划问题,叫什么最优比率生成环. 题目概括一下,就是求一个环,满足∑v[i] / ∑c[i]最大. 我们可以堆上面的式子变个型:令 x = ∑v[i] / ∑c[i],则x ...

  4. 检测Android和IOS

    var u=navigator.userAgent; var isAndroid=u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; / ...

  5. ESlint 语法检测配置说明

    部分vue-cli脚手架创建的默认eslint规则: 代码末尾不能加分号 ; 代码中不能存在多行空行 tab键不能使用,必须换成两个空格 代码中不能存在声明了但未使用的变量 关闭eslint 这里只说 ...

  6. 持续集成之Jenkins+Gitlab实现持续集成

    项目使用git+jenkins实现持续集成 开始构建  General  源码管理 我们安装的是Git插件,还可以安装svn插件  我们将git路径存在这里还需要权限认证,否则会出现error  我们 ...

  7. java中prepareStatement与createStatement的区别

    首先来看两段代码: 第一个使用createStatement() public void delete( int id){ try { Connection c = DBUtil.getConnect ...

  8. HDU 2018母牛的故事(类似斐波那契,找规律)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2018 母牛的故事 Time Limit: 2000/1000 MS (Java/Others)     ...

  9. Xcode-push到远程仓库不能使用邮箱名,需使用昵称

    1.Xcode-push到远程仓库不能使用邮箱 2.Xcode-push到远程仓库需使用昵称

  10. win7系统安装VS2013后,连不上远程sqlserver数据库解决办法

    刚搬了地方,顺便把电脑重做了系统,把sql2012和vs2013装好,怎么弄也连不上远程的数据库了,用程序连IIS直接死掉,用ssms连也是直接失去响应,开始以为是网线端口被运营商封杀了,最后发现不是 ...