package Beans;
import java.sql.*;
/**
* 该Bean利用 DBOperation Bean连接数据库
* 读取数据库中,图书的基本信息
*/
public class BookBean
{
private String id = null; //图书id编号
private String name = null; //图书名称
private String author = null;// 图书的作者
private String publisher = null;//图书的出版社
private float price =0f;// 图书价格
private String introduce = null;//图书的介绍
/**
* 构造一个新的<b>BookBean</b>
*/
public BookBean(){
}
/**
* 根据图书的id编号,给图书的其他信息赋值
*
* @param id String类型的图书ID编号
*/
public void setBookinfo(String book_id){
id = null;
name = null;
//author = null;
publisher = null;
price = 0f;
introduce = null;
DBOperation database = new DBOperation();
try{
ResultSet rst = database.executeSQL(" select * from book where book_id='"+book_id+"' ");
if( rst.next() ){
id = rst.getString("book_id");
name = rst.getString("book_name");
//author = rst.getString("author");
publisher = rst.getString("company");
price = rst.getFloat("book_price");
introduce = rst.getString("content");
}
}catch(SQLException e){
e.printStackTrace();
}finally{
try{
database.closeDatabase();
}catch(SQLException e){
e.printStackTrace();
}
}
}
/**
* 获取图书的编号
*
* @return String
*/
public String getBookid(){
return id;
}
/**
* 获取图书的名称
*
* @return String
*/
public String getBookname(){
return name;
}
/**
*方法说明: 获取图书的作者
*
* @return String
*/
public String getBookauthor(){
return author;
}
/**
* 获取图书的出版社信息
*
* @return String
*/
public String getBookpublisher(){
return publisher;
}
/**
* 获取图书的价格
*
* @return String
*/
public float getBookprice(){
return price;
}
/**
* 获取图书的介绍信息
*
* @return String
*/
public String getBookintroduce(){
return introduce;
}
/**
* 将Bean作为一个application进行测试用
*/
public static void main(String[] args){
BookBean book = new BookBean();
book.setBookinfo("book_id");
System.out.println (book.getBookid());
System.out.println (book.getBookname());
System.out.println (book.getBookauthor());
System.out.println (book.getBookpublisher());
System.out.println (book.getBookprice());
System.out.println (book.getBookintroduce());
}
}
------------------------------------------------------------
<%--
程序:shoppingCart.jsp
说明:该页面是一个购物车
--%>
<%@ page contentType="text/html; charset=gb2312" language="java" session="true" %>
<jsp:useBean id="bookInfo" class="Beans.BookBean" scope="page"/>
<%
/* 禁止使用浏览器cache */
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires",0);
%>
<html>
<head>
<title>随便购书网 user_name=<%=session.getValue("user_name") %></title>
<script language="javascript">
<!--
//-->
</script>
</head>
<hr></hr>
<body>
<table width=80% border="0" align="center" cellpadding="0" cellspacing="1"
bgcolor="#666666">
<tr>
<td height="30" bgcolor="#F6F6F6">
<table width="100%" border="0" cellspacing="0"
cellpadding="0">
<tr>
<td align="center"><font class="fontSize">随便网上书店购物车</td>
</tr>
</table>
</td>
</tr>
</table>
<br>
<form action="" onsubmit="">
<table width=80% border="1" align="center" cellpadding="0" cellspacing="0">
<tr height=25>
<td align="center"><font class="fontSize">编号</td>
<td align="center"><font class="fontSize">书名</td>
<td align="center"><font class="fontSize">单价</td>
<td align="center"><font class="fontSize">数量</td>
<td align="center"><font class="fontSize"> </td>
</tr>
<%
Cookie[] cookies= request.getCookies();//获得cookie信息数组
for(int i=0;i< cookies.length; i++){
String book_id = cookies[i].getName();
String num = cookies[i].getValue();//获得值
if( book_id.startsWith("book_id") ){ //判断cookie信息是否以book_id为开头
bookInfo.setBookinfo(book_id.substring(7));//从第7位开始读取字符串信息
%>
<tr>
<td align="center"><%= book_id.substring(7) %></td>
<td align="center"><%= bookInfo.getBookname() %></td>
<td align="center"><%= bookInfo.getBookprice() %></td>
<td align="center"><input size="5" type="text" maxlength="5"
value="<%=num%>" name="num"></td>
<td align="center"><a href="deleteBook.jsp?book_id=<%=bookInfo.getBookid()%>">删除</a></td>
</tr>
<%
}
}
%>
</table>
</form>
<br>
<TABLE width=80% border="0" align="center" cellpadding="0" cellspacing="0">
<TR>
<TD align="center"><A href="index1.htm">返回首页</A></TD>
<TD align="center"><a href="deleteBook.jsp?book_id=empty">清空购物车
</a></TD>
<TD align="center"><a href="order.jsp" >填写/提交订单</a></TD>
<TD align="center"> </TD>
</TR>
</TABLE>
</body>
</html>
------------------------------------------------------------
运行jsp时,<= bookInfo.getBookname() %>和<%= bookInfo.getBookprice() %>
显示为null,值不能从bean中传过来.