67,512
社区成员
发帖
与我相关
我的任务
分享
public class SqlHelper
{
private static Connection ct=null;//连接
private static ResultSet rs=null;//结果
private static PreparedStatement ps=null;
public static void executeUpdate(String sql, Object[] parameters) {
try {
// 1.创建一个ps
ct=DBUtil.getCon();
ps = ct.prepareStatement(sql);
// 给?赋值
if (parameters != null)
for (int i = 0; i < parameters.length; i++) {
ps.setObject(i + 1, parameters[i]);
}
// 执行
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();// 开发阶段
throw new RuntimeException(e.getMessage());
} finally {
// 关闭资源
DBUtil.close(rs, ps, ct);
}
}
public class cargo {
private int cid;
private String cname;
private float cprice;
private String cfactory;
private int cinventory;
public cargo(String cname,float cprice,String cfactory,int cinventory ){
super();
this.cname=cname;
this.cprice=cprice;
this.cfactory=cfactory;
this.cinventory=cinventory;
}
public int getCid() {
return cid;
}
public void setCid(int cid) {
this.cid = cid;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public float getCprice() {
return cprice;
}
public void setCprice(float cprice) {
this.cprice = cprice;
}
public String getCfactory() {
return cfactory;
}
public void setCfactory(String cfactory) {
this.cfactory = cfactory;
}
public int getCinventory() {
return cinventory;
}
public void setCinventory(int cinventory) {
this.cinventory = cinventory;
}
}
public class addCargoUI extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
String name=request.getParameter("name");
float price=Float.parseFloat(request.getParameter("price").toString());
String factory=request.getParameter("factory");
int inventory=Integer.parseInt(request.getParameter("inventory").toString());
cargo cargo=new cargo(name, price, factory, inventory);
cargoService cargoService=new cargoService();
if(cargoService.addCargo(cargo)){
request.getRequestDispatcher("cargo.jsp").forward(request, response);
}
else {
request.getRequestDispatcher("cargo.jsp").forward(request, response);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
public class cargoService {public boolean addCargo(cargo cargo){
String sql="insert into cargo(cname,cprice,cfactory,cinventory) values(?,?,?,?)";
String name=cargo.getCname();
float price=cargo.getCprice();
String factory=cargo.getCfactory();
int inventory=cargo.getCinventory();
Object[] parameters={name,price,factory,inventory};
SqlHelper.executeUpdate(sql, parameters);
return true;
}
}
<body>
<div class="login">
<h1>添加餐具</h1>
<form action="/qym/addCargoUI" method="post">
<input type="text" name="name" placeholder="商品名" " required='required' oninvalid="setCustomValidity('请填写商品名')" />
<input type="text" name="price" placeholder="价格" " required='required' oninvalid="setCustomValidity('请填写价格')" />
<input type="text" name="factory" placeholder="制造商" " required='required' oninvalid="setCustomValidity('请填写制造商')" />
<input type="text" name="inventory" placeholder="库存" " required='required' oninvalid="setCustomValidity('请填写库存')" oninput="setCustomValidity('')" />
<button type="submit" class="btn btn-primary btn-block btn-large">添加</button>
</form>
</div>
</body>