?? projectmanager.java
字號:
package zhaobiao.db;
import java.util.*;
import java.sql.*;
import zhaobiao.db.*;
import zhaobiao.data.*;
/**
* <p>Title: 工程項目信息管理類</p>
* <p>Description:有關工程項目的瀏覽操作</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/
public class ProjectManager {
public ProjectManager() {
}
/** 獲得所有的項目集合
*
* @param page
* @param pagemax
* @return Vector 所有的項目集合
*/
public Vector getProjectList(long page ,long pagemax)
{
page=page-1;
Vector list=new Vector();
db=DBConnectionManager.getInstance();
con=db.getConnection("idb");
String sql="select *from project";
try {
Statement ps1=con.createStatement();
rs=ps1.executeQuery(sql);
int count=1;
while(rs.next())
{
if (count>(page*pagemax)&&count<=(page*pagemax+pagemax)){
Project pj=new Project();
pj.setProject_id(rs.getLong("project_id"));
pj.setProject_name(rs.getString("project_name"));
pj.setProject_time(rs.getString("project_time"));
pj.setProject_company(rs.getString("project_company"));
pj.setProject_cost(rs.getString("project_cost"));
pj.setProject_content(rs.getString("project_content"));
list.add(pj);
}
count++;
}
if(ps1!=null)
ps1.close() ;
}
catch (SQLException ex) {
freeCon();
ex.printStackTrace();
}
freeCon();
return list;
}
/** 獲得所有的項目集合的最大數(shù)量
*
* @return long 所有的項目集合的最大數(shù)量
*/
public long getProjectListCount()
{
db=DBConnectionManager.getInstance();
con=db.getConnection("idb");
long count=0;
String sql=" select count(*)as count from project";
try {
Statement ps1=con.createStatement();
rs=ps1.executeQuery(sql);
while(rs.next())
{
count=Long.parseLong(rs.getString("count"));
}
if(ps1!=null)
ps1.close() ;
}
catch (SQLException ex) {
freeCon();
ex.printStackTrace();
}
freeCon();
return count;
}
/**根據(jù)項目id獲得該項目相關的所有類別列表
*
* @param project_id(項目id)
* @return Vector該項目相關的所有類別列表
*/
public Vector getProjectTypeList(long project_id)
{
Vector list=new Vector();
db=DBConnectionManager.getInstance();
con=db.getConnection("idb");
String sql="select *from project_product where project_id=?";
try {
ps=con.prepareStatement(sql);
ps.setLong(1,project_id);
rs=ps.executeQuery();
while(rs.next())
{
Type type=new Type();
long typeid;
typeid=rs.getLong("type_id");
Basic basic=new Basic();
type=basic.getType(typeid); //調(diào)用getType方法
list.add(type);
}
}
catch (SQLException ex) {
ex.printStackTrace();
freeCon();
}
freeCon();
return list;
}
/** 根據(jù)項目id和類別id獲得滿足兩個條件的產(chǎn)品集合(Set類型)
*
* @param page
* @param pagemax
* @param project_id(項目id)
* @param type_id(類別id)
* @return Set滿足兩個條件的產(chǎn)品集合
*/
public Set getPTProductList(long page ,long pagemax,long project_id,long type_id)
{
//Vector list=new Vector();
page=page-1;
Set set1=new TreeSet(); //分頁判斷,所得到的集合區(qū)間
Set set2=new TreeSet(); //真正的返回值
db=DBConnectionManager.getInstance();
con=db.getConnection("idb");
String sql="select *from ppm where project_id=?";
try {
ps=con.prepareStatement(sql);
ps.setLong(1,project_id);
rs=ps.executeQuery();
int count=1;
boolean flag=true;
Basic basic=new Basic();
while(rs.next())
{
Product pd=new Product();
pd=basic.getProduct(rs.getLong("product_id"));
if(pd.getType_id()==type_id)
{
if(set1.add(pd))
{
if (count>(page*pagemax)&&count<=(page*pagemax+pagemax))
{
set2.add(pd);;
}
count++; //計數(shù)器加1
}
}
}
basic.freeCon();
}
catch (SQLException ex) {
freeCon();
ex.printStackTrace();
}
freeCon();
return set2;
}
/**根據(jù)項目id和類別id獲得滿足兩個條件的產(chǎn)品數(shù)量
*
* @param project_id(項目id)
* @param type_id(類別id)
* @return long 滿足兩個條件的產(chǎn)品數(shù)量
*/
public long getPTProductListCount(long project_id,long type_id)
{
long count=0;
Set set1=new TreeSet();
db=DBConnectionManager.getInstance();
con=db.getConnection("idb");
String sql="select *from ppm where project_id=?";
try {
ps=con.prepareStatement(sql);
ps.setLong(1,project_id);
rs=ps.executeQuery();
Basic basic=new Basic();
while(rs.next())
{
Product pd=new Product();
pd=basic.getProduct(rs.getLong("product_id"));
if(pd.getType_id()==type_id)
{
if(set1.add(pd))
count++; //計數(shù)器加1
}
}
basic.freeCon();
}
catch (SQLException ex) {
freeCon();
ex.printStackTrace();
}
freeCon();
return count;
}
/** 根據(jù)項目id和產(chǎn)品id得到相關的PPM的id的集合
*
* @param page
* @param pagemax
* @param project_id(項目id)
* @param product_id(產(chǎn)品id)
* @return VectorPPM的id的集合
*/
public Vector getPPMList(long page ,long pagemax,long project_id,long product_id)
{
page=page-1;
Vector list=new Vector();
db=DBConnectionManager.getInstance();
con=db.getConnection("idb");
String sql="select *from ppm where project_id=? and product_id=?";
try {
ps=con.prepareStatement(sql);
ps.setLong(1,project_id);
ps.setLong(2,product_id);
rs=ps.executeQuery();
int count=1;
while(rs.next())
{
if (count>(page*pagemax)&&count<=(page*pagemax+pagemax)){
Long ppmid=new Long(rs.getLong("id"));
list.add(ppmid);
}
count++;
}
}
catch (SQLException ex) {
freeCon();
ex.printStackTrace();
}
freeCon();
return list;
}
/** 根據(jù)項目id和產(chǎn)品id得到相關的PPM集合的數(shù)量
*
* @param project_id(項目id)
* @param product_id(產(chǎn)品id)
* @return long 相關的PPM集合的數(shù)量
*/
public long getPPMListCount(long project_id,long product_id)
{
long count=0;
db=DBConnectionManager.getInstance();
con=db.getConnection("idb");
String sql="select *from ppm where project_id=? and product_id=?";
try {
ps=con.prepareStatement(sql);
ps.setLong(1,project_id);
ps.setLong(2,product_id);
rs=ps.executeQuery();
while(rs.next())
{
count++;
}
}
catch (SQLException ex) {
freeCon();
ex.printStackTrace();
}
freeCon();
return count;
}
/*
/** 根據(jù)標價id獲的標價對象
*
*
public Price getPrice(long Price_id)
{
Price price=new Price();
db=DBConnectionManager.getInstance();
con=db.getConnection("idb");
String sql="select *from price where price_id=?";
try {
ps=con.prepareStatement(sql);
rs=ps.executeQuery();
if(rs.next())
{
price.setPrice_id(rs.getLong("price_id"));
}
}
catch (SQLException ex) {
freeCon();
ex.printStackTrace();
}
freeCon();
return price;
}
*/
/**
* 釋放數(shù)據(jù)庫資源<p>
*PreparedStatement和ResultSep將關閉,Connection返回給連接池
* @param 無
* @repurn 無
* @exception SQLException
*
*/
public void freeCon(){
try {
if (rs!=null)
rs.close() ;
if (ps!=null)
ps.close() ;
}
catch (SQLException ex) {
}
if (db!=null)
db.freeConnection("idb",con) ;
}
public static void main(String[] args) {
ProjectManager projectManager1 = new ProjectManager();
/* Vector list=projectManager1.getProjectTypeList(1);
for(int i=0;i<list.size();i++)
{
Type type=new Type();
type=(Type)list.elementAt(i);
System.out.println(type.getType_name());
}
*/
Set set1=new TreeSet();
// pjid=3&typeid=3&page1=2
set1= projectManager1.getPTProductList(2,1,3,3);
System.out.print("set1.size()="+set1.size());
//set1.add("ddddddddd");
//set1.add("fsdads");
}
private DBConnectionManager db;
private Connection con=null;
private ResultSet rs=null;
private PreparedStatement ps=null;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -