封装数据库操作问题...

CNetol 2004-09-27 10:11:43
小弟有一段代码不明白其意思,还望达人指点,谢谢!!


连接数据库的Bean、产品Bean、产品相关操作Bean都能看明白,就是在添加和删除的时候这些在jsp中的代码没看明白.

增加产品addProduct.jsp

<h1>增加一个新的商品</h1>
<form action="addProduct_do.jsp" method="post" name="product">
<table border="1" bgcolor="#0099CC">
<tr><td>产品ID:<input type="text" name="productId"></td></tr>
<tr><td>产品名称:<input type="text" name="name"></td></tr>
<tr><td>产品价格:<input type="text" name="price"></td></tr>
<tr><td>=厂商=:<input type="text" name="producer"></td></tr>
<tr><td>=描述=:<input type="text" name="description"></td></tr>
<tr><td>=类别=:<select name="categoryId">
<%
Collection categorys=category.getAllCategory();
Iterator it=categorys.iterator();
while(it.hasNext())
{
Category temp=(Category)it.next();
out.println("<option value="+temp.getCategoryId()+">"+temp.getCategoryId()+"</option>");
}
%>





-------------------------
addProduct_do.jsp

<%
productBean.addProduct(product);
%>


这两个文件中的代码看不太明白,数据是怎么到商品Bean中的呢?
...全文
121 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
pcdll 2004-09-27
  • 打赏
  • 举报
回复
你看看bag.Product这个类,操作应该都在这里面!
<jsp:useBean id="product" class="bag.Product" scope="page">
<jsp:setProperty name="product" property="*"/>
CNetol 2004-09-27
  • 打赏
  • 举报
回复
up
CNetol 2004-09-27
  • 打赏
  • 举报
回复
这个是addProduct_do.jsp
//////////////////////////////
<%@ page contentType="text/html; charset=gb2312" language="java" import="bag.*,java.util.*,java.io.*" errorPage="error.jsp" %>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<jsp:useBean id="product" class="bag.Product" scope="page">
<jsp:setProperty name="product" property="*"/>
</jsp:useBean>
<jsp:useBean id="productBean" class="bag.ProductBean" scope="page"/>
<body>
<%
productBean.addProduct(product);%>
<center>
<h1>增加商品成功</h1>
<a href="index.html">返回</a>
</center>
</body>
</html>
pcdll 2004-09-27
  • 打赏
  • 举报
回复
你看看addProduct_do.jsp中的product这个变量是如何赋值的,它就是一个model,里面保存着上个页面过来的值,在addProduct_do.jsp这个页面应该有request.getParameter这样的方法给product变量赋值!
CNetol 2004-09-27
  • 打赏
  • 举报
回复
package bag;

import java.sql.*;
import java.util.*;
import java.io.*;
import bag.*;
/**
*ProductBean包含和Product表相关的操作
*/
public class ProductBean
{
private Connection con;
//构造方法,获得数据库的连接。
public ProductBean()
{
this.con=DataBaseConnection.getConnection();
}
/**
*搜索所有的商品信息。
*返回由Product值对象组成的Collection
*/
public Collection getAllProduct()throws Exception
{
Statement stmt=con.createStatement();
ResultSet rst=stmt.executeQuery("select * from Products");
Collection ret=new ArrayList();
while(rst.next())
{
Product temp=new Product();
temp.setProductId(rst.getString("productid"));
temp.setCategoryId(rst.getString("category"));
temp.setName(rst.getString("name"));
temp.setDescription(rst.getString("descn"));
temp.setProducer(rst.getString("producer"));
temp.setPrice(rst.getFloat("price"));

ret.add(temp);
}
con.close();
return ret;
}

/**
*按照商品的类别查找商品,
*返回由Product值对象组成的Collection
*/
public Collection getProductByCategory(String categoryId)throws Exception
{
Statement stmt=con.createStatement();
ResultSet rst=stmt.executeQuery("select * from Products where category='"+categoryId+"'");
Collection ret=new ArrayList();
while(rst.next())
{
Product temp=new Product();
temp.setProductId(rst.getString("productid"));
temp.setCategoryId(rst.getString("category"));
temp.setName(rst.getString("name"));
temp.setDescription(rst.getString("descn"));
temp.setProducer(rst.getString("producer"));
temp.setPrice(rst.getFloat("price"));

ret.add(temp);
}
con.close();
return ret;
}

/**
*添加一个商品,使用Product值对象作为参数传给这个方法。
*/
public void addProduct(Product product)throws Exception
{

PreparedStatement pstmt=con.prepareStatement("insert into products values(?,?,?,?,?,?)");
pstmt.setString(1,product.getProductId());
pstmt.setString(2,product.getCategoryId());
pstmt.setString(3,product.getName());
pstmt.setString(4,product.getProducer());
pstmt.setFloat(5,product.getPrice());
pstmt.setString(6,product.getDescription());
pstmt.execute();

}

/**
*更改商品的信息,使用Product值对象作为参数传给这个方法。
*/
public void modifyProduct(Product product)throws Exception
{

PreparedStatement pstmt=con.prepareStatement("update products set category=?,name=?, producer=?,price=?,descn=? where productid=?");
pstmt.setString(1,product.getCategoryId());
pstmt.setString(2,product.getName());
pstmt.setString(3,product.getProducer());
pstmt.setFloat(4,product.getPrice());
pstmt.setString(5,product.getDescription());
pstmt.setString(6,product.getProductId());
pstmt.execute();
}

/**
*删除没个商品,指定商品的ID
*/
public void deleteProduct(String productId)throws Exception
{
Statement stmt=con.createStatement();
stmt.execute("delete from products where productid='"+productId+"'");
}

/**
*返回给定ProductId的商品的信息,
*返回的是值对象
*/
public Product getProductInfo(String productId)throws Exception
{

Statement stmt=con.createStatement();
ResultSet rst=stmt.executeQuery("select * from products where productid='"+productId+"'");
Product product=null;
while(rst.next())
{
product=new Product();
product.setProductId(rst.getString("productid"));
product.setCategoryId(rst.getString("category"));
product.setName(rst.getString("name"));
product.setDescription(rst.getString("descn"));
product.setProducer(rst.getString("producer"));
product.setPrice(rst.getFloat("price"));
}
return product;
}
}


----------------------------------------------

上面是那个类,我这里还不明白,就是这么操作完,那个商品Bean里就存有form里的值了吗??
为什么要这么操作呢?不有request.getParameter方法吗?

Iterator it=categorys.iterator(); //对类别进行迭代
while(it.hasNext())
{
Category temp=(Category)it.next(); //获得每一个具体的类别
out.println("<option value="+temp.getCategoryId()+">"+temp.getCategoryId()+"</option>"); //组成下拉列表中每一项的值,值和名称都是类别的id
}

pcdll 2004-09-27
  • 打赏
  • 举报
回复
Collection categorys=category.getAllCategory(); //取得所有的类别
Iterator it=categorys.iterator(); //对类别进行迭代
while(it.hasNext())
{
Category temp=(Category)it.next(); //获得每一个具体的类别
out.println("<option value="+temp.getCategoryId()+">"+temp.getCategoryId()+"</option>"); //组成下拉列表中每一项的值,值和名称都是类别的id
}


productBean.addProduct(product); //应该是往数据库里插值,具体如何实现,请参考productBean中的addProduct方法
alaal 2004-09-27
  • 打赏
  • 举报
回复
你看一看addProduct_do.jsp中
productBean.addProduct()方法中是怎么写的

81,090

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