📄 itemdao.java
字号:
package com.webshop.db;
import java.sql.*;
import com.webshop.domain.Item;
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 ItemDAO {
Connection con;//数据库连接
String selectItemById="select * from item where itemId=?";
String selectItemByProductId="select * from item where productId=?";
/**
* Constructor for ItemDAO.
*/
public ItemDAO() {
super();
}
public Item getItemById(String id)
{
ProductDAO productDao=new ProductDAO();
Item item=null;
try
{
con=DatabaseConnection.getConnection();
}
catch(Exception e)
{
throw new ActionException("不能获得连接,"+e.getMessage());
}
try
{
PreparedStatement pstmt=con.prepareStatement(selectItemById);
pstmt.setString(1,id);
ResultSet rst=pstmt.executeQuery();
if(rst.next())
{
item=new Item();
item.setItemId(rst.getString("itemid"));
item.setProductId(rst.getString("productid"));
item.setUnitCost(rst.getFloat("unitcost"));
item.setListPrice(rst.getFloat("listprice"));
item.setSupplierId(rst.getInt("supplier"));
item.setStatus(rst.getString("status"));
item.setAttr1(rst.getString("attr1"));
item.setAttr2(rst.getString("attr2"));
item.setAttr3(rst.getString("attr3"));
item.setAttr4(rst.getString("attr4"));
item.setAttr5(rst.getString("attr5"));
item.setProduct(productDao.getProductById(item.getProductId()));
}
con.close();
}
catch(SQLException e)
{
throw new ActionException("执行数据库操作发生错误,"+e.getMessage());
}
finally
{
try
{
if(con!=null)
con.close();
}
catch(Exception e2)
{
}
}
return item;
}
/**
* 根据productid来查找item
*/
public java.util.Collection getItemByProductId(String productId)
{
java.util.Collection items=new java.util.ArrayList();
ProductDAO productDao=new ProductDAO();
Item item=null;
try
{
con=DatabaseConnection.getConnection();
}
catch(Exception e)
{
throw new ActionException("不能获得连接,"+e.getMessage());
}
try
{
PreparedStatement pstmt=con.prepareStatement(selectItemByProductId);
pstmt.setString(1,productId);
ResultSet rst=pstmt.executeQuery();
while(rst.next())
{
item=new Item();
item.setItemId(rst.getString("itemid"));
item.setProductId(rst.getString("productid"));
item.setUnitCost(rst.getFloat("unitcost"));
item.setListPrice(rst.getFloat("listprice"));
item.setSupplierId(rst.getInt("supplier"));
item.setStatus(rst.getString("status"));
item.setAttr1(rst.getString("attr1"));
item.setAttr2(rst.getString("attr2"));
item.setAttr3(rst.getString("attr3"));
item.setAttr4(rst.getString("attr4"));
item.setAttr5(rst.getString("attr5"));
item.setProduct(productDao.getProductById(item.getProductId()));
items.add(item);
}
con.close();
}
catch(SQLException e)
{
throw new ActionException("执行数据库操作发生错误,"+e.getMessage());
}
finally
{
try
{
if(con!=null)
con.close();
}
catch(Exception e2)
{
}
}
return items;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -