/*
* This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/).
*/
package ch09.database;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.*;
import javax.servlet.http.HttpSession;
import ch09.*;
import ch09.object.unit.*;
/**
* 针对商品信息的数据处理类
* @author ShenYK
* @version 1.0
*/
public class DItem extends DCommon
{
//根据商品ID获取商品对象
public Item getItemById ( String itemId )
throws Exception
{
//获得数据库连接
Connection conn = this.getDBConnection();
if ( conn == null )
{
throw new Exception("数据库连接获得失败!");
}
Statement stmt = null;
ResultSet rs = null;
try
{
//从数据库中抽取数据
stmt = conn.createStatement();
//执行SQL语句
String sQuery = "select * from item where item_id='" + itemId + "' ";
rs = stmt.executeQuery( sQuery );
if ( rs.next() )
{
Item oItem = new Item(rs);
return oItem;
}
else
{
return null;
}
}
catch(Exception e)
{
e.printStackTrace();
throw e;
}
finally
{
try
{
rs.close();
stmt.close();
conn.close();
}catch(Exception ex)
{
}
}
}
//根据商品分类ID获取商品分类对象
public Category getCategoryById ( String categoryId )
throws Exception
{
//获得数据库连接
Connection conn = this.getDBConnection();
if ( conn == null )
{
throw new Exception("数据库连接获得失败!");
}
Statement stmt = null;
ResultSet rs = null;
try
{
//从数据库中抽取数据
stmt = conn.createStatement();
//执行SQL语句
String sQuery = "select * from category where category_id='" + categoryId + "' ";
rs = stmt.executeQuery( sQuery );
if ( rs.next() )
{
Category oCategory = new Category(rs);
return oCategory;
}
else
{
return null;
}
}
catch(Exception e)
{
e.printStackTrace();
throw e;
}
finally
{
try
{
rs.close();
stmt.close();
conn.close();
}catch(Exception ex)
{
}
}
}
//登录新商品
public String registerNewItem ( Item oItem )
throws Exception
{
//获得数据库连接
Connection conn = this.getDBConnection();
if ( conn == null )
{
throw new Exception("数据库连接获得失败!");
}
Statement stmt = null;
ResultSet rs = null;
try
{
//从数据库中抽取数据
stmt = conn.createStatement();
//执行SQL语句生成最大的itemId
String sItemId = "";
String sQuery = "select max(item_id) from item";
rs = stmt.executeQuery( sQuery );
rs.next();
String sCurrentMaxId = rs.getString(1);
//当前是第一次登录
if ( sCurrentMaxId == null )
{
sItemId = "000001";
}
else
{
int iMaxCd = Integer.parseInt(sCurrentMaxId);
sItemId = String.valueOf(iMaxCd+1);
int iLength = sItemId.length();
for(int i=6; i>iLength; i--)
{
sItemId = "0" + sItemId;
}
}
//执行SQL语句,添加商品信息
String sInsertQuery = "insert into item set "
+ "item_id = '" + sItemId + "', "
+ "item_name = '" + oItem.getItemName() + "', "
+ "short_name = '" + oItem.getShortName() + "', "
+ "category = '" + oItem.getCategory() + "', "
+ "standard = '" + oItem.getStandard() + "', "
+ "place = '" + oItem.getPlace() + "', "
+ "unit = '" + oItem.getUnit() + "', "
+ "product_id = '" + oItem.getProductId() + "'";
stmt.executeUpdate( sInsertQuery );
return sItemId;
}
catch(Exception e)
{
e.printStackTrace();
throw e;
}
finally
{
try
{
rs.close();
stmt.close();
conn.close();
}catch(Exception ex)
{
}
}
}
//修改商品
public void modifyItem ( Item oItem )
throws Exception
{
//获得数据库连接
Connection conn = this.getDBConnection();
if ( conn == null )
{
throw new Exception("数据库连接获得失败!");
}
Statement stmt = null;
ResultSet rs = null;
try
{
//从数据库中抽取数据
stmt = conn.createStatement();
//执行SQL语句,添加商品信息
String sUdpateQuery = "update item set "
+ "item_name = '" + oItem.getItemName() + "', "
+ "short_name = '" + oItem.getShortName() + "', "
+ "category = '" + oItem.getCategory() + "', "
+ "standard = '" + oItem.getStandard() + "', "
+ "place = '" + oItem.getPlace() + "', "
+ "unit = '" + oItem.getUnit() + "', "
+ "product_id = '" + oItem.getProductId() + "' "
+ "where item_id = '" + oItem.getItemId() + "'";
stmt.executeUpdate( sUdpateQuery );
}
catch(Exception e)
{
e.printStackTrace();
throw e;
}
finally
{
try
{
rs.close();
stmt.close();
conn.close();
}catch(Exception ex)
{
}
}
}
//删除商品
public void deleteItem ( String sItemId )
throws Exception
{
//获得数据库连接
Connection conn = this.getDBConnection();
if ( conn == null )
{
throw new Exception("数据库连接获得失败!");
}
Statement stmt = null;
ResultSet rs = null;
try
{
//从数据库中抽取数据
stmt = conn.createStatement();
//执行SQL语句,删除商品信息
String sUdpateQuery = "delete from item where item_id = '" + sItemId + "'";
stmt.executeUpdate( sUdpateQuery );
}
catch(Exception e)
{
e.printStackTrace();
throw e;
}
finally
{
try
{
rs.close();
stmt.close();
conn.close();
}catch(Exception ex)
{
}
}
}
//登录新商品分类
public String registerNewCategory ( C
评论12