?? addthings_do.jsp
字號:
<%@ page contentType="text/html;charset=GBK" pageEncoding="gb2312" import ="java.util.*"%><%@ page import ="java.sql.*,cn.wy.Pet.Things,cn.wy.DBConnection,org.lxh.smart.Request,cn.wy.Pet.User" %><jsp:useBean id="up" scope="page" class="org.lxh.smart.SmartUpload"/><% HttpSession s = request.getSession(); User user = (User)s.getAttribute("user"); DBConnection dbc = null ; String sql =""; String msgStr = ""; String forUrl = "adminiindex.jsp"; String imgPath = ""; boolean isView = false ; boolean isViewPrice =false ; //是修改還是添加 int isUpdata = 0; try { up.initialize(pageContext) ; up.upload() ; Request req = up.getRequest(); boolean flageShoping = true; //是修改還是添加 isUpdata = Integer.parseInt(req.getParameter("isUpdata")); //取出單選按扭的值 String vstr[] = req.getParameterValues("isView"); //如果第一個選重 if(vstr[0].equals("1")) isView = true ; String vpstr[] = req.getParameterValues("isViewPrice"); if (vpstr[0].equals("1")) isViewPrice = true; String ext = up.getFiles().getFile(0).getFileExt(); //指定圖片路徑 String PhotoPath = ""; //確定是否選擇了圖片//System.out.println("isMissing:" + up.getFiles().getFile(0).isMissing()); //如果有文件 if (!up.getFiles().getFile(0).isMissing()) { //是修改或者是以前沒有圖片的情況 if (isUpdata == 0 | req.getParameter("imgPath").length()<13) PhotoPath = "/upPetImages/"+ "Things" + String.valueOf(System.currentTimeMillis()) + "." + ext ; else PhotoPath = "/upPetImages/"+ req.getParameter("imgPath"); PhotoPath.replaceAll("\'",""); //判斷文件大小最大1M,類型必須是圖片類型 if (up.getFiles().getFile(0).getSize()<=1048576 & up.getFiles().getFile(0).getTypeMIME().equals("image")) { //驗證成功,開始上傳文件 up.getFiles().getFile(0).saveAs(PhotoPath); System.out.println("文件上傳成功"); imgPath = "sttpath='" + PhotoPath + "'," ; }else { //文件不符合規定返回到上一個頁面 out.print("<script>alert('文件不合法,文件大小必須在1M以內,必須是圖片(如 jpg,gif,png)')</script>"); out.print("<script>history.go(-1)</script>"); return ; } } PreparedStatement ps = null ; dbc = new DBConnection(); //開始提交表單信息 //如果是更新寵物的信息 if (req.getParameter("editThingId") != null & isUpdata == 1) { sql = "update Things set stname=?," + imgPath + " stspprice=?,stsaleprice=?,sttdate=getdate(),uname=?,sttremark=?,isview=?,isviewprice=? where stthingsid=?"; ps = dbc.getCon().prepareStatement(sql); ps.setString(1, req.getParameter("thName")); ps.setFloat(2, Float.parseFloat(req.getParameter("spprice"))); ps.setFloat(3, Float.parseFloat(req.getParameter("spsellprice"))); ps.setString(4, "admin"); ps.setString(5, req.getParameter("thremark") + ""); ps.setBoolean(6, isView); ps.setBoolean(7, isViewPrice); ps.setInt(8,Integer.parseInt(req.getParameter("editThingId"))); ps.executeUpdate(); ps.close(); msgStr = "修改"; //取出該寵物所在的頁 forUrl = "petList.jsp?pageNo=" + req.getParameter("pageNo"); System.out.println("更新成功! "); } //否則就是添加寵物 else{ sql = "insert into Things(stname,sttpath,stspprice,stsaleprice,uname,sttremark,isview,isviewprice)values(?,?,?,?,?,?,?,?)"; ps = dbc.getCon().prepareStatement(sql); ps.setString(1, req.getParameter("thName")); ps.setString(2, PhotoPath); ps.setFloat(3, Float.parseFloat(req.getParameter("spprice"))); ps.setFloat(4, Float.parseFloat(req.getParameter("spsellprice"))); ps.setString(5, user.getUName());//測試用 ps.setString(6, req.getParameter("thremark") + ""); ps.setBoolean(7, isView); ps.setBoolean(8, isViewPrice); ps.executeUpdate(); ps.close(); msgStr = "添加" ; System.out.println("東西添加成功!"); forUrl = "addThings.jsp"; } }catch(Exception e) { //添加失敗,瀏覽器后退 out.print("<script>alert('表單信息錯誤,增加寵物用品失敗,請檢查您填寫的信息無誤。');history.go(-1)</script>"); e.printStackTrace();} finally { if (dbc != null) dbc.dbClose(); } out.print("<script>alert('寵物用品" + msgStr + "成功!');history.go(-1)</script>"); %>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -