码迷,mamicode.com
首页 > 其他好文 > 详细

购物系统①

时间:2015-05-27 12:02:53      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:

因为有上学期的自己和同学的demo。所以进度比较快。

到目前为止。使用smartUpload做好了上传商品功能,同时支持删除商品功能

期间遇到两个问题。用smartUpload上传商品的时候,

 

如果是使用绝对路径的话,在自己所建立的upload文件夹中是不会出现你上传的照片的。如下图图一图二所示

技术分享

                                             图一

 

                               技术分享

                                            图二

 

所以我改用同学的使用相对路径进行保存

 

                                技术分享

                                                   图三

 

                       技术分享

                                                   图四

                                                   

 

                               技术分享

                                                  图五

 

问题二

在guanli.jsp中,我把xuhao 写成了xuehao。导致在在shopservlet中获取不了这个值,所以删除不了,最后顺利解决了这个问题!

                     技术分享

                 技术分享

备注:

1.附上到目前为止的源代码和截图

2.所有代码都是我全部重新敲过,建议大伙可以自己多敲敲代码

3.在shopservlet.java中把接下来需要的功能代码全都复制进去了。也就是说shopservlet.jsp后期不能再增加内容了

                                      技术分享

DBbean.java

  1 package bean;
  2 
  3 
  4 import java.sql.*;
  5 //import javax.sql.*;
  6 
  7 //import javax.naming.Context;
  8 //import javax.naming.InitialContext;
  9 
 10 public class DBbean 
 11 {
 12     static Connection con;
 13     static String url;
 14     static String driverName="com.mysql.jdbc.Driver";
 15     static String userName="root";
 16     static String userPassword="123456";
 17     static String dbName="dbtemp";
 18     
 19     public DBbean() throws ClassNotFoundException,SQLException
 20     {
 21         url="jdbc:mysql://localhost/"+dbName+"?autoReconnect=true&useUnicode=true&characterEncoding=GBK&user="+userName+"&password="+userPassword;
 22     }
 23     public static Connection getDBConnection()
 24     {
 25         try{
 26             Class.forName("com.mysql.jdbc.Driver");//加载驱动
 27             con=DriverManager.getConnection(url);
 28         }catch(ClassNotFoundException e){
 29                     }catch(SQLException e){}
 30         return con;
 31     }
 32     /*public static Connection getPoolConnection()
 33     {
 34         try{
 35             Context ctx=new InitialContext();//向上转型,Context是java.name包中的一个接口,用于查找数据库连接池的配置文件
 36             ctx=(Context)ctx.lookup("java:com/env");
 37             DataSource ds=(DataSource)ctx.lookup("DBPool");
 38             con=ds.getConnection();
 39             
 40         }catch(Exception e)
 41         {
 42             e.printStackTrace();
 43         }
 44         return con;
 45     }*/
 46     //运行更新。插入。删除操作
 47     public static void executesql(String query)throws SQLException
 48     {
 49         Statement stmt=null;
 50         try{
 51             stmt=DBbean.getDBConnection().createStatement();
 52             stmt.execute(query);
 53             stmt.close();
 54         }catch(SQLException e){}
 55         
 56     }
 57     //查询一个整数字段值
 58     public int getIntResult(String query)throws SQLException
 59     {
 60         Statement stmt=null;
 61         int i=0;
 62         try{
 63             stmt=DBbean.getDBConnection().createStatement();
 64             //stmt=DBbean.getPoolConncetion().creatStatement();
 65             ResultSet rs=stmt.executeQuery(query);
 66             while(rs.next())
 67             {
 68                 i=Integer.parseInt(rs.getString(1));
 69                 
 70             }
 71             stmt.close();
 72             con.close();
 73         }catch(SQLException e){}
 74         return i;
 75     }
 76     //查询一个字符串字段值
 77     public String getOneStringResult(String query,int i)throws SQLException
 78     {
 79         Statement stmt=null;
 80         ResultSet rs=null;
 81         String str="Null";
 82         try{
 83             stmt=DBbean.getDBConnection().createStatement();
 84             //stmt=DBbean.getPoolConnection().creatStatement();
 85             rs=stmt.executeQuery(query);
 86             while(rs.next())
 87             {
 88                 str=rs.getString(i);                
 89             }
 90             stmt.close();
 91             con.close();
 92         }catch(SQLException e){}
 93         return str;
 94     }
 95     //查询返回一个结果集
 96     public static ResultSet getResultSet(String query)throws SQLException
 97     {
 98         ResultSet rs=null;
 99         Statement stmt=null;
100         try{
101             stmt=DBbean.getDBConnection().createStatement();
102             rs=stmt.executeQuery(query);
103             
104         }catch(SQLException e){
105             e.printStackTrace();
106         }
107         return rs;
108     }
109     //查询一个字段的结果集,并以字符串数组形式返回
110     public String[] getResultSetToStr(String query)throws SQLException
111     {
112         ResultSet rs1=null,rs2=null;
113         int i=0,counter=0;
114         String[] name=null;
115         Statement stmt1=null,stmt2=null;
116         try{
117             stmt1=DBbean.getDBConnection().createStatement();
118             rs2=stmt1.executeQuery(query);
119             while(rs2.next())
120             {
121                 counter++;
122             }
123             stmt2=DBbean.getDBConnection().createStatement();
124             rs1=stmt2.executeQuery(query);
125             name=new String[counter];
126             while(rs1.next())
127             {
128                 name[i]=rs1.getString(1);
129                 i++;
130             }
131             
132         }catch(SQLException e)
133         {
134             e.printStackTrace();
135         }
136         rs1.close();
137         rs2.close();
138         stmt1.close();
139         stmt2.close();
140         con.close();
141         return name;
142         
143     }
144     public static boolean execute(String query)
145     {
146         boolean result=false;
147         Statement stmt=null;
148         try{
149             stmt=DBbean.getDBConnection().createStatement();
150             result=stmt.execute(query);
151             
152         }catch(SQLException e){}
153         return result;
154     }
155     public void closeon() throws SQLException
156     {
157         con.close();
158     }
159 
160 }

 

shopservlet.java

  1 package servlet;
  2 
  3 import java.io.IOException;
  4 import java.sql.ResultSet;
  5 import java.sql.SQLException;
  6 
  7 import javax.servlet.ServletException;
  8 import javax.servlet.annotation.WebServlet;
  9 import javax.servlet.http.HttpServlet;
 10 import javax.servlet.http.HttpServletRequest;
 11 import javax.servlet.http.HttpServletResponse;
 12 
 13 import bean.DBbean;
 14 
 15 /**
 16  * Servlet implementation class shopservlet
 17  */
 18 @WebServlet("/shopservlet")
 19 public class shopservlet extends HttpServlet 
 20 {
 21     private static final long serialVersionUID = 1L;
 22        
 23     /**
 24      * @see HttpServlet#HttpServlet()
 25      */
 26     public shopservlet()
 27     {
 28         super();
 29         // TODO Auto-generated constructor stub
 30     }
 31 
 32     /**
 33      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 34      */
 35     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
 36     {
 37         // TODO Auto-generated method stub
 38         doPost(request,response);
 39     }
 40 
 41     /**
 42      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 43      */
 44     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
 45     {
 46         // TODO Auto-generated method stub
 47         request.setCharacterEncoding("gbk");
 48         String action=request.getParameter("action");
 49         if(action.equals("delete"))
 50         {
 51             String a=request.getParameter("xuhao");
 52             String sql="delete from shop where xuhao=‘"+a+"‘";
 53             try{
 54                 DBbean.executesql(sql);
 55             }catch(SQLException e){
 56                 //TODO Auto-generated catch block
 57                 e.printStackTrace();
 58             }
 59             response.sendRedirect("guanli.jsp");
 60         }
 61         if(action.equals("delete2"))
 62         {
 63             String a=request.getParameter("xuhao");
 64             String sql="delete from cart where xuhao=‘"+"‘";
 65             try{
 66                 DBbean.executesql(sql);
 67                 
 68             }catch(SQLException e){
 69                 //TODO Auto-generated catch block
 70                 e.printStackTrace();
 71             }
 72             response.sendRedirect("cart.jsp");
 73         }
 74         if(action.equals("jian"))
 75         {
 76             String a=request.getParameter("xuhao");
 77             String sql="update cart set shuliang=shuliang-1 where xuhao=‘"+a+"‘";
 78             try{
 79                 DBbean.executesql(sql);
 80             }catch(SQLException e){
 81                 //TODO Auto-generated catch block
 82                 e.printStackTrace();
 83             }
 84             response.sendRedirect("car.jsp");
 85         }
 86         if(action.equals("jia"))
 87         {
 88             String a=request.getParameter("xuhao");
 89             String sql="update cart set shuliang =shuliang+1 where xuhao=‘"+a+"‘";
 90             try {
 91                 DBbean.executesql(sql);
 92             } catch (SQLException e) {
 93                 // TODO Auto-generated catch block
 94                 e.printStackTrace();
 95             }
 96             response.sendRedirect("cart.jsp");
 97         }
 98         if(action.equals("update"))
 99         {
100             String a=request.getParameter("xuhao");
101             String b=request.getParameter("mingchen");
102             //b=new String(b.getBytes("ISO-8859-1"),"gbk");
103             String c=request.getParameter("miaoshu");
104             //c=new String(c.getBytes("ISO-8859-1"),"gbk");
105             String d=request.getParameter("jiege");
106             String e=request.getParameter("cunhuo");
107             String f=request.getParameter("zhuangtai");
108             //f=new String(f.getBytes("ISO-8859-1"),"gbk");
109             String sql="update shop set ";
110             sql=sql+"mingchen=‘"+b+"‘, miaoshu=‘"+c+"‘, jiege=‘"+d+"‘, cunhuo=‘"+e+"‘, zhuangtai=‘"+f+"‘";
111             sql=sql+" where xuhao=‘"+a+"‘";
112             try {
113                 DBbean.executesql(sql);
114             } catch (SQLException e1) {
115                 // TODO Auto-generated catch block
116                 e1.printStackTrace();
117             }
118             response.sendRedirect("guanli.jsp");
119         }
120         if(action.equals("qingkong"))
121         {
122             String sql="delete from cart";
123             try {
124                 DBbean.executesql(sql);
125             } catch (SQLException e) {
126                 // TODO Auto-generated catch block
127                 e.printStackTrace();
128             }
129             response.sendRedirect("home.jsp");
130         }
131         if(action.equals("upordown"))
132         {
133             String a=request.getParameter("xuhao");
134             String b=new String(request.getParameter("zhuangtai").getBytes("iso-8859-1"),"gbk");
135             String sql2=null;
136             try{
137                 if(b.equals("下架"))
138                 {
139                     sql2="update shop set zhuangtai=‘上架‘ where  xuhao=‘"+a+"‘";
140                     DBbean.executesql(sql2);
141                 }
142                 if(b.equals("上架"))
143                 {
144                     sql2="update shop set zhuangtai=‘上架‘ where xuhao=‘"+a+"‘";
145                     DBbean.execute(sql2);
146                 }
147             }catch(SQLException e){
148                 //TODO Auto-generated catch block
149                 e.printStackTrace();
150             }
151             response.sendRedirect("guanli.jsp");
152         }
153     }
154 
155 }

uploadservlet.java

  1 package servlet;
  2 
  3 import java.io.IOException;
  4 import java.sql.SQLException;
  5 
  6 import javax.servlet.ServletConfig;
  7 import javax.servlet.ServletException;
  8 import javax.servlet.annotation.WebServlet;
  9 import javax.servlet.http.HttpServlet;
 10 import javax.servlet.http.HttpServletRequest;
 11 import javax.servlet.http.HttpServletResponse;
 12 
 13 import bean.DBbean;
 14 
 15 import com.jspsmart.upload.File;
 16 import com.jspsmart.upload.SmartUpload;
 17 import com.jspsmart.upload.SmartUploadException;
 18 
 19 /**
 20  * Servlet implementation class uploadservlet
 21  */
 22 @WebServlet("/uploadservlet")
 23 public class uploadservlet extends HttpServlet 
 24 {
 25     private static final long serialVersionUID = 1L;
 26     private static final int SAVEAS_PHYSICAL=0;
 27     ServletConfig servletconfig;
 28        
 29     /**
 30      * @see HttpServlet#HttpServlet()
 31      */
 32     public uploadservlet() 
 33     {
 34         super();
 35         // TODO Auto-generated constructor stub
 36     }
 37     public void init(ServletConfig config)throws ServletException
 38     {
 39         this.servletconfig=config;
 40     }
 41 
 42     /**
 43      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 44      */
 45     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
 46     {
 47         // TODO Auto-generated method stub
 48         doPost(request,response);
 49     }
 50 
 51     /**
 52      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 53      */
 54     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
 55     {
 56         // TODO Auto-generated method stub
 57         request.setCharacterEncoding("gbk");
 58         SmartUpload su=new SmartUpload();
 59         su.initialize(servletconfig, request, response);
 60         //limit the size of the upload‘s things
 61         su.setMaxFileSize(1000000);
 62         //限制总上传数据的长度
 63         su.setTotalMaxFileSize(2000000);
 64         //设定允许上传的文件(通过扩展名限制),禁止上传带有exe,bat,jsp,htm,html扩展名的文件和没有扩展名的文件。
 65         try{
 66             su.setDeniedFilesList("exe,bat,jsp,htm,html,");
 67         }catch(SQLException e){
 68             //TODO Auto-generated catch block
 69             e.printStackTrace();
 70         }
 71         try{
 72             su.upload();
 73         }catch(SmartUploadException e){
 74             //TODO Auto-generated catch block
 75             e.printStackTrace();
 76         }
 77         String a=su.getRequest().getParameter("xuhao");
 78         String b=su.getRequest().getParameter("mingchen");
 79         String c=su.getRequest().getParameter("miaoshu");
 80         String d=su.getRequest().getParameter("jiege");
 81         String e=su.getRequest().getParameter("cunhuo");
 82         String f=su.getRequest().getParameter("zhuangtai");
 83         File g=su.getFiles().getFile(0);//得到上传的第一个文件
 84         String h=b+"."+g.getFileExt();
 85         
 86         
 87         
 88         h="/upload/"+h;
 89         String realpath="/dingcan/WebContent";//这个要根据具体情况填写
 90         realpath=realpath+h;
 91         h="D:/Java"+realpath;//??
 92         System.out.println(h);
 93         
 94         
 95         /*String path=request.getSession().getServletContext().getRealPath("/upload");
 96         System.out.println(path);
 97          String h1="upload/"+h;*/
 98         
 99         
100         
101         String sql="insert into shop values(";
102         //sql=sql+"‘"+a+"‘"+","+"‘"+b+"‘"+","+"‘"+c+"‘"+"‘"+"‘"+h+"‘"+","+"‘"+d+"‘"+","+"‘"+e+"‘"+","+"‘"+f+"‘"+")";
103         sql=sql+"‘"+a+"‘"+","+"‘"+b+"‘"+","+"‘"+c+"‘"+","+"‘"+h+"‘"+","+"‘"+d+"‘"+","+"‘"+e+"‘"+","+"‘"+f+"‘"+")";
104         try{
105             DBbean.executesql(sql);
106         }catch(SQLException e1){
107             //TODO Auto-generated catch block
108             e1.printStackTrace();
109         }
110         try{
111             g.saveAs(h, SAVEAS_PHYSICAL);//?
112             //g.saveAs(path+"/"+h);
113             
114         }catch(SmartUploadException e1){
115             //TODO Auto-generated catch block
116             e1.printStackTrace();
117         }
118         response.sendRedirect("guanli.jsp");
119     }
120 
121 }

guanli.jsp

 1 <%@ page language="java" contentType="text/html; charset=gbk"
 2     pageEncoding="gbk"%>
 3 <%@ page import="com.mysql.jdbc.Driver" %>
 4 <%@ page import="java.sql.*" %>
 5 <%@ page import="bean.DBbean" %>
 6 <jsp:useBean id="DBbean" scope="page" class="bean.DBbean"/>
 7 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 8 <html>
 9 <head>
10 <meta http-equiv="Content-Type" content="text/html; charset=gbk">
11 <title>管理商品</title>
12 </head>
13 <body>
14 <h1 align="center">商品列表</h1>
15 <%
16 request.setCharacterEncoding("gbk");
17 String sql="select * from shop";
18 ResultSet rs=DBbean.getResultSet(sql);
19 %>
20 <table border="1" align="center">
21 <tr>
22     <th>商品序号</th>
23     <th>商品名称</th>
24     <th>商品价格</th>
25     <th>商品存货</th>
26     <th>上下架管理</th>
27 </tr>
28 <%while(rs.next()){%>
29 <tr>
30     <td><%=rs.getString("xuhao") %></td>
31     <td><%=rs.getString("mingchen") %></td>
32     <td><%=rs.getString("jiege") %></td>
33     <td><%=rs.getString("cunhuo") %></td>
34     <td><a href=‘shopservlet?action=delete&xuhao=<%=rs.getString("xuhao") %>‘ onclick="return confirm(‘确认将此记录删除?‘)">删除</a></td>
35     <td><a href=‘update.jsp?action=update&xuhao=<%=rs.getString("xuhao")%>‘>修改</a></td>
36     <%}
37        rs.close();%>
38 </tr>
39 </table>
40 <form action="uploadservlet" method="post" enctype="multipart/form-data" name="upload">
41 <table align=center>
42     <tr>
43         <td align=center><h1>上传商品</h1></td>
44     </tr>
45     <tr>
46         <td >
47         序号<input type="text" name="xuhao" size="10">名称<input type="text" name="mingchen" size="20">
48         价格<input type="text" name="jiege" size="10">存货<input type="text" name="cunhuo" size="10"><br>
49         描述<textarea  name="miaoshu" rows="5" cols="65"></textarea><br>
50         商品状态选择:<input type="radio" name="zhuangtai" value="上架" checked="checked">存入仓库<input type="radio" name="zhuangtai" value="下架">发布商品&nbsp
51         上传图片<input type="file" name="tupian" size="20">
52         </td>
53     </tr>
54     <tr>
55         <td  align="center"><input value="开始上传" type="submit"></td>
56     </tr>
57 </table>
58 </form>
59 </body>
60 </html>

运行界面

技术分享

 

购物系统①

标签:

原文地址:http://www.cnblogs.com/zzzzw/p/4532887.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!