商品信息系统

entity层

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;
 }
 
 dao层
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 L
 *
 */
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();
   a = 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;
 }
}
 
增删改查方法的set层
package com.hjf.service;
import java.util.List;
import com.hjf.dao.CourseDao;
import com.hjf.entity.Course;
/**
 * CourseService
 * 服务层
 * @author L
 *
 */
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();
 }
}
servelet层
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();
 }
}
 
连接数据库的DButil
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();
   }
  }
 }
}
 
二、JSP
add.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
 .a{
  margin-top: 20px;
 }
 .b{
  font-size: 20px;
  width: 160px;
  color: white;
  background-color: greenyellow;
 }
</style>
</head>
<body>
 <%
      Object message = request.getAttribute("message");
      if(message!=null && !"".equals(message)){
 
 %>
      <script type="text/javascript">
           alert("<%=request.getAttribute("message")%>");
      </script>
 <%} %>
 <div align="center">
  <h1 style="color: red;">物资信息录入</h1>
  <a href="index.jsp">返回主页</a>
  <form action="CourseServlet method=add" method="post" onsubmit="return check()">
   <div class="a">
    物资名称<input type="text" id="name" name="name"/>
   </div>
   <div class="a">
    生产厂家<input type="text" id="place" name="place" />
   </div>
   <div class="a">
    生产规格<input type="text" id="guige" name="guige" />
   </div>
   <div class="a">
    生产型号<input type="text" id="number" name="number" />
   </div>
   <div class="a">
    <button type="submit" class="b">保&nbsp;&nbsp;&nbsp;存</button>
   </div>
  </form>
 </div>
 <script type="text/javascript">
  function check() {
   var name = document.getElementById("name");;
   var place = document.getElementById("place");
   var guige = document.getElementById("guige");
   var number = document.getElementById("number");
   
   //非空
   if(name.value == '')
   {
    alert('物资名称为空');
    name.focus();
    return false;
   }
   
   if(place.value == '')
   {
    alert('生产厂家为空');
    place.focus();
    return false;
   }
   
   if(guige.value == '')
   {
    alert('生产规格为空');
    guige.focus();
    return false;
   }
   
   if(number.value == '')
   {
    alert('生产型号为空');
    guige.focus();
    return false;
   }
}
  
 </script>
</body>
</html>
 
del.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
 .a{
  margin-top: 20px;
 }
 .b{
  font-size: 20px;
  width: 160px;
  color: white;
  background-color: greenyellow;
 }
</style>
</head>
<body>
 <%
      Object message = request.getAttribute("message");
      if(message!=null && !"".equals(message)){
 
 %>
      <script type="text/javascript">
           alert("<%=request.getAttribute("message")%>");
      </script>
 <%} %>
 <div align="center">
  <h1 style="color: red;">物资信息删除</h1>
  <a href="index.jsp">返回主页</a>
  <form action="CourseServlet?method=getcoursebyname" method="post" onsubmit="return check()">
   <div class="a">
    物资名称<input type="text" id="name" name="name"/>
   </div>
   <div class="a">
    <button type="submit" class="b">查&nbsp;&nbsp;&nbsp;找</button>
   </div>
  </form>
 </div>
 <script type="text/javascript">
  function check()
  {
   var name = document.getElementById("name");
   
   //非空
   if(name.value == '')
   {
    alert('物资名称为空');
    name.focus();
    return false;
   }
  }
 </script>
</body>
</html>
 
detail.jsp (删除)
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
 .a{
  margin-top: 20px;
 }
 .b{
  font-size: 20px;
  width: 160px;
  color: white;
  background-color: greenyellow;
 }
 .tb, td {
  border: 1px solid black;
  font-size: 22px;
 }
</style>
</head>
<body>
 <div align="center">
  <h1 style="color: red;">物资信息删除</h1>
  <a href="index.jsp">返回主页</a>
  <table class="tb">
   <tr>
    <td>物资名称</td>
    <td>${course.name}</td>
   </tr>
   <tr>
    <td>生产厂家</td>
    <td>${course.place}</td>
   </tr>
   <tr>
    <td>生产规格</td>
    <td>${course.guige}</td>
   </tr>
   <tr>
    <td>生产型号</td>
    <td>${course.number}</td>
   </tr>
  </table>
  <div class="a">
   <a onclick="return check()" href="CourseServlet?method=del&id=${course.id}">删&nbsp;&nbsp;&nbsp;除</a>
  </div>
 </div>
 <script type="text/javascript">
  function check() {
   if (confirm("真的要删除吗?"))
   {
    return true;
   }
   else
   {
    return false;
   }
  }
 </script>
</body>
</html>
 
detail2.jsp (修改)
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
 .a{
  margin-top: 20px;
 }
 .b{
  font-size: 20px;
  width: 160px;
  color: white;
  background-color: greenyellow;
 }
