📄 productaction.java
字号:
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.accp.struts.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.upload.FormFile;
import com.accp.struts.Bean.DBConnection;
import com.accp.struts.form.ProductForm;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class ProductAction extends DispatchAction {
// 增加商品
public ActionForward addProduct(ActionMapping actionMapping,
ActionForm actionForm,
HttpServletRequest request,
HttpServletResponse response) {
ProductForm f = (ProductForm) actionForm;
ActionForward forward = new ActionForward();
//取得checkbox表单数据
// String productAddress = "";
// for(int i=0;i<agentProductForm.getProductAddress().length;i++){
// if(productAddress.equals("")){
// productAddress = agentProductForm.getProductAddress()[i] ;
// }
// else{
// productAddress =productAddress + "," + agentProductForm.getProductAddress()[i] ;
// }
// }
// System.out.println("productAddress:"+productAddress);
System.out.println("1");
//取得时间
Calendar applyDate = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String applyTime = sdf.format(applyDate.getTime());
//定义数据对象
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
/*
//保存文件到相应的目录
FormFile uploadFile = null;//定义图片文件
uploadFile = ProductForm.getUploadFile();//取得图片文件
String fileName = uploadFile.getFileName();//取得图片文件名称
//相片保存地址
String dir = servlet.getServletContext().getRealPath("/images/UserPic/");
//上传图片到相应的目录
String fileName_path = dir + "\\" + fileName;
InputStream streamIn_one = uploadFile.getInputStream();
OutputStream streamOut_one = new FileOutputStream(fileName_path);
int bytesRead1 = 0;
byte[] buffer1 = new byte[8192];
while ( (bytesRead1 = streamIn_one.read(buffer1, 0, 8192)) != -1) {
streamOut_one.write(buffer1, 0, bytesRead1);
}
streamOut_one.close();
streamIn_one.close();
System.out.println("2");
//取得最大商品ID值
int productId = 0;
conn = DBConnection.getConnection();
String sql = "select max(product_id) from product ";
System.out.println("sql:"+sql);
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
if (rs.next()) {
productId = rs.getInt(1)+1;
}
*/
System.out.println("3");
conn=DBConnection.getConnection();
//增加商品
String sql = "insert into producttable1 values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1,Integer.parseInt(f.getProduct_id()));
pstmt.setString(2, f.getProduct_name());
pstmt.setString(3, f.getNew_level());
pstmt.setFloat(4, Float.parseFloat(f.getPrice()));
pstmt.setString(5, f.getStart_date());
pstmt.setString(6, f.getEnd_date());
pstmt.setString(7, "A0");
pstmt.setString(8, f.getDescription());
pstmt.setString(9, f.getAddress_picture());
pstmt.setInt(10, Integer.parseInt(f.getStore_number()));
pstmt.setString(11, f.getShow_main());
pstmt.setString(12, f.getShow_command());
pstmt.setString(13, f.getAddress());
pstmt.setString(14, f.getMeno());
pstmt.setString(15, f.getCreate_time());
int result = pstmt.executeUpdate();
if (result > 0) {
forward = actionMapping.findForward("success");
} else {
forward = actionMapping.findForward("error");
}
} catch (Exception e) {
e.printStackTrace();
forward = actionMapping.findForward("error");
} finally {
try{
if(rs!=null)rs.close();
if(pstmt!=null)pstmt.close();
}catch(Exception ex){
System.out.println(ex);
}
}
System.out.println("forward:" + forward);
return forward;
}
//商品下架
public ActionForward updateStateProduct(ActionMapping actionMapping,
ActionForm actionForm,
HttpServletRequest request,
HttpServletResponse response) {
ProductForm agentProductForm = (ProductForm) actionForm;
ActionForward forward = new ActionForward();
//定义数据对象
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try{
//修改商品
conn = DBConnection.getConnection();
String sql = "update producttable1 set state=A2 where product_id =?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1,Integer.parseInt(agentProductForm.getProduct_id()));
int result = pstmt.executeUpdate();
if(result > 0){
forward = actionMapping.findForward("modifyProductSuccess");
}
else{
forward = actionMapping.findForward("failure");
}
}
catch(Exception e){
e.printStackTrace();
}
finally{
try{
if(rs!=null)rs.close();
if(pstmt!=null)pstmt.close();
}catch(SQLException ex){
System.out.println(ex);
}
}
System.out.println("forward:"+forward);
return forward;
}
/**
* 修改商品信息
* @param actionMapping ActionMapping
* @param actionForm ActionForm
* @param request HttpServletRequest
* @param response HttpServletResponse
* @return ActionForward
*/
public ActionForward modifyProduct(ActionMapping actionMapping,
ActionForm actionForm,
HttpServletRequest request,
HttpServletResponse response) {
ProductForm f = (ProductForm) actionForm;
ActionForward forward = new ActionForward();
//取得时间
Calendar applyDate = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String applyTime = sdf.format(applyDate.getTime());
//定义数据对象
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String productId = request.getParameter("product_id");//取得主键值
System.out.println("主见只为:"+productId);
try{
/*
//保存文件到相应的目录
FormFile uploadFile = null;//定义图片文件
uploadFile = f.getUploadFile();//取得图片文件
String fileName = uploadFile.getFileName();//取得图片文件名称
//相片保存地址
String dir = servlet.getServletContext().getRealPath("/images/UserPic/");
//上传图片到相应的目录
String fileName_path = dir + "\\" + fileName;
InputStream streamIn_one = uploadFile.getInputStream();
OutputStream streamOut_one = new FileOutputStream(fileName_path);
int bytesRead1 = 0;
byte[] buffer1 = new byte[8192];
while ( (bytesRead1 = streamIn_one.read(buffer1, 0, 8192)) != -1) {
streamOut_one.write(buffer1, 0, bytesRead1);
}
streamOut_one.close();
streamIn_one.close();
*/
//修改商品
conn = DBConnection.getConnection();
System.out.println(f.getProduct_id());
System.out.println(f.getProduct_name());
System.out.println(f.getAddress_picture());
System.out.println(f.getNew_level());
String sql = "update producttable1 set product_name=?,new_level=?,price=?,start_date=?,end_date=?,state=?,description=?,address_picture=?,store_number=?,show_main=?,show_command=?,address=?,meno=?,create_time=? where product_id =?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1,f.getProduct_name());
pstmt.setString(2,f.getNew_level());
pstmt.setString(3,f.getPrice());
pstmt.setString(4,f.getStart_date());
pstmt.setString(5,f.getEnd_date());
pstmt.setString(6,"A0");
pstmt.setString(7,f.getDescription());
pstmt.setString(8,f.getAddress_picture());
pstmt.setInt(9,Integer.parseInt(f.getStore_number()));
pstmt.setString(10,f.getShow_main());
pstmt.setString(11,f.getShow_command());
pstmt.setString(12,f.getAddress());
pstmt.setString(13,f.getMeno());
pstmt.setString(14,f.getCreate_time());
pstmt.setInt(15,Integer.parseInt(f.getProduct_id()));
int result = pstmt.executeUpdate();
if(result > 0){
forward = actionMapping.findForward("success");
}
else{
forward = actionMapping.findForward("error");
}
}
catch(Exception e){
System.out.println(e.getMessage());
forward = actionMapping.findForward("error");
}
finally{
try{
if(rs!=null)rs.close();
if(pstmt!=null)pstmt.close();
}catch(SQLException ex){
System.out.println(ex.getMessage());
forward=actionMapping.findForward("error");
}
}
System.out.println("forward:"+forward);
return forward;
}
/**
* 商家管理员审核上架商品
* @param actionMapping ActionMapping
* @param actionForm ActionForm
* @param request HttpServletRequest
* @param response HttpServletResponse
* @return ActionForward
*/
public ActionForward auditProduct(ActionMapping actionMapping,
ActionForm actionForm,
HttpServletRequest request,
HttpServletResponse response) {
ProductForm f = (ProductForm) actionForm;
ActionForward forward = new ActionForward();
//定义数据对象
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try{
conn = DBConnection.getConnection();
String sql = "update producttable1 set state =? where product_id =?";
pstmt = conn.prepareStatement(sql);
System.out.println(123);
pstmt.setString(1,"A1");
System.out.println(f.getProduct_id());
pstmt.setInt(2,Integer.parseInt(f.getProduct_id()));
int result = pstmt.executeUpdate();
if(result > 0){
forward = actionMapping.findForward("success");
}
else{
forward = actionMapping.findForward("error");
}
}
catch(Exception e){
e.printStackTrace();
}
finally{
try{
if(rs!=null)rs.close();
if(pstmt!=null)pstmt.close();
}catch(SQLException ex){
System.out.println(ex);
}
}
System.out.println("forward:"+forward);
return forward;
}
/**
* 商家管理员审核下架商品
* @param actionMapping ActionMapping
* @param actionForm ActionForm
* @param request HttpServletRequest
* @param response HttpServletResponse
* @return ActionForward
*/
public ActionForward deleteProduct(ActionMapping actionMapping,
ActionForm actionForm,
HttpServletRequest request,
HttpServletResponse response) {
ProductForm f = (ProductForm) actionForm;
ActionForward forward = new ActionForward();
//定义数据对象
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try{
conn = DBConnection.getConnection();
String sql = "update producttable1 set state = 'A2' where product_id =?";
pstmt = conn.prepareStatement(sql);
System.out.println(f.getProduct_id());
pstmt.setInt(1,Integer.parseInt(f.getProduct_id()));
int result = pstmt.executeUpdate();
if(result > 0){
forward = actionMapping.findForward("success");
}
else{
forward = actionMapping.findForward("error");
}
}
catch(Exception e){
e.printStackTrace();
}
finally{
try{
if(rs!=null)rs.close();
if(pstmt!=null)pstmt.close();
}catch(SQLException ex){
System.out.println(ex);
}
}
System.out.println("forward:"+forward);
return forward;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -