连数据库层
package ConnectDb;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Connections {
private static String url = "jdbc:mysql://localhost:3306/4.0?useSSL=false&serverTimezone=UTC"; //数据库地址
private static String userName = "root"; //数据库用户名
private static String passWord = "xcl123"; //数据库密码
private static Connection conn = null;
@SuppressWarnings("unused")
private void DbHelper()
{
}
public static Connection getConnection()
{
if(conn==null)
{
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn=DriverManager.getConnection(url, userName, passWord);
}
catch (ClassNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return 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=getConnection();
PreparedStatement pstmt=null;
ResultSet rs=null;
String sql="select*from manager";
pstmt=conn.prepareStatement(sql);
rs=pstmt.executeQuery();
if(rs.next())
{
System.out.println("不空");
}
else
{
System.out.println("空");
}
}
}
dao层
package Dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;
import ConnectDb.Connections;
import Property.basis;
public class Daos
{
//查询所有用户信息
public List<basis> getAll()
{
List<basis> list=new ArrayList<basis>();
Connection conn=Connections.getConnection();//连接数据库
String sql="select*from manager";
try {
PreparedStatement pst=conn.prepareStatement(sql);
ResultSet rst=pst.executeQuery();
while(rst.next())
{
basis bs=new basis();
bs.setId(rst.getInt("id"));
bs.setName(rst.getString("name"));
bs.setChangjia(rst.getString("changjia"));
bs.setXinghao(rst.getString("xinghao"));
bs.setGuige(rst.getString("guige"));
bs.setShuliang(rst.getString("shuliang"));
bs.setDates(rst.getString("dates"));
bs.setTime(rst.getString("time"));
bs.setDanname(rst.getString("danname"));
bs.setSongname(rst.getString("songname"));
list.add(bs);
}
rst.close();
pst.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
public boolean addBasis(basis bs)
{
String sql = "insert into manager(name, changjia, xinghao,guige,shuliang,dates,time,danname,songname) values('" + bs.getName() + "','" + bs.getChangjia() + "','" + bs.getXinghao() + "','"+bs.getGuige()+"','"+bs.getShuliang()+"','"+bs.getDates()+"','"+bs.getTime()+"','"+bs.getDanname()+"','"+bs.getSongname()+"')";
//创建数据库链接
Connection conn = Connections.getConnection();
Statement state = null;
try {
state = conn.createStatement();
//state.executeUpdate(sql);
int a= state.executeUpdate(sql);
state.close();
return a>0?true:false;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public List<basis> getByName(String name,String dates)
{
List<basis> list=new ArrayList<basis>();
Connection conn=Connections.getConnection();
String sql=null;
basis bs=null;
if(name!=null)
{
sql="select*from manager where name like'%"+name+"%'";
}
if(dates!=null)
{
sql="select*from manager where dates like'%"+dates+"%'";
}
Statement state=null;
ResultSet rst=null;
try
{
state=conn.createStatement();
rst=state.executeQuery(sql);
while(rst.next())
{
bs=new basis();
bs.setName(rst.getString("name"));
bs.setChangjia(rst.getString("changjia"));
bs.setXinghao(rst.getString("xinghao"));
bs.setGuige(rst.getString("guige"));
bs.setShuliang(rst.getString("shuliang"));
bs.setDates(rst.getString("dates"));
bs.setTime(rst.getString("time"));
bs.setDanname(rst.getString("danname"));
bs.setSongname(rst.getString("songname"));
list.add(bs);
}
}
catch (SQLException e)
{
e.printStackTrace();
}
return list;
}
}
基础数据层
package Property;
public class basis
{
private int id;
private String name;
private String changjia;
private String xinghao;
private String guige;
private String shuliang;
private String dates;
private String time;
private String danname;
private String songname;
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 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 getDates() {
return dates;
}
public void setDates(String dates) {
this.dates = dates;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getDanname() {
return danname;
}
public void setDanname(String danname) {
this.danname = danname;
}
public String getSongname() {
return songname;
}
public void setSongname(String songname) {
this.songname = songname;
}
}
添加servlet层
package Servlets;
import java.io.IOException;
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 Dao.Daos;
import Property.basis;
/**
* Servlet implementation class AddServlet
*/
@WebServlet("/AddServlet")
public class AddServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
this.doPost(req, resp);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
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 dates=req.getParameter("dates");
String time=req.getParameter("time");
String danname=req.getParameter("danname");
String songname=req.getParameter("songname");
basis bs=new basis();
bs.setName(name);
bs.setChangjia(changjia);
bs.setXinghao(xinghao);
bs.setGuige(guige);
bs.setShuliang(shuliang);
bs.setDates(dates);
bs.setTime(time);
bs.setDanname(danname);
bs.setSongname(songname);
Daos dao=new Daos();
dao.addBasis(bs);//添加到数据库中
req.getRequestDispatcher("index.jsp").forward(req, resp);
}
}
显示全部数据层
package Servlets;
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 Dao.Daos;
import Property.basis;
@WebServlet("/ListServlet")
public class ListServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
Daos dao=new Daos();
List<basis> list=dao.getAll();
req.setAttribute("basisInfoList", list);
req.getRequestDispatcher("list.jsp").forward(req, resp);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
this.doGet(req, resp);
}
}
add.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>新增用户</title>
</head>
<body>
<form action="AddServlet"method="post">
<table align="center" width="450">
<tr>
<td align="center" colspan="2">
<h1>添加入库数据</h1>
<hr>
</td>
</tr>
<tr>
<td align="right">商品名称</td>
<td><input type="text"name="name"></td>
</tr>
<tr>
<td align="right">生产厂家</td>
<td><input type="text"name="changjia"></td>
</tr>
<tr>
<td align="right">型号</td>
<td><input type="text"name="xinghao"></td>
</tr>
<tr>
<td align="right">规格</td>
<td><input type="text"name="guige"></td>
</tr>
<tr>
<td align="right">数量</td>
<td><input type="text"name="shuliang"></td>
</tr>
<tr>
<td align="right">日期</td>
<td><input type="text"name="date"></td>
</tr>
<tr>
<td align="right">时间</td>
<td><input type="text"name="time"></td>
</tr>
<tr>
<td align="right">入库单位名称</td>
<td><input type="text"name="danname"></td>
</tr>
<tr>
<td align="right">送货人姓名</td>
<td><input type="text"name="songname"></td>
</tr>
<tr>
<td align="center"colspan="2">
<input type="submit" value="添 加">
</td>
</tr>
</table>
</form>
</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>
</head>
<body>
<div align="center">
<div class="a">
<a href="add.jsp">增加入库单数据</a>
</div>
<div class="a">
<a href="delete.jsp">删除入库单信息</a>
</div>
<div class="a">
<a href="update.jsp">查询入库单信息</a>
</div>
<div class="a">
<a href="list.jsp">查看入库单信息</a>
</div>
</div>
</body>
</html>
list.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@page import="java.util.List"%>
<%@page import="Property.basis"%>
<%@page import="Dao.Daos"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>所有用户</title>
<style type="text/css">
td {
font-size: 12px;
}
h2 {
margin: 0px
}
</style>
<script type="text/javascript">
</script>
</head>
<body>
<form action="list"method="post">
<h2 align="center">
<a href="add.jsp">添加新用户</a>
</h2>
<br>
<table align="center" width="700" border="10" height="250"
bordercolor="white" bgcolor="black" cellpadding="1" cellspacing="1">
<tr bgcolor="white">
<td align="center" colspan="15">
<h2>所有用户信息</h2>
</td>
</tr>
<tr align="center" bgcolor="#e1ffc1">
<td><b>ID</b></td>
<td><b>商品名称</b></td>
<td><b>生产厂家</b></td>
<td><b>型号</b></td>
<td><b>规格</b></td>
<td><b>数量</b></td>
<td><b>日期</b></td>
<td><b>时间</b></td>
<td><b>入库单位名称</b></td>
<td><b>送货人姓名</b></td>
<td colspan="2"><b>操作</b></td>
</tr>
<%
// 获取用户信息集合
//List<basis> list = (List<basis>) request.getAttribute("basisInfoList");
Daos dao=new Daos();
List<basis> list=dao.getAll();
// 判断是否有数据
if (list == null || list.size() < 1) {
%>
<tr bgcolor="white"><td colspan="15" ><h4 align="center">没有数据</h4></td></tr>
<%
} else {
// 遍历用户集合中的数据
for (basis bs : list) {
%>
<tr align="center" bgcolor="white">
<td><%=bs.getId()%></td>
<td><%=bs.getName()%></td>
<td><%=bs.getChangjia()%></td>
<td><%=bs.getXinghao()%></td>
<td><%=bs.getGuige()%></td>
<td><%=bs.getShuliang()%></td>
<td><%=bs.getDates()%></td>
<td><%=bs.getTime()%></td>
<td><%=bs.getDanname()%></td>
<td><%=bs.getSongname()%></td>
<td >
<a href="update?id=<%=bs.getId()%>">修改</a>
</td>
<td>
<a href="delete?id=<%=bs.getId()%>">删除</a>
</td>
</tr>
<%
}
}
%>
</table>
</form>
</body>
</html>
show1.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@page import="java.util.List"%>
<%@page import="Property.basis"%>
<%@page import="Dao.Daos"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<table align="center">
<tr align="center" >
<td><b>商品名称</b></td>
<td><b>生产厂家</b></td>
<td><b>型号</b></td>
<td><b>规格</b></td>
<td><b>数量</b></td>
<td><b>日期</b></td>
<td><b>时间</b></td>
<td><b>入库单位名称</b></td>
<td><b>送货人姓名</b></td>
</tr>
<%
Daos dao=new Daos();
List<basis> list=dao.getByName(request.getParameter("name"),request.getParameter("dates")
);
for (basis bs : list){
%>
<tr align="center" bgcolor="white">
<td><%=bs.getName()%></td>
<td><%=bs.getChangjia()%></td>
<td><%=bs.getXinghao()%></td>
<td><%=bs.getGuige()%></td>
<td><%=bs.getShuliang()%></td>
<td><%=bs.getDates()%></td>
<td><%=bs.getTime()%></td>
<td><%=bs.getDanname()%></td>
<td><%=bs.getSongname()%></td>
<%
}
%>
</table>
</body>
</html>
update.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>
</head>
<body>
<div align="center">
<h1 style="color: black;">查询</h1>
<a href="index.jsp">返回主页</a>
<form action="show1.jsp" method="post" >
<div class="a">
商品名称<input type="text" name="name"/>
</div>
<div class="a">
出入库日期<input type="text" name="dates" />
</div>
<div class="a">
<button type="submit" class="b">查 询</button>
</div>
</form>
</div>
</body>
</html>
查看所有数据
查询页面
添加页面