From 2f11430685ebb2f2c19eb11ddd3b11036af37968 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E5=AD=90=E6=89=8D?= <2296566318@qq.com> Date: Sun, 13 Jan 2019 16:52:13 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=AE=A2=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 后端/org/use/dao/PballsDao.java | 66 ++++++++++++++++++++ 后端/org/use/entity/Pballs.java | 47 ++++++++++++++ 后端/org/use/servlet/AddPballsServlet.java | 47 ++++++++++++++ 后端/org/use/util/DbHelper.java | 37 +++++++++++ 4 files changed, 197 insertions(+) create mode 100644 后端/org/use/dao/PballsDao.java create mode 100644 后端/org/use/entity/Pballs.java create mode 100644 后端/org/use/servlet/AddPballsServlet.java create mode 100644 后端/org/use/util/DbHelper.java diff --git a/后端/org/use/dao/PballsDao.java b/后端/org/use/dao/PballsDao.java new file mode 100644 index 0000000..b77a4b6 --- /dev/null +++ b/后端/org/use/dao/PballsDao.java @@ -0,0 +1,66 @@ +package dao; + +import entity.Pballs; +import util.DbHelper; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +public class PballsDao{ + /** + * 添加订单 + * + * @param pballs + * @return + */ + public int addPballs(Pballs pballs) { + String sql = "INSERT INTO pballs(name,ball,btime) VALUES(?,?,?)"; + int count = 0; + Connection conn = DbHelper.getConnection(); + try { + PreparedStatement pst = conn.prepareStatement(sql); + //pst.setInt(1, pballs.getId()); + // pst.setInt(2, pballs.getPid()); + pst.setString(1,pballs.getName()); + pst.setString(2,pballs.getBall()); + pst.setString(3,pballs.getBtime()); + // pst.setInt(6, pballs.getNumberNow()); + count = pst.executeUpdate(); + pst.close(); + conn.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + return count; + } + /** + * 查询当前订单 + * + * @param pballs.id + * @return + */ + public Pballs getPballsById(int id) { + Connection conn = DbHelper.getConnection(); + String sql = "select * from Pballs where id = " + id; + Pballs pballs = new Pballs(); + try { + PreparedStatement pst = conn.prepareStatement(sql); + ResultSet rst = pst.executeQuery(); + while (rst.next()) { + pballs.setId(rst.getInt("id")); + pballs.setPid(rst.getInt("pid")); + pballs.setName(rst.getString("name")); + pballs.setBall(rst.getString("ball")); + pballs.setBtime(rst.getString("btime")); + pballs.setNumberNow(rst.getInt("numberNow")); + } + rst.close(); + pst.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + return pballs; + } + } + diff --git a/后端/org/use/entity/Pballs.java b/后端/org/use/entity/Pballs.java new file mode 100644 index 0000000..309ab87 --- /dev/null +++ b/后端/org/use/entity/Pballs.java @@ -0,0 +1,47 @@ +package entity; + +public class Pballs { + private int id; + private int pid; + private String name; + private String ball; + private String btime; + private int numberNow; + public int getId() { + return id; + } + public void setId(int id) { + this.id = id; + } + public int getPid() { + return pid; + } + public void setPid(int pid) { + this.pid = pid; + } + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public String getBall() { + return ball; + } + public void setBall(String ball) { + this.ball = ball; + } + public String getBtime() { + return btime; + } + public void setBtime(String btime) { + this.btime = btime; + } + public int getNumberNow() { + return numberNow; + } + public void setNumberNow(int numberNow) { + this.numberNow = numberNow; + } + +} diff --git a/后端/org/use/servlet/AddPballsServlet.java b/后端/org/use/servlet/AddPballsServlet.java new file mode 100644 index 0000000..0723912 --- /dev/null +++ b/后端/org/use/servlet/AddPballsServlet.java @@ -0,0 +1,47 @@ +package servlet; +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.PballsDao; +import entity.Pballs; + +import java.io.IOException; + + +@WebServlet("/addPballs") +public class AddPballsServlet extends HttpServlet { + + /** + * + */ + private static final long serialVersionUID = 1L; + @Override + public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + req.getRequestDispatcher("yq.jsp").forward(req, resp); + } + + @Override + public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + req.setCharacterEncoding("utf-8"); + Pballs pballs = new Pballs(); + // int id = Integer.parseInt(req.getParameter("id")); + // int pid = Integer.parseInt(req.getParameter("pid")); + String name = req.getParameter("name"); + String ball = req.getParameter("ball"); + String btime=req.getParameter("btime"); + // int numberNow=Integer.parseInt(req.getParameter("numberNow")); + pballs.setId(1); + pballs.setPid(1); + pballs.setName(name); + pballs.setBall(ball); + pballs.setBtime(btime); + pballs.setNumberNow(1); + System.out.println(pballs); + PballsDao pballsDao = new PballsDao(); + pballsDao.addPballs(pballs); + req.getRequestDispatcher("yq.jsp").forward(req,resp); + } +} \ No newline at end of file diff --git a/后端/org/use/util/DbHelper.java b/后端/org/use/util/DbHelper.java new file mode 100644 index 0000000..250d73d --- /dev/null +++ b/后端/org/use/util/DbHelper.java @@ -0,0 +1,37 @@ +package util; + +import java.sql.Connection; +import java.sql.DriverManager; + + +public class DbHelper { + private static String url = "jdbc:mysql://localhost:3306/yqb"; // 数据库地址 + private static String userName = "root"; // 数据库用户名 + private static String passWord = "123456"; // 数据库密码 + private static Connection conn = null; + /** + * 获得数据库连接 + * + * @return + */ + public static Connection getConnection() { + if (null == conn) { + try { + Class.forName("com.mysql.cj.jdbc.Driver"); + conn = DriverManager.getConnection(url, userName, passWord); + } catch (Exception e) { + e.printStackTrace(); + } + } + return conn; + } + /** + * 测试数据库是否连通 + * + * @param args + */ + public static void main(String[] args) { + System.err.println(getConnection()); + } + +}