添加订单

master
徐子才 6 years ago
parent 1dbd547656
commit 2f11430685

@ -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;
}
}

@ -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;
}
}

@ -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);
}
}

@ -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());
}
}
Loading…
Cancel
Save