📄 productdao.java
字号:
/*
* ProductDAO.java
*
* Created on 2007年4月19日, 上午9:48
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package com.ebuy.dataaccess;
import com.ebuy.common.dao.IProductDAO;
import com.ebuy.entities.ProductEntity;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
*
* @author Administrator
*/
public class ProductDAO extends DAOAdapter implements IProductDAO
{
public List findByCategoryID(int categoryid, int pagesize, int pageindex)
{
List list=new ArrayList();
String sp="GONGBIN.PAGINGBYCATEGORYID(?,?,?,?)";
try
{
java.sql.CallableStatement csmt=this.server.getCallableStatement(sp);
csmt.setInt(1,pagesize);
csmt.setInt(2,pageindex);
csmt.setInt(3,categoryid);
csmt.registerOutParameter(4,oracle.jdbc.OracleTypes.CURSOR);
csmt.execute();
java.sql.ResultSet rs=(java.sql.ResultSet)csmt.getObject(4);
this.loadResultSetToList(list,rs);
}
catch (Exception ex)
{
ExceptionManager.handlerException(ex);
}
finally
{
this.server.closeConnection();
}
return list;
}
//将查询结果以ProductEntity对象集合的形式保存
private void loadResultSetToList(List list,ResultSet rs)throws SQLException
{
ProductEntity p=null;
while(rs.next())
{
p=new ProductEntity();
p.setProductid(rs.getInt(1));
p.setProductname(rs.getString(2));
p.setCategoryid(rs.getInt(3));
p.setUnitprice(rs.getDouble(4));
p.setImagename(rs.getString(5));
p.setQuantity(rs.getInt(6));
p.setDownbound(rs.getInt(7));
list.add(p);
}
}
public List findByProductNameKey(String key, int pagesize, int pageindex)
{
List list=new ArrayList();
String sp="GONGBIN.PAGINGBYPRODUCTNAME(?,?,?,?)";
try
{
java.sql.CallableStatement csmt=this.server.getCallableStatement(sp);
csmt.setInt(1,pagesize);
csmt.setInt(2,pageindex);
csmt.setString(3,key);
csmt.registerOutParameter(4,oracle.jdbc.OracleTypes.CURSOR);
csmt.execute();
java.sql.ResultSet rs=(java.sql.ResultSet)csmt.getObject(4);
this.loadResultSetToList(list,rs);
}
catch (SQLException ex)
{
ExceptionManager.handlerException(ex);
}
finally
{
this.server.closeConnection();
}
return list;
}
public int getRecordCountByCategoryID(int categoryid)
{
String sql="select count(*) from Products where CategoryID = "+categoryid;
return this.getCountBySql(sql);
}
public int getRecordCountByProductNameKey(String key)
{
String sql="select count(*) from Products where lower(ProductName) like '%"+key.toLowerCase()+"%'";
return this.getCountBySql(sql);
}
private int getCountBySql(String sql)
{
try
{
java.sql.ResultSet rs=this.server.execQuery(sql);
rs.next();
int count=rs.getInt(1);
return count;
}
catch (SQLException ex)
{
ExceptionManager.handlerException(ex);
return 0;
}
finally
{
this.server.closeConnection();
}
}
public boolean addEntity(Object entity)
{
ProductEntity p=(ProductEntity)entity;
String sql="insert into Products values (SEQ_ProductID.nextval,?,?,?,?,?,?,?)";
try
{
java.sql.PreparedStatement pstmt=this.server.getPreparedStatement(sql);
pstmt.setString(1,p.getProductname());
pstmt.setInt(2,p.getCategoryid());
pstmt.setDouble(3,p.getUnitprice());
pstmt.setString(4,p.getImagename());
pstmt.setInt(5,p.getQuantity());
pstmt.setInt(6,400);
pstmt.setInt(7,p.getDownbound());
pstmt.execute();
return true;
}
catch (SQLException ex)
{
ExceptionManager.handlerException(ex);
return false;
}
finally
{
this.server.closeConnection();
}
}
public static void main(String[] args)
{
ProductDAO dao=new ProductDAO();
ProductEntity p=new ProductEntity();
p.setCategoryid(3);
p.setDownbound(100);
p.setImagename("a.jpg");
p.setQuantity(150);
p.setUnitprice(88.99);
p.setProductname("测试");
boolean b=dao.addEntity(p);
System.out.println(b);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -