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.User;
import com.hjf.util.DBUtil;
public class Dao {
public boolean add(User user) {
String sql = "insert into user( name, changjia,xinghao,guige,shuliang,riqi,danwei,xingming) values('" + user.getName() + "','" + user.getChangjia() + "','" + user.getXinghao() + "','" + user.getGuige() + "','" + user.getShuliang() + "','" + user.getRiqi() +"','" + user.getDanwei() +"','" + user.getXingming() +"')";
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;
}
public boolean delete (String name) {
boolean f = false;
String sql = "delete from user where name='" + name + "'";
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;
}
public boolean update(User user) {
String sql = "update user set guige='"+user.getGuige()+ "', changjia='" + user.getChangjia() + "', xinghao='" + user.getXinghao()+ "', shuliang='" + user.getShuliang() +"', riqi='" + user.getRiqi() +"', danwei='" + user.getDanwei()+"', xingming='" + user.getXingming() +"'where name='" + user.getName() + "'";
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;
}
public boolean name(String name) {
boolean flag = false;
String sql = "select name from user 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;
}
public User getUserByName(String name) {
String sql = "select * from user where name ='" + name + "'";
Connection conn = DBUtil.getConn();
Statement state = null;
ResultSet rs = null;
User user= null;
try {
state = conn.createStatement();
rs = state.executeQuery(sql);
while (rs.next()) {
String name1 = rs.getString("name");
String changjia= rs.getString("changjia");
String xinghao= rs.getString("xinghao");
String guige =rs.getString("guige");
String shuliang=rs.getString("shuliang");
String riqi=rs.getString("riqi");
String danwei=rs.getString("danwei");
String xingming=rs.getString("xingming");
user = new User(name1,changjia,xinghao,guige,shuliang,riqi,danwei,xingming);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.close(rs, state, conn);
}
return user;
}
public List<User> search(String name,String changjia,String xinghao,String guige,String shuliang,String riqi,String danwei,String xingming ) {
String sql = "select * from user where ";
if (name != "") {
sql += "name like '%" + name + "%'";
}
if (changjia!= "") {
sql += "changjia like '%" + changjia + "%'";
}
if (xinghao != "") {
sql += "xinghao like '%" + xinghao+ "%'";
}
if (guige != "") {
sql += "guige like '%" + guige+ "%'";
}
if (shuliang != "") {
sql += "shuliang like '%" + shuliang + "%'";
}
if (riqi != "") {
sql += "riqi like '%" + riqi + "%'";
}
if (danwei != "") {
sql += "danwei like '%" + danwei+ "%'";
}
if (xingming != "") {
sql += "xingming like '%" + xingming + "%'";
}
List<User> list = new ArrayList<>();
Connection conn = DBUtil.getConn();
Statement state = null;
ResultSet rs = null;
try {
state = conn.createStatement();
rs = state.executeQuery(sql);
User bean = null;
while (rs.next()) {
String name1 = rs.getString("name");
String changjia1= rs.getString("changjia");
String xinghao1= rs.getString("xinghao");
String guige1 =rs.getString("guige");
String shuliang1=rs.getString("shuliang");
String riqi1=rs.getString("riqi");
String danwei1=rs.getString("danwei");
String xingming1=rs.getString("xingming");
bean = new User(name1,changjia1,xinghao1,guige1,shuliang1,riqi1,danwei1,xingming1);
list.add(bean);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(rs, state, conn);
}
return list;
}
public List<User> list() {
String sql = "select * from user";
List<User> list = new ArrayList<>();
Connection conn = DBUtil.getConn();
Statement state = null;
ResultSet rs = null;
try {
state = conn.createStatement();
rs = state.executeQuery(sql);
User bean = null;
while (rs.next()) {
String name = rs.getString("name");
String changjia= rs.getString("changjia");
String xinghao= rs.getString("xinghao");
String guige =rs.getString("guige");
String shuliang=rs.getString("shuliang");
String riqi=rs.getString("riqi");
String danwei=rs.getString("danwei");
String xingming=rs.getString("xingming");
bean= new User(name,changjia,xinghao,guige,shuliang,riqi,danwei,xingming);
list.add(bean);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(rs, state, conn);
}
return list;
}
}
package com.hjf.entity;
import java.sql.Connection;
public class User {
private String name;
private String changjia;
private String xinghao;
private String guige;
private String shuliang;
private String riqi;
private String danwei;
private String xingming;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public String getChangjia() {
return changjia;
}
public void setChangjia(String changjia) {
this.changjia = changjia;
}
public String getXinghao() {
return xinghao;
}
public void setXinghao(String xinghao) {
this.xinghao = xinghao;
}
public String getGuige() {
return guige;
}
public void setGuige(String guige) {
this.guige = guige;
}
public String getShuliang() {
return shuliang;
}
public void setShuliang(String shuliang) {
this.shuliang = shuliang;
}
public String getRiqi() {
return riqi;
}
public void setRiqi(String riqi) {
this.riqi = riqi;
}
public String getDanwei() {
return danwei;
}
public void setDanwei(String danwei) {
this.danwei = danwei;
}
public String getXingming() {
return xingming;
}
public void setXingming(String xingming) {
this.xingming = xingming;
}
public User(String name,String changjia,String xinghao,String guige,String shuliang,String riqi,String danwei,String xingming ) {
this.name = name;
this.changjia = changjia;
this.xinghao = xinghao;
this.guige = guige;
this.shuliang = shuliang;
this.riqi = riqi;
this.danwei = danwei;
this.xingming = xingming;
}
public User() {
// TODO Auto-generated constructor stub
}
}
package com.hjf.service;
import java.util.List;
import com.hjf.dao.Dao;
import com.hjf.entity.User;
public class UserService {
Dao cDao = new Dao();
public boolean add(User user) {
boolean f = false;
if(!cDao.name(user.getName())) {
cDao.add(user);
f = true;
}
return f;
}
public void del(String name) {
cDao.delete(name);
}
public void update(User user) {
cDao.update(user);
}
public User getUserByName(String name) {
return cDao.getUserByName(name);
}
public List<User> search(String name,String changjia,String xinghao,String guige,String shuliang,String riqi,String danwei,String xingming ) {
return cDao.search(name,changjia,xinghao,guige,shuliang,riqi,danwei,xingming);
}
public List<User> 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.User;
import com.hjf.service.UserService;
/**
* Servlet implementation class UserServlet
*/
@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
UserService service = new UserService();
/**
* 方法选择
*/
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 ("getuserbyid".equals(method)) {
getUserById(req, resp);
} else if ("getuserbyname".equals(method)) {
getUserByName(req, resp);
} else if ("list".equals(method)) {
list(req, resp);
}
}
/**
* 添加
* @param <service>
* @param req
* @param resp
* @throws IOException
* @throws ServletException
*/
private <service> void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
req.setCharacterEncoding("utf-8");
String name = req.getParameter("name");
String changjia= req.getParameter("changjia");
String xinghao=req.getParameter("xinghao");
String guige =req.getParameter("guige");
String shuliang=req.getParameter("shuliang");
String riqi=req.getParameter("riqi");
String danwei=req.getParameter("danwei");
String xingming=req.getParameter("xingming");
User user = new User(name,changjia,xinghao,guige,shuliang,riqi,danwei,xingming);
//添加后消息显示
if(service.add(user)) {
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<User> users = service.list();
req.setAttribute("users", users);
req.getRequestDispatcher("list.jsp").forward(req,resp);
}
/**
* 通过ID得到User
* @param req
* @param resp
* @throws ServletException
*/
private void getUserById(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
req.setCharacterEncoding("utf-8");
String name= req.getParameter("name");
User user = service.getUserByName(name);
req.setAttribute("user", user);
req.getRequestDispatcher("detail2.jsp").forward(req,resp);
}
/**
* 通过名字查找
* 跳转至删除
* @param req
* @param resp
* @throws IOException
* @throws ServletException
*/
private void getUserByName(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
req.setCharacterEncoding("utf-8");
String name = req.getParameter("name");
User user = service.getUserByName(name);
if(user == null) {
req.setAttribute("message", "查无此商品!");
req.getRequestDispatcher("del.jsp").forward(req,resp);
} else {
req.setAttribute("user", user);
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");
String name = req.getParameter("name");
service.del(name);
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");
String name = req.getParameter("name");
String changjia= req.getParameter("changjia");
String xinghao=req.getParameter("xinghao");
String guige =req.getParameter("guige");
String shuliang=req.getParameter("shuliang");
String riqi=req.getParameter("riqi");
String danwei=req.getParameter("danwei");
String xingming=req.getParameter("xingming");
User user = new User(name,changjia,xinghao,guige,shuliang,riqi,danwei,xingming);
service.update(user);
req.setAttribute("message", "修改成功");
req.getRequestDispatcher("UserServlet?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 changjia= req.getParameter("changjia");
String xinghao= req.getParameter("xinghao");
String guige =req.getParameter("guige");
String shuliang=req.getParameter("shuliang");
String riqi=req.getParameter("riqi");
String danwei=req.getParameter("danwei");
String xingming=req.getParameter("xingming");
List<User> users = service.search(name,changjia,xinghao,guige,shuliang,riqi,danwei,xingming);
req.setAttribute("users", users);
req.getRequestDispatcher("searchlist.jsp").forward(req,resp);
}
}
package com.hjf.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBUtil {
public static String db_url = "jdbc:mysql://127.0.0.1:3306/xinxiguanli?useUnicode=true&characterEncoding=utf-8";
public static String db_user = "root";
public static String db_pass = "704794513";
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();
}
}
}
}
<%@ 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 background="C:/lol.jpg">
<body>
<center>
<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="NewFile.jsp">商品删除</a>
</div>
<div class="a">
<a href="search.jsp">商品查看</a>
</div>
</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 background="C:/lol.jpg">
<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="UserServlet?method=add" method="post" οnsubmit="return check()">
<div class="a">
分类名<input type="text" name="name" />
</div>
<div class="a">
厂家<input type="text" name="changjia" />
</div>
<div class="a">
型号<input type="text" name="xinghao"/>
</div>
<div class="a">
规格<input type="text" name="guige"/>
</div>
<div class="a">
数量<input type="text" name="shuliang" />
</div>
<div class="a">
日期<input type="text" name="riqi" />
</div>
<div class="a">
单位名称<input type="text" name="danwei" />
</div>
<div class="a">
人名<input type="text" name="xingming"/>
</div>
<div class="a">
<input type="submit" value="添加" onClick="delcfm1()" />
</div>
</form>
</div>
<script language="javascript">
function delcfm1() {
if (!confirm("确认添加")) {
window.event.returnValue = false;
}
}
</script>
</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>
<body background="C:/lol.jpg">
</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="UserServlet?method=getuserbyname" method="post" οnsubmit="return check()">
<div class="a">
商品名称<input type="text" name="name" />
</div>
<div class="a">
<button type="submit" class="b">查 找<tton>
</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>
<%@ 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>
<body background="C:/lol.jpg">
<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>${user.name
}</td>
</tr>
<tr>
<td>厂家</td>
<td>${user.changjia}</td>
</tr>
<tr>
<td>型号</td>
<td>${user.xinghao}</td>
</tr>
<tr>
<td>规格</td>
<td>${user.guige}</td>
</tr>
<tr>
<td>数量</td>
<td>${user.shuliang}</td>
</tr>
<tr>
<td>日期</td>
<td>${user.riqi}</td>
</tr>
<tr>
<td>单位</td>
<td>${user.danwei}</td>
</tr>
<tr>
<td>姓名</td>
<td>${user.xingming}</td>
</tr>
</table>
<div class="a">
<a οnclick="return check()" href="UserServlet?method=del&name=${user.name
}">删 除</a>
</div>
</div>
<script type="text/javascript">
function check() {
if (confirm("删除?")){
return true;
}else{
return false;
}
}
</script>
</body>
</html>
<%@page import="entity.Test"%>
<%@page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>修改界面</title>
</head>
<body>
<form action="uqdateresult.jsp" method="post" id="form" >
<input type="hidden" name="methodName" value="0"/>
<h4> 商品名称 :<input type="text" name="name"title="不能为空"></input><br></h4>
<h4> 成产厂商 :<input type="text" name="name_C"title="不能为空"></input><br></h4>
<h4> 商品型号(数字) :<input type="text" name="id"title="不能为空"></input><br></h4>
<h4> 商品规格 :<input type="text" name="Gui"title="不能为空"></input><br></h4>
<h4> :<input type="text" name="Xing_hao"title="不能为空"></input><br></h4>
<h4> 商品数量(数字) :<input type="text" name="num"title="不能为空"></input><br></h4>
<h4> 送货(取货)单位 :<input type="text" name="name_Dan"title="不能为空"></input><br></h4>
<h4> 送货(取货)人姓名:<input type="text" name="name_peo"title="不能为空"></input><br></h4>
<h4> 日期 :<input type="text" name="Date"title="不能为空"></input><br></h4>
<input type="submit" value="完成"/>
</form>
</body>
</html>
过程:因为之前学长的讲解,看见这个题的时候其实也已经有点思路了。我一开始先从java开始入手,遵从学长的讲解,先从数据库的连接入手。之后我挨个写了每个变量的构造函数,之后用java Bean的具体操作办法完成了每个变量的带入与检验,还有之前的service层,其实那个servlet我有点不理解。之前就抽过时间好好看过。但还是有一点不太理解,对于它的功能总是有点模糊不清。还有那个出库的问题,我一开始想的太复杂了。老感觉那个出库,然后还有每个商品数量的问题。有点蒙圈,之后才知道没这么复杂。
然后就按部就班完成jsp中增删改查的工作,这也是因为有之前学长的铺垫我才能全部完成。最后在修改那出了点问题,回到宿舍还是解决了这个问题。
总结:自己之前非常懵的原因,其实就是自己不认真学习这方面的原因。尤其是这个servlet的很多事情,我一开始第一回做这个功能的时候,只用了java +java Bean完成了 数据的增和删,但之后也知道期末考试是java+java Bean+servlet 所以也在书上看了好多次,因为具体实践不够,所以真的不能完全理解。这次实践又加深了我对它的理解。自己之后也要加强这个的实践。