标题:为什么在浏览器中显示不出数据库中Product这个表中的详细信息,只显示框架? ...
只看楼主
deersfly
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2012-10-16
 问题点数:0 回复次数:1 
为什么在浏览器中显示不出数据库中Product这个表中的详细信息,只显示框架??Product数据已经导入到数据库中
怎样解决????就是在浏览器中不显示Product表的详细信息???


一.Product.java

package entity;   

public class Product {
    private int productId;//商品ID、主键
    private String name;//商品名称
    private int catId;//商品所属类别,外键
    private double price;//商品单价
    private String desc;//商品描述信息
    private String attr;//商品属性
    public Product(){}
    public Product(int  productId,String name,int catId,double price,String desc,String attr)
    {
        this.productId = productId;
        this.name = name;
        this.catId = catId;
        this.price = price;
        this.desc = desc;
        this.attr = attr;
    }
    public int getProductId() {
        return productId;
    }
    public void setProductId(int productId) {
        this.productId = productId;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getCatId() {
        return catId;
    }
    public void setCatId(int catId) {
        this.catId = catId;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
    public String getDesc() {
        return desc;
    }
    public void setDesc(String desc) {
        this.desc = desc;
    }
    public String getAttr() {
        return attr;
    }
    public void setAttr(String attr) {
        this.attr = attr;
    }
}
二.操作类:ProductDao.java
package dao;
import java.util.*;
import java.sql.*;

public class ProductDao {
    public List<entity.Product> searchAll(){
        List<entity.Product> list = new ArrayList<entity.Product>();
        try {
            Connection con=ConnectionManager.getConnection();
            Statement stmt = con.createStatement();
            String sql = "select * from product";
            ResultSet res = stmt.executeQuery(sql);
            while(res.next()){
                entity.Product product = new entity.Product();
                product.setProductId(res.getInt("productId"));
                product.setName(res.getString("name"));
                product.setCatId(res.getInt("catId"));
                product.setPrice(res.getDouble("price"));
                product.setDesc(res.getString("desc"));
                product.setAttr(res.getString("attr"));
                list.add(product);
            }
            res.close();
            stmt.close();
            con.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return list;
    }
    /*
     * 通过CatId查询某一类别的商品
     */
    public List<entity.Product> searchByCat(int catId){
        List<entity.Product> list = new ArrayList<entity.Product>();
        try {
            Connection con=ConnectionManager.getConnection();
        
            String sql = "select * from product where CatId=?";
            PreparedStatement pstmt = con.prepareStatement(sql);
            pstmt.setInt(1, catId);
            ResultSet res =pstmt.executeQuery();
            while(res.next()){
                entity.Product product = new entity.Product();
                product.setProductId(res.getInt("productId"));
                product.setName(res.getString("name"));
                product.setCatId(res.getInt("catId"));
                product.setPrice(res.getDouble("price"));
                product.setDesc(res.getString("desc"));
                product.setAttr(res.getString("attr"));
                list.add(product);
            }
            res.close();
            pstmt.close();
            con.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return list;
        
    }
    public List<entity.Product> searchBy(int productId){
        List<entity.Product> list = new ArrayList<entity.Product>();
        try {
            Connection con=ConnectionManager.getConnection();
        
            String sql = "select * from product where ProductId=?";
            PreparedStatement pstmt = con.prepareStatement(sql);
            pstmt.setInt(1, productId);
            ResultSet res =pstmt.executeQuery();
            while(res.next()){
                entity.Product product = new entity.Product();
                product.setProductId(res.getInt("productId"));
                product.setName(res.getString("name"));
                product.setCatId(res.getInt("catId"));
                product.setPrice(res.getDouble("price"));
                product.setDesc(res.getString("desc"));
                product.setAttr(res.getString("attr"));
                list.add(product);
            }
            res.close();
            pstmt.close();
            con.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return list;
        
    }
    /*
     * 向数据库对应的表中增加新的产品
     */
    public int Add(entity.Product product){
        int result=0;
        try {
            Connection con = ConnectionManager.getConnection();
            String sql = "insert into product values(null,?,?,?,?,?)";
            PreparedStatement pstmt = con.prepareStatement(sql);
            pstmt.setString(1, product.getName());
            pstmt.setInt(2, product.getCatId());
            pstmt.setDouble(3, product.getPrice());
            pstmt.setString(4, product.getDesc());
            pstmt.setString(5, product.getAttr());
            
            result=pstmt.executeUpdate();
            pstmt.close();
            con.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return result;
    }
    /*
     * 根据produtId删除商品
     */
    public int Del(int productId){
        int result=0;
        try {
            Connection con = ConnectionManager.getConnection();
            String sql="delete from product where productId=?";
            PreparedStatement pstmt=con.prepareStatement(sql);
            pstmt.setInt(1, productId);
            result=pstmt.executeUpdate();
            pstmt.close();
            con.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return result;
    }
   
    /*
     * 更新指定productId的商品的信息
     */
    public int Update(entity.Product product){
        int result=0;
        try {
            Connection con = ConnectionManager.getConnection();
            String sql="update product set name=?,catId =?,price=?,attr=? where productId=?";
            PreparedStatement pstmt=con.prepareStatement(sql);
            pstmt.setString(1, product.getName());
            pstmt.setInt(2, product.getCatId());
            pstmt.setDouble(3, product.getPrice());
            pstmt.setString(4, product.getAttr());
            pstmt.setInt(5, product.getProductId());
            result=pstmt.executeUpdate();
            pstmt.close();
            con.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return result;
    }
    public static void main(String[]args) {
        // TODO Auto-generated method stub
        List<entity.Product> products = null;
        ProductDao test = new ProductDao();
        products = test.searchByCat(1);
        for(entity.Product product : products){
            System.out.println(product.getName());
        }
        
        

    }
}
三.JSP显示:displayProduct.jsp
<%@ page language="java" contentType="text/html; charset=gb2312"
    pageEncoding="gb2312"%>
<%@page import="java.util.List"%>
<%@page import="entity.Product"%>
   <jsp:useBean id="ProductDao" class="dao.ProductDao" />

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Insert title here</title>
</head>
<%if(session.getAttribute("USER")==null)
    response.sendRedirect("login.jsp");
 %>
 <%
     List<Product> list=ProductDao.searchAll();
 %>
<body>
<form name="form1" method="post" action="">
  <p align="center">商品信息一览表</p>
  <table width="664" border="1" align="center">
    <tr>
      <th width="183" height="25" scope="col">名称</th>
      <th width="77" scope="col">价格</th>
      <th width="200" scope="col">描述信息</th>
      <th width="76" scope="col">属性</th>
    </tr>
    <%for(int i=0;i<list.size();i++){
        Product product=(Product)list.get(i);
    %>
    <tr>
      <td height="26"><%=product.getName() %></td>
      <td><%=product.getPrice() %></td>
      <td><%=product.getDesc() %></td>
      <td><%=product.getAttr() %></td>
    </tr>
    <%} %>
  </table>
  <center><a href="SearchByType.jsp">查询</a></center>
  <p>&nbsp;</p>
</form>
</body>
</html>
搜索更多相关主题的帖子: public private 详细信息 浏览器 商品 
2012-10-16 15:17
leesanchuan
Rank: 2
等 级:论坛游民
威 望:1
帖 子:11
专家分:27
注 册:2012-10-24
得分:0 
先不说你的程序,你先看一下你的jdbc 对不,连数据库驱动都没加载,你怎么连接,晕死,Class.forName("com.mysql.jdbc.Driver");
   conn = DriverManager.getConnection("url", "user", "password");
2012-10-24 09:26



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-383321-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.222867 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved