• 大二上每日总结


    今日完成了讲解视频中JSP+JavaBean模式实现商品信息展示的案例。

    案例可以显示商品信息,点击图片可以查看商品详细信息,并可以查看最近五次浏览记录。

    效果截图:

     案例中使用了三个java类:

     Items 商品类:

    private int id;
        private String name;
        private String city;
        private int price;
        private int number;
        private String picture;
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getCity() {
            return city;
        }
        public void setCity(String city) {
            this.city = city;
        }
        public int getPrice() {
            return price;
        }
        public void setPrice(int price) {
            this.price = price;
        }
        public int getNumber() {
            return number;
        }
        public void setNumber(int number) {
            this.number = number;
        }
        public String getPicture() {
            return picture;
        }
        public void setPicture(String picture) {
            this.picture = picture;
        }

    DBHelper连接数据库类:

    private static String driver="com.mysql.jdbc.Driver";
        private static String url = "jdbc:mysql://localhost:3306/classname?useUnicode=true&characterEncoding=UTF-8"; //数据库名
        private static String username = "root";  //数据库用户名
        private static String password = "123456";  //数据库用户密码
        private static Connection conn=null;
        static
        {
            try
            {
                Class.forName(driver);
            }
            catch(Exception ex)
            {
                ex.printStackTrace();
            }
        }
        public static Connection getConnection() throws SQLException
        {
            if(conn==null)
            {
                conn=  DriverManager.getConnection(url,username,password);
            }
            return  conn;
        }
        public static void main(String[] args)
        {
            try
            {
                Connection conn=DBHelper.getConnection();
                if(conn!=null)
                {
                    System.out.println("数据库连接正常");
                }
                else
                {
                    System.out.println("数据库连接异常");
                }
            }
            catch(Exception ex)
            {
                ex.printStackTrace();
            }
        }

     ItemsDAO业务逻辑类:

    public ArrayList<Items> getAllItems()
        {
            Connection conn=null;
            PreparedStatement stmt=null;
            ResultSet rs=null;
            ArrayList<Items> list=new ArrayList<Items>();
            try
            {
                conn=DBHelper.getConnection();
                String sql="select * from items;";
                stmt=conn.prepareStatement(sql);
                rs=stmt.executeQuery();
                while(rs.next())
                {
                    Items item=new Items();
                    item.setId(rs.getInt("id"));
                    item.setName(rs.getString("name"));
                    item.setCity(rs.getString("city"));
                    item.setNumber(rs.getInt("number"));
                    item.setPrice(rs.getInt("price"));
                    item.setPicture(rs.getString("picture"));
                    list.add(item);
                }
                return list;
            }
            catch(Exception ex)
            {
                ex.printStackTrace();
                return null;
            }
            finally
            {
                if(rs!=null)
                {
                    try
                    {
                        rs.close();
                        rs=null;
                    }
                    catch(Exception ex)
                    {
                        ex.printStackTrace();
                    }
                }
                if(stmt!=null)
                {
                    try
                    {
                        stmt.close();
                        stmt=null;
                    }
                    catch(Exception ex)
                    {
                        ex.printStackTrace();
                    }
                }
            }
        }
        public Items getItemsById(int id)
        {
            Connection conn=null;
            PreparedStatement stmt=null;
            ResultSet rs=null;
            try
            {
                conn=DBHelper.getConnection();
                String sql="select * from items where id=?;";
                stmt=conn.prepareStatement(sql);
                stmt.setInt(1,id);
                rs=stmt.executeQuery();
                if(rs.next())
                {
                    Items item=new Items();
                    item.setId(rs.getInt("id"));
                    item.setName(rs.getString("name"));
                    item.setCity(rs.getString("city"));
                    item.setNumber(rs.getInt("number"));
                    item.setPrice(rs.getInt("price"));
                    item.setPicture(rs.getString("picture"));
                    return item;
                }
                else
                {
                    return null;
                }
            }
            catch(Exception ex)
            {
                ex.printStackTrace();
                return null;
            }
            finally
            {
                if(rs!=null)
                {
                    try
                    {
                        rs.close();
                        rs=null;
                    }
                    catch(Exception ex)
                    {
                        ex.printStackTrace();
                    }
                }
                if(stmt!=null)
                {
                    try
                    {
                        stmt.close();
                        stmt=null;
                    }
                    catch(Exception ex)
                    {
                        ex.printStackTrace();
                    }
                }
            }
        }
        public ArrayList<Items> getViewList(String list)
        {
            ArrayList<Items> itemlist=new ArrayList<Items>();
            int iCount=5;
            if(list!=null&&list.length()>0)
            {
                String[] arr=list.split("#");
                if(arr.length>=5)
                {
                    for(int i=arr.length-1;i>=arr.length-iCount;i--)
                    {
                        itemlist.add(getItemsById(Integer.parseInt(arr[i])));
                    }
                }
                else
                {
                    for(int i=arr.length-1;i>=0;i--)
                    {
                        itemlist.add(getItemsById(Integer.parseInt(arr[i])));
                    }
                }
                return itemlist;
            }
            else
            {
                return null;
            }
        }

    以及两个jsp文件:

    商品展示:

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    <%@ page import="dao.ItemsDAO" %>
    <%@ page import="entity.Items" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">       
        
        <title>My JSP 'index.jsp' starting page</title>
    	<meta http-equiv="pragma" content="no-cache">
    	<meta http-equiv="cache-control" content="no-cache">
    	<meta http-equiv="expires" content="0">    
    	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    	<meta http-equiv="description" content="This is my page">
    	<!--
    	<link rel="stylesheet" type="text/css" href="styles.css">
    	-->
    	<style type="text/css">
    	   div{
    	      float:left;
    	      margin: 10px;
    	   }
    	   div dd{
    	      margin:0px;
    	      font-size:10pt;
    	   }
    	   div dd.dd_name
    	   {
    	      color:blue;
    	   }
    	   div dd.dd_city
    	   {
    	      color:#000;
    	   }
    	</style>
      </head>
      
      <body>
        <h1>商品展示</h1>
        <hr>
         <center>
         <table width="750" height="60" cellpadding="0" cellspacing="0" border="0">
            <tr>
              <td>
                   <%
                       ItemsDAO itemsDao=new ItemsDAO();
                       ArrayList<Items> list=itemsDao.getAllItems(); 
                       if(list!=null&&list.size()>0)
                       {
                           for(int i=0;i<list.size();i++)
                           {
                              Items item=list.get(i); 
                   %>
                   <div>
                       <dl>
                         <dt>
                           <a href="details.jsp?id=<%=item.getId()%>"><img src="images/<%=item.getPicture() %>" width="120" height="90" border="1"/></a>
                         </dt>
                         <dd class="dd_name"><%=item.getName() %></dd>
                         <dd class="dd_city">产地:<%=item.getCity() %>  价格:¥<%=item.getPrice() %></dd>
                       </dl>
                   </div>
                   <%
                           }
                       }
                    
                   %>
                </td>
              </tr>  
           </table>
         </center>
      </body>
    </html>
    

     商品详细内容以及浏览记录:

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    <%@ page import="dao.ItemsDAO" %>
    <%@ page import="entity.Items" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'details.jsp' starting page</title>
        
    	<meta http-equiv="pragma" content="no-cache">
    	<meta http-equiv="cache-control" content="no-cache">
    	<meta http-equiv="expires" content="0">    
    	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    	<meta http-equiv="description" content="This is my page">
    	<!--
    	<link rel="stylesheet" type="text/css" href="styles.css">
    	-->
    	<style type="text/css">
    	   div{
    	      float:left;
    	      margin-left: 30px;
    	      margin-right:30px;
    	      margin-top: 5px;
    	      margin-bottom: 5px;
    	   }
    	   div dd{
    	      margin:0px;
    	      font-size:10pt;
    	   }
    	   div dd.dd_name
    	   {
    	      color:blue;
    	   }
    	   div dd.dd_city
    	   {
    	      color:#000;
    	   }
    	</style>
      </head>
      
      <body>
      <h1>商品详情</h1>
        <hr>
        <center>
        <table width="750" height="60" cellpadding="0" cellspacing="0" border="0"> <br>
           <tr>
             <!--  商品详情 -->
             <%
                ItemsDAO itemDao=new ItemsDAO();
                Items item=itemDao.getItemsById(Integer.parseInt(request.getParameter("id")));
                if(item!=null)
                {
             %>
             <td width="70%" valign="top">
                <table>
                   <tr>
                      <td rowspan="4"><img src="images/<%=item.getPicture() %>" width="200" height="160"></td>
                   </tr>
                   <tr>
                      <td><B><%=item.getName() %></B></td>
                   </tr>
                   <tr>
                      <td>产地:<%=item.getCity() %></td>
                   </tr>
                   <tr>
                      <td>价格: <%=item.getPrice() %>¥</td>
                   </tr>
                </table>
             </td>
             <%
                }
              %>
              <%
                String list="";
                Cookie[] cookies=request.getCookies();
                if(cookies!=null&&cookies.length>0)
                {
                
                    for(Cookie c:cookies)
                    {
                         if(c.getName().equals("listViewCookie"))
                         {
                             list=c.getValue();
                         }
                     }
                }
                     list+=request.getParameter("id")+"#";
                     String[] arr=list.split("#");
                     if(arr!=null&&arr.length>0)
                     {
                         if(arr.length>1000)
                         {
                             list="";
                         }
                     }
                     Cookie cookie=new Cookie("listViewCookie",list);
                     response.addCookie(cookie);
               %> 
               <td width="30%" bgcolor="#EEE" align="center">
                  <br>
                  <b>你浏览过的商品</b><br>
                  <%
                     ArrayList<Items> itemlist =itemDao.getViewList(list);
                     if(itemlist!=null&&itemlist.size()>0)
                     {
                         System.out.println("itemlist.size="+itemlist.size());
                         for(Items i:itemlist)
                         {
                            
                   %> 
                    <div>
                       <dl>
                         <dt>
                           <a href="details.jsp?id=<%=i.getId()%>"><img src="images/<%=i.getPicture() %>" width="120" height="90" border="1"/></a>
                         </dt>
                         <dd class="dd_name"><%=i.getName() %></dd>
                         <dd class="dd_city">产地:<%=i.getCity() %>  价格:¥<%=i.getPrice() %></dd>
                       </dl>
                   </div>   
                   <%
                           }
                       }
                   %>
              </td>
          </tr>   
        </table>
        </center> 
      </body>
    </html>
    

      

  • 相关阅读:
    vue 中的虚拟dom
    Vue基操
    表头固定,表的主体设置滚动条,同时解决错位问题
    AngularJS处理服务器端返回的JSON数据的格式问题
    jQuery ajax-param()
    Bootstrap中内联单选按钮
    angularJS中控制器和作用范围
    如何理解MVC?
    CSS3动画简介以及动画库animate.css的使用
    UGUI实现打字的效果
  • 原文地址:https://www.cnblogs.com/fengchuiguobanxia/p/13926576.html
Copyright © 2020-2023  润新知