库存管理系统
该代码只能实现商品的增删改查。
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>首页</title> 8 9 </head> 10 <body> 11 <div align="center"> 12 13 <div class="a"> 14 <a href="add.jsp">商品信息添加</a> 15 </div> 16 <div class="a"> 17 <a href="CourseServlet?method=list">商品信息修改</a> 18 </div> 19 <div class="a"> 20 <a href="del.jsp">商品信息删除</a> 21 </div> 22 <div class="a"> 23 <a href="search.jsp">商品信息查询</a> 24 </div> 25 </div> 26 </body> 27 </html>
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <% 11 Object message = request.getAttribute("message"); 12 if(message!=null && !"".equals(message)){ 13 14 %> 15 <script type="text/javascript"> 16 alert("<%=request.getAttribute("message")%>"); 17 </script> 18 <%} %> 19 <div align="center"> 20 <h1 style="color: black;">商品信息录入</h1> 21 <a href="index.jsp">返回主页</a> 22 <form action="CourseServlet?method=add" method="post" onsubmit="return check()"> 23 <div class="a"> 24 商品名称<input type="text" id="name" name="name"/> 25 </div> 26 <div class="a"> 27 商品生产厂家<input type="text" id="chanjia" name="chanjia" /> 28 </div> 29 <div class="a"> 30 商品型号<input type="text" id="number" name="number" /> 31 </div> 32 <div class="a"> 33 商品规格<input type="text" id="guige" name="guige"/> 34 </div> 35 <div class="a"> 36 商品日期<input type="text" id="riqi" name="riqi"/> 37 </div> 38 <div class="a"> 39 商品时间<input type="text" id="time" name="time"/> 40 </div> 41 <div class="a"> 42 入库单位名称<input type="text" id="ruku" name="ruku"/> 43 </div> 44 <div class="a"> 45 送货人姓名<input type="text" id="songhuo" name="songhuo"/> 46 </div> 47 <div class="a"> 48 <button type="submit" class="b">保 存</button> 49 </div> 50 </form> 51 </div> 52 <script type="text/javascript"> 53 function check() { 54 var name = document.getElementById("name");; 55 var chanjia = document.getElementById("chanjia"); 56 var number = document.getElementById("number"); 57 var guige = document.getElementById("guige"); 58 } 59 </body> 60 </html>
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>Insert title here</title> 8 9 </head> 10 <body> 11 <% 12 Object message = request.getAttribute("message"); 13 if(message!=null && !"".equals(message)){ 14 15 %> 16 <script type="text/javascript"> 17 alert("<%=request.getAttribute("message")%>"); 18 </script> 19 <%} %> 20 <div align="center"> 21 <h1 style="color: black;">商品信息删除</h1> 22 <a href="index.jsp">返回主页</a> 23 <form action="CourseServlet?method=getcoursebyname" method="post" onsubmit="return check()"> 24 <div class="a"> 25 商品名称<input type="text" id="name" name="name"/> 26 </div> 27 <div class="a"> 28 <button type="submit" class="b">查 找</button> 29 </div> 30 </form> 31 </div> 32 <script type="text/javascript"> 33 function check() { 34 var name = document.getElementById("name");; 35 36 //非空 37 if(name.value == '') { 38 alert('商品名称为空'); 39 name.focus(); 40 return false; 41 } 42 } 43 </script> 44 </body> 45 </html>
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>Insert title here</title> 8 <style> 9 .a{ 10 margin-top: 20px; 11 } 12 .b{ 13 font-size: 20px; 14 width: 160px; 15 color: white; 16 background-color: greenyellow; 17 } 18 .tb, td { 19 border: 1px solid black; 20 font-size: 22px; 21 } 22 </style> 23 </head> 24 <body> 25 <div align="center"> 26 <h1 style="color: black;">商品信息删除</h1> 27 <a href="index.jsp">返回主页</a> 28 <table class="tb"> 29 <tr> 30 <td>商品名称</td> 31 <td>${course.name}</td> 32 </tr> 33 <tr> 34 <td>商品上产产家</td> 35 <td>${course.chanjia}</td> 36 </tr> 37 <tr> 38 <td>商品型号</td> 39 <td>${course.number}</td> 40 </tr> 41 <tr> 42 <td>商品规格</td> 43 <td>${course.guige}</td> 44 </tr> 45 <tr> 46 <td>商品日期</td> 47 <td>${course.riqi}</td> 48 </tr> 49 <tr> 50 <td>商品时间</td> 51 <td>${course.time}</td> 52 </tr> 53 <tr> 54 <td>入库单位名称</td> 55 <td>${course.ruku}</td> 56 </tr> 57 <tr> 58 <td>送货人姓名</td> 59 <td>${course.songhuo}</td> 60 </tr> 61 </table> 62 <div class="a"> 63 <a onclick="return check()" href="CourseServlet?method=del&id=${course.id}">删 除</a> 64 </div> 65 </div> 66 <script type="text/javascript"> 67 function check() { 68 if (confirm("真的要删除吗?")){ 69 return true; 70 }else{ 71 return false; 72 } 73 } 74 </script> 75 </body> 76 </html>
<%@ 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> </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: black;">商品信息列表</h1> <a href="index.jsp">返回主页</a> <table class="tb"> <tr> <td>id</td> <td>商品名称</td> <td>商品生产产家</td> <td>商品型号</td> <td>商品规格</td> <td>商品日期</td> <td>商品时间</td> <td>入库单位名称</td> <td>送货人姓名</td> <td align="center" colspan="2">操作</td> </tr> <c:forEach items="${courses}" var="item"> <tr> <td>${item.id}</td> <td>${item.name}</td> <td>${item.chanjia}</td> <td>${item.number}</td> <td>${item.guige}</td> <td>${item.riqi}</td> <td>${item.time}</td> <td>${item.ruku}</td> <td>${item.songhuo}</td> <td><a href="CourseServlet?method=getcoursebyid&id=${item.id}">修改</a></td> </tr> </c:forEach> </table> </div> </body> </html>
<%@ 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> </head> <body> <div align="center"> <h1 style="color: black;">商品信息查询</h1> <a href="index.jsp">返回主页</a> <form action="CourseServlet?method=search" method="post" onsubmit="return check()"> <div class="a"> 商品名称<input type="text" id="name" name="name"/> </div> <div class="a"> 商品生产产家<input type="text" id="chanjia" name="chanjia" /> </div> <div class="a"> 商品型号<input type="text" id="number" name="number" /> </div> <div class="a"> 商品规格<input type="text" id="guige" name="guige" /> </div> <div class="a"> 商品日期<input type="text" id="riqi" name="riqi" /> </div> <div class="a"> 商品时间<input type="text" id="time" name="time" /> </div> <div class="a"> 入库单位名称<input type="text" id="ruku" name="ruku" /> </div> <div class="a"> 送货人姓名<input type="text" id="songhuo" name="songhuo" /> </div> <div class="a"> <button type="submit" class="b">查 询</button> </div> </form> </div> <script type="text/javascript"> function check() { var name = document.getElementById("name");; var chanjia = document.getElementById("chanjia"); var number = document.getElementById("number"); var guige = document.getElementById("guige"); var riqi = document.getElementById("riqi"); var time = document.getElementById("time"); var ruku = document.getElementById("ruku"); var songhuo = document.getElementById("songhuo"); //非空 if(name.value == '' && chanjia.value == '' && number.value == ''&& guige.value == ''&& riqi.value == ''&& time.value == ''&& ruku.value == ''&& songhuo.value == '') { alert('请填写一个条件'); return false; } } </script> </body> </html>
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 4 <!DOCTYPE html> 5 <html> 6 <head> 7 <meta charset="UTF-8"> 8 <title>Insert title here</title> 9 10 </head> 11 <body> 12 <div align="center"> 13 <h1 style="color: black;">商品信息列表</h1> 14 <a href="index.jsp">返回主页</a> 15 <table class="tb"> 16 <tr> 17 <td>id</td> 18 <td>商品名称</td> 19 <td>商品上产产家</td> 20 <td>商品型号</td> 21 <td>商品规格</td> 22 <td>商品日期</td> 23 <td>商品时间</td> 24 <td>入库单位名称</td> 25 <td>送货人姓名</td> 26 </tr> 27 <!-- forEach遍历出adminBeans --> 28 <c:forEach items="${courses}" var="item" varStatus="status"> 29 <tr> 30 <td>${item.id}</td> 31 <td><a>${item.name}</a></td> 32 <td>${item.chanjia}</td> 33 <td>${item.number}</td> 34 <td>${item.guige}</td> 35 <td>${item.riqi}</td> 36 <td>${item.time}</td> 37 <td>${item.ruku}</td> 38 <td>${item.songhuo}</td> 39 </tr> 40 </c:forEach> 41 </table> 42 </div> 43 </body> 44 </html>
下面是src包的代码
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; public class CourseDao { /** * 添加 * @param course * @return */ public boolean add(Course shangpin) { String sql = "insert into shangpin(name, chanjia, number, guige, riqi, time, ruku, songhuo) values('" + shangpin.getName() + "','" + shangpin.getChanjia() + "','" + shangpin.getNumber() + "','" + shangpin.getGuige() + "','" + shangpin.getRiqi() + "','" + shangpin.getTime() + "','" + shangpin.getRuku() + "','" + shangpin.getSonghuo() + "')"; //创建数据库链接 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 shangpin 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 shangpin) { String sql = "update shangpin set name='" + shangpin.getName() + "', chanjia='" + shangpin.getChanjia() + "', number='" + shangpin.getNumber()+"', guige='"+shangpin.getGuige()+ "', riqi='" + shangpin.getRiqi() + "', time='" + shangpin.getTime() + "', ruku='" + shangpin.getRuku() + "', songhuo='" + shangpin.getSonghuo() + "' where id='" + shangpin.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 shangpin 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 shangpin where id ='" + id + "'"; Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; Course shangpin = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); while (rs.next()) { String name = rs.getString("name"); String chanjia = rs.getString("chanjia"); String number = rs.getString("number"); String guige = rs.getString("guige"); String riqi = rs.getString("riqi"); String time = rs.getString("time"); String ruku = rs.getString("ruku"); String songhuo = rs.getString("songhuo"); shangpin = new Course(id, name, chanjia, number,guige,riqi, time, ruku, songhuo); } } catch (Exception e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return shangpin; } /** * 通过name得到Course * @param name * @return */ public Course getCourseByName(String name) { String sql = "select * from shangpin where name ='" + name + "'"; Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; Course shangpin = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); while (rs.next()) { int id = rs.getInt("id"); String chanjia = rs.getString("chanjia"); String number = rs.getString("number"); String guige = rs.getString("guige"); String riqi = rs.getString("riqi"); String time = rs.getString("time"); String ruku = rs.getString("ruku"); String songhuo = rs.getString("songhuo"); shangpin = new Course(id, name, chanjia, number,guige,riqi, time, ruku, songhuo); } } catch (Exception e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return shangpin; } /** * 查找 * @param name * @param teacher * @param classroom * @return */ public List<Course> search(String name, String chanjia, String number,String guige,String riqi,String time,String ruku,String songhuo) { String sql = "select * from shangpin where "; if (name != "") { sql += "name like '%" + name + "%'"; } if (chanjia != "") { sql += " chanjia like '%" + chanjia + "%'"; } if (number != "") { sql += " number like '%" + number + "%'"; } if (guige != "") { sql += " guige like '%" + guige + "%'"; } if (riqi != "") { sql += " guige like '%" + riqi + "%'"; } if (time != "") { sql += " guige like '%" + time + "%'"; } if (ruku != "") { sql += " guige like '%" + ruku + "%'"; } if (songhuo != "") { sql += " guige like '%" + songhuo + "%'"; } 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 chanjia2 = rs.getString("chanjia"); String number2 = rs.getString("number"); String guige2 = rs.getString("guige"); String riqi2 = rs.getString("riqi"); String time2 = rs.getString("time"); String ruku2 = rs.getString("ruku"); String songhuo2 = rs.getString("songhuo"); bean = new Course(id, name2, chanjia2, number2,guige2,riqi2, time2, ruku2, songhuo2); list.add(bean); } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return list; } /** * 全部数据 * @param name * @param teacher * @param classroom * @return */ public List<Course> list() { String sql = "select * from shangpin"; 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 chanjia2 = rs.getString("chanjia"); String number2 = rs.getString("number"); String guige2 = rs.getString("guige"); String riqi2 = rs.getString("riqi"); String time2 = rs.getString("time"); String ruku2 = rs.getString("ruku"); String songhuo2 = rs.getString("songhuo"); bean = new Course(id, name2, chanjia2, number2,guige2,riqi2, time2, ruku2, songhuo2); list.add(bean); } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return list; } }
package com.hjf.entity;
public class Course {
private int id;
private String name;
private String chanjia;
private String number;
private String guige;
private String riqi;
private String time;
private String ruku;
private String songhuo;
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 getChanjia() {
return chanjia;
}
public void setTeacher(String chanjia) {
this.chanjia = chanjia;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getGuige() {
return guige;
}
public void setGuige(String guige) {
this.guige= guige;
}
public String getRiqi() {
return riqi;
}
public void setRiqi(String riqi) {
this.riqi = riqi;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getRuku() {
return ruku;
}
public void setRuku(String ruku) {
this.ruku =ruku;
}
public String getSonghuo() {
return songhuo;
}
public void setSonghuo(String songhuo) {
this.songhuo =songhuo;
}
public Course() {}
public Course(int id, String name, String chanjia, String number,String guige,String riqi,String time,String ruku,String songhuo) {
this.id = id;
this.name = name;
this.chanjia = chanjia;
this.number = number;
this.guige=guige;
this.riqi=riqi;
this.time=time;
this.ruku=ruku;
this.songhuo=songhuo;
}
public Course(String name, String chanjia, String number,String guige,String riqi,String time,String ruku,String songhuo) {
this.name = name;
this.chanjia = chanjia;
this.number = number;
this.guige=guige;
this.riqi=riqi;
this.time=time;
this.ruku=ruku;
this.songhuo=songhuo;
}
}
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 shangpin) {
boolean f = false;
if(!cDao.name(shangpin.getName())) {
cDao.add(shangpin);
f = true;
}
return f;
}
/**
* 删除
*/
public void del(int id) {
cDao.delete(id);
}
/**
* 修改
* @return
*/
public void update(Course shangpin) {
cDao.update(shangpin);
}
/**
* 通过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 chanjia, String number,String guige,String riqi,String time,String ruku,String songhuo) {
return cDao.search(name,chanjia,number,guige,riqi,time,ruku,songhuo);
}
/**
* 全部数据
* @return
*/
public List<Course> list() {
return cDao.list();
}
}
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 chanjia = req.getParameter("chanjia");
String number = req.getParameter("number");
String guige = req.getParameter("guige");
String riqi = req.getParameter("riqi");
String time = req.getParameter("time");
String ruku = req.getParameter("ruku");
String songhuo = req.getParameter("songhuo");
Course shangpin = new Course(name,chanjia,number, guige,riqi,time,ruku,songhuo);
//添加后消息显示
if(service.add(shangpin)) {
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> shangpin = service.list();
req.setAttribute("courses", shangpin);
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 shangpin = service.getCourseById(id);
req.setAttribute("courses", shangpin);
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 shangpin = service.getCourseByName(name);
if(shangpin == null) {
req.setAttribute("message", "查无此商品!");
req.getRequestDispatcher("del.jsp").forward(req,resp);
} else {
req.setAttribute("courses", shangpin);
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 chanjia = req.getParameter("chanjia");
String number = req.getParameter("number");
String guige = req.getParameter("guige");
String riqi = req.getParameter("riqi");
String time = req.getParameter("time");
String ruku = req.getParameter("ruku");
String songhuo = req.getParameter("songhuo");
Course shangpin = new Course(name, chanjia,number, guige,riqi,time,ruku,songhuo);
service.update(shangpin);
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 chanjia = req.getParameter("chanjia");
String number = req.getParameter("number");
String guige = req.getParameter("guige");
String riqi = req.getParameter("riqi");
String time = req.getParameter("time");
String ruku = req.getParameter("ruku");
String songhuo = req.getParameter("songhuo");
List<Course> shangpin = service.search(name, chanjia,number, guige,riqi,time,ruku,songhuo);
req.setAttribute("courses", shangpin);
req.getRequestDispatcher("searchlist.jsp").forward(req,resp);
}
}
package com.hjf.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; 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/shangpin?useUnicode=true&characterEncoding=utf8&useSSL=false"; public static String db_user = "root"; public static String db_pass = "123"; public static Connection getConn () { Connection conn = null; try { Class.forName("com.mysql.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(); } } } public static void main(String[] args) throws SQLException { Connection conn = getConn(); PreparedStatement pstmt = null; ResultSet rs = null; String sql ="select * from course"; pstmt = conn.prepareStatement(sql); rs = pstmt.executeQuery(); if(rs.next()){ System.out.println("空"); }else{ System.out.println("不空"); } } }