</style>
</head>
<body>
 <%
      Object message = request.getAttribute("message");
      if(message!=null && !"".equals(message)){
 
 %>
      <script type="text/javascript">
           alert("<%=request.getAttribute("message")%>");
      </script>
 <%} %>
 <div align="center">
  <h1 style="color: red;">物资信息修改</h1>
  <a href="index.jsp">返回主页</a>
  <form action="CourseServlet?method=update" method="post" onsubmit="return check()">
   <div class="a">
    物资名称<input type="text" id="name" name="name" value="${course.name}"/>
   </div>
   <div class="a">
    生产厂家<input type="text" id="place" name="place" value="${course.place}"/>
   </div>
   <div class="a">
    生产规格<input type="text" id="guige" name="guige" value="${course.guige}"/>
   </div>
   <div class="a">
    生产型号<input type="text" id="number" name="number" value="${course.number}"/>
   </div>
   <input type="hidden" id="id" name="id" value="${course.id}"/>
   <div class="a">
    <button type="submit" class="b">修&nbsp;&nbsp;&nbsp;改</button>
   </div>
  </form>
 </div>
 <script type="text/javascript">
  function check()
  {
   var name = document.getElementById("name");;
   var place = document.getElementById("place");
   var guige = document.getElementById("guige");
   var number = document.getElementById("number");
   
   //非空
   if(name.value == '')
   {
    alert('物资名称为空');
    name.focus();
    return false;
   }
   
   if(place.value == '')
   {
    alert('生产厂家为空');
    place.focus();
    return false;
   }
   
   if(guige.value == '')
   {
    alert('生产规格为空');
    guige.focus();
    return false;
   }
   
   if(number.value == '')
   {
    alert('生产型号为空');
    guige.focus();
    return false;
   }
   
  }
 </script>
</body>
</html>
 
index.jsp(初始界面)
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>首页</title>
<style>
 .a{
  font-size: 26px;
  margin-top: 20px;
 }
</style>
</head>
<body>
 <div align="center">
  <h1 style="color: red;">库存物资管理系统</h1>
  <div class="a">
   <a href="add.jsp">物资信息录入</a>
  </div>
  <div class="a">
   <a href="CourseServlet?method=list">物资信息修改</a>
  </div>
  <div class="a">
   <a href="del.jsp">物资信息删除</a>
  </div>
  <div class="a">
   <a href="search.jsp">物资信息查询</a>
  </div>
 </div>
</body>
</html>
 
list.jsp(查询菜单)
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>首页</title>
<style>
 .a{
  font-size: 26px;
  margin-top: 20px;
 }
</style>
</head>
<body>
 <div align="center">
  <h1 style="color: red;">库存物资管理系统</h1>
  <div class="a">
   <a href="add.jsp">物资信息录入</a>
  </div>
  <div class="a">
   <a href="CourseServlet?method=list">物资信息修改</a>
  </div>
  <div class="a">
   <a href="del.jsp">物资信息删除</a>
  </div>
  <div class="a">
   <a href="search.jsp">物资信息查询</a>
  </div>
 </div>
</body>
</html>
 
search.jsp (搜索显示界面)
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>首页</title>
<style>
 .a{
  font-size: 26px;
  margin-top: 20px;
 }
</style>
</head>
<body>
 <div align="center">
  <h1 style="color: red;">库存物资管理系统</h1>
  <div class="a">
   <a href="add.jsp">物资信息录入</a>
  </div>
  <div class="a">
   <a href="CourseServlet?method=list">物资信息修改</a>
  </div>
  <div class="a">
   <a href="del.jsp">物资信息删除</a>
  </div>
  <div class="a">
   <a href="search.jsp">物资信息查询</a>
  </div>
 </div>
</body>
</html>
 
searchlist.jsp (遍历层)
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
 .a{
  margin-top: 20px;
 }
 .b{
  font-size: 20px;
  width: 160px;
  color: white;
  background-color: greenyellow;
 }
 .tb, td {
  border: 1px solid black;
  font-size: 22px;
 }
</style>
</head>
<body>
 <div align="center">
  <h1 style="color: red;">物资信息列表</h1>
  <a href="index.jsp">返回主页</a>
  <table class="tb">
   <tr>
    <td>id</td>
    <td>物资名称</td>
    <td>生产厂家</td>
    <td>生产规格</td>
    <td>生产型号</td>
   </tr>
   <!-- forEach遍历出adminBeans -->
   <c:forEach items="${courses}" var="item" varStatus="status">
    <tr>
     <td>${item.id}</td>
     <td><a>${item.name}</a></td>
     <td>${item.place}</td>
     <td>${item.guige}</td>
     <td>${item.number}</td>
    </tr>
   </c:forEach>
  </table>
 </div>
</body>
</html>
 
总结:革命尚未成功,同志仍需努力
posted @ 2018-12-13 22:19  米汤000  阅读(152)  评论(0编辑  收藏  举报