Chinaunix首页 | 论坛 | 博客
  • 博客访问: 402785
  • 博文数量: 114
  • 博客积分: 7010
  • 博客等级: 少将
  • 技术积分: 1395
  • 用 户 组: 普通用户
  • 注册时间: 2008-07-05 17:54
文章分类

全部博文(114)

文章存档

2011年(2)

2009年(1)

2008年(111)

我的朋友

分类: Java

2008-07-08 23:25:28

   刚开始做,首先要对整个业务有所了解:电子书店就是使用网络技术实现网络间的买书与卖书的过程,真是的情况是这样的 1:来到书店 2:查找自己需要的书籍 3:放入购物车 4:继续购物或者结账
   网络中的过程也是一样,只是实现的方式不一样而已:首先登陆电子书店显示所有的书籍名称,当顾客单击书籍名称或图片时以超链接的方式传递书籍的ID或是唯一标识列,显示该书的详细信息,以便用户确认,并显示“放入购物车”按钮,当用户购买结束后进行结账
 
一:数据库设计:数据库名为 books ,表名为 titles 字段信息为
    private String isbn; //isbn编号
    private String title;//书名
    private int editionNumber;//版本
    private String copyright;//版权
    private int publisherID;//出版商ID
    private String imageFile;//封面图片
    private double price;//价格
二:封装表中字段的Bean 名为BookBean 为每个属性创建 set 和 get方法 具体代码如下:
 

package ShopBean;
import java.io.Serializable;

public class BookBean implements Serializable{
    private String isbn; //isbn编号
    private String title;//书名
    private int editionNumber;//版本
    private String copyright;//版权
    private int publisherID;//出版商ID
    private String imageFile;//封面图片
    private double price;//价格
    public BookBean() {
    }

    public void setIsbn(String isbn) {
        this.isbn = isbn;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setEditionNumber(int editionNumber) {
        this.editionNumber = editionNumber;
    }

    public void setCopyright(String copyright) {
        this.copyright = copyright;
    }

    public void setPublisherID(int publisherID) {
        this.publisherID = publisherID;
    }

    public void setImageFile(String imageFile) {
        this.imageFile = imageFile;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getIsbn() {
        return isbn;
    }

    public String getTitle() {
        return title;
    }

    public int getEditionNumber() {
        return editionNumber;
    }

    public String getCopyright() {
        return copyright;
    }

    public int getPublisherID() {
        return publisherID;
    }

    public String getImageFile() {
        return imageFile;
    }

    public double getPrice() {
        return price;
    }
}

 

三:大家在上一步的代码中可以看见,我在定义BookBean的时候,还实现了Serializable接口,这是JAVA语言为我们提供的一种序列化机制,可以把Byte数据恢复 重新构建那个对象。现在说第三步,这步是创建数据库连接类 代码省略..不会的前面有

四:创建TitlesBean.java对象,里面有查询方法 返回Lise集合 具体代码如下:

package ShopBean;

import java.sql.*;
import java.util.*;
import java.io.*;
import java.util.Date;
import ShopDB.Dblei;
import java.util.ArrayList;

public class TitlesBean {
    public TitlesBean() {
    }

    Connection conn = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    Dblei db = new Dblei();
   
   public List getTitles() {
       String sql = "select * from titles order by title";
       List TitlesList = new ArrayList();
       this.conn = db.getConn();
       try {
           this.pstmt = conn.prepareStatement(sql);
           this.rs = pstmt.executeQuery();
           while (rs.next()) {
               BookBean ub = new BookBean();
               ub.setIsbn(rs.getString("isbn"));
               ub.setTitle(rs.getString("title"));
               ub.setEditionNumber(rs.getInt("editionNumber"));
               ub.setCopyright(rs.getString("copyright"));
               ub.setPublisherID(rs.getInt("publisherID"));
               ub.setImageFile(rs.getString("imageFile"));
               ub.setPrice(rs.getDouble("price"));
               TitlesList.add(ub);
           }

       } catch (SQLException ex) {
           ex.printStackTrace();
       } finally {
           db.closeResultSet(rs);
           db.closeStatement(pstmt);
           db.closeConn(conn);

       }
       return TitlesList;
   }


}

 

五:现在创建显示页面 books.jsp 关键代码如下:

<%@ page contentType="text/html; charset=GBK" import="ShopBean.*,java.util.*" %>
<html>
<head>
<title>
books
</title>
</head>
<body bgcolor="#ffffff">
<table style="TEXT-ALIGN:center" width="590" border="0">
  <tbody>
  
  <%
  TitlesBean titlesBean = new TitlesBean();
  //调用访问数据库方法,返回所有的书籍列表

  List titles = titlesBean.getTitles();
  BookBean currentBook;
  //把书籍列表保存在session中

  session.setAttribute("titles",titles);
  //对书籍列表进行遍历

  for(int i = 0;i<titles.size();i++)
  {
    currentBook = (BookBean)titles.get(i);
    if(i%3==0)
    {%>
    <tr>
      
      <% }%>
      <td>
        <table cellspacing="0" width="180">
          <tbody>
            <tr>
              <td valign="bottom" height="30">
                <a href="displayBook.jsp?isbn=<%=currentBook.getIsbn() %>">
                  <%=subStr(currentBook.getTitle()+","+currentBook.getEditionNumber()) %>
                </a>
              </td>
            </tr>
            <tr align="center">
              <td height="120">
                <a href="displayBook.jsp?isbn=<%=currentBook.getIsbn() %>">
                  <img height="110" alt="" src="images/<%=currentBook.getImageFile() %>" width="90" />
                </a>
              </td>
            </tr>
          </tbody>
        </table>
        <br />
      </td>
      <%if(i%3==2)
      {%>
      </tr>
      <%} } %>
  </tbody>
</table>
</body>
</html>
<%!
public String subStr(String str)
{
  if(str==null||"".equals(str))
  
    return "";
    if(str.length()>20)
    return str.substring(0,20)+"...";
    else
    return str;
  }
  %>

到目前为止,我们已经实现了在页面上显示数据库表titles中的所有书籍的信息

后边将实现当用户点击后的继续操作 displayBook.jsp 明天继续吧

 

阅读(493) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~