⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 productdao.java

📁 IBM提供的购物车
💻 JAVA
字号:
package com.webshop.db;
import java.sql.*;
import com.webshop.domain.Product;
import com.webshop.exception.ActionException;

/**
 * @author w
 *
 * To change this generated comment edit the template variable "typecomment":
 * Window>Preferences>Java>Templates.
 * To enable and disable the creation of type comments go to
 * Window>Preferences>Java>Code Generation.
 */
public class ProductDAO {

	Connection con; //数据库连接
	
	String selectProductByCategory="select * from product where category=?";
	String selectProductById="select * from product where productid=?";

	/**
	 * Constructor for ProductDAO.
	 */
	public ProductDAO() {
		super();
	}
	
	public java.util.Collection getProductByCategory()
	{
		java.util.Collection products=new java.util.ArrayList();
		try
		{
			con=DatabaseConnection.getConnection();
		}
		catch(Exception e)
		{
			throw new ActionException("不能获得连接,"+e.getMessage());
		}
		try
		{
			PreparedStatement pstmt=con.prepareStatement(selectProductByCategory);
			ResultSet rst=pstmt.executeQuery(); 
            Product product;
			while(rst.next())
			{
				product=new Product();
				product.setProductId(rst.getString("productid"));
				product.setCategory(rst.getString("category"));
				product.setName(rst.getString("name"));
				product.setDescription(rst.getString("descn"));
				products.add(product);
			}
				con.close();
		}
		catch(SQLException e)
		{
			e.printStackTrace();
			 throw new ActionException("执行数据库操作发生错误,"+e.getMessage());
		}
		finally
		{
			try
			{
				if(con!=null)
				con.close();
			}
			catch(Exception e2)
			{
			}
		}
		return products;
	}		
    
    /**
     * 根据产品的ID查找
     */
    public Product getProductById(String productId)
	{
		Product product=null;
		try
		{
			con=DatabaseConnection.getConnection();
		}
		catch(Exception e)
		{
			throw new ActionException("不能获得连接,"+e.getMessage());
		}
		try
		{
			PreparedStatement pstmt=con.prepareStatement(selectProductById);
			pstmt.setString(1,productId);
			ResultSet rst=pstmt.executeQuery();
          
			if(rst.next())
			{
				product=new Product();
				product.setProductId(rst.getString("productid"));
				product.setCategory(rst.getString("category"));
				product.setName(rst.getString("name"));
				product.setDescription(rst.getString("descn"));
				
			}
				con.close();
		}
		catch(SQLException e)
		{
			 throw new ActionException("执行数据库操作发生错误,"+e.getMessage());
		}
		finally
		{
			try
			{
				if(con!=null)
				con.close();
			}
			catch(Exception e2)
			{
			}
		}
		return product;
	}		

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -