📄 buyservlet.java
字号:
package jxc;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.*;
/**
* <p>Title: jxc demo</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author cwx
* @version 1.0
*/
public class BuyServlet extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GB2312";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//处理在进货信息录入界面中浏览器传送过来的参数
String supplierID = request.getParameter("supplierID");
if (supplierID == null) {
supplierID = "";
}
String wareID = request.getParameter("wareID");
if (wareID == null) {
wareID = "";
}
float buyPrice;
try {
buyPrice = Float.parseFloat(request.getParameter("buyPrice"));
}
catch(Exception e) {
buyPrice = 0f;
}
int buyQty;
try {
buyQty = Integer.parseInt(request.getParameter("buyQty"));
}
catch(NumberFormatException e) {
buyQty = 0;
}
String buyDate = request.getParameter("buyDate");
if (buyDate == null) {
buyDate = "";
}
String buyPerson = request.getParameter("buyPerson");
if (buyPerson == null) {
buyPerson = "";
}
//在参数处理完毕后,需要把进货信息添加到数据库中,因此需要改变下面这些代码
/* 注释部分为JBuilder自动生成的代码
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>BuyServlet</title></head>");
out.println("<body bgcolor=\"#ffffff\">");
out.println("<p>The servlet has received a " + request.getMethod() + ". This is the reply.</p>");
out.println("</body></html>");
*/
if(BuyManager.addBuy(supplierID, wareID, buyPrice, buyQty, buyDate, buyPerson)){
//转往销售结果显示界面
String supplierName = "", wareName = "", resQty = "";
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try{
conn = ConnectionManager.getConnection();
//读取供应商名称
stmt = conn.prepareStatement("select * from Supplier where supplierID=" +
supplierID);
rs = stmt.executeQuery();
if (rs.next())
supplierName = rs.getString("supplierName");
rs.close();
stmt.close();
//读取商品名称
stmt = conn.prepareStatement("select * from Ware where wareID=" +
wareID);
rs = stmt.executeQuery();
if (rs.next())
wareName = rs.getString("wareName");
rs.close();
stmt.close();
//读取商品库存数量
stmt = conn.prepareStatement("select * from Reserve where wareID=" +
wareID);
rs = stmt.executeQuery();
if (rs.next())
resQty = rs.getString("resQty");
rs.close();
}
catch(java.sql.SQLException e){
System.err.print(e);
}
finally {
//关闭数据库资源
if (stmt != null) {
try {
stmt.close();
} catch (Exception exception) {}
}
if (conn != null) {
try {
conn.close();
} catch (Exception exception) {}
}
}
request.setAttribute("supplierName", supplierName);
request.setAttribute("wareName", wareName);
request.setAttribute("buyPrice", new Float(buyPrice));
request.setAttribute("buyQty", new Integer(buyQty));
request.setAttribute("buyDate", buyDate);
request.setAttribute("buyPerson", buyPerson);
request.setAttribute("resQty", resQty);
getServletContext().getRequestDispatcher("/buyInfo.jsp").forward(request, response);
}
else{
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>BuyServlet</title></head>");
out.println("<body bgcolor=\"#ffffff\">");
out.println("<p>error!</p>");
out.println("</body></html>");
}
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
//Clean up resources
public void destroy() {
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -