import java.sql.*;
import java.util.LinkedList;
public class DB
{
private static Connection con;
public static void init() throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // load driver
con = DriverManager.getConnection("jdbc:odbc:PPP Shopping Cart");
}
public static void store(ProductData pd) throws Exception
{
PreparedStatement s = buildProductInsertionStatement(pd);
executeStatement(s);
}
private static PreparedStatement buildProductInsertionStatement(ProductData pd) throws SQLException
{
PreparedStatement s = con.prepareStatement("INSERT into Products VALUES (?, ?, ?)");
s.setString(1, pd.sku);
s.setString(2, pd.name);
s.setInt(3, pd.price);
return s;
}
public static ProductData getProductData(String sku) throws Exception
{
PreparedStatement s = buildProductQueryStatement(sku);
ResultSet rs = s.executeQuery();
ProductData pd = null;
if (rs.next())
{
pd = extractProductDataFromResultSet(rs);
rs.close();
}
s.close();
return pd;
}
private static PreparedStatement buildProductQueryStatement(String sku) throws SQLException
{
PreparedStatement s = con.prepareStatement("SELECT * FROM Products WHERE sku = ?;");
s.setString(1, sku);
return s;
}
private static ProductData extractProductDataFromResultSet(ResultSet rs) throws SQLException
{
ProductData pd = new ProductData();
pd.sku = rs.getString(1);
pd.name = rs.getString(2);
pd.price = rs.getInt(3);
return pd;
}
public static void store(ItemData id) throws Exception
{
PreparedStatement s = buildItemInsersionStatement(id);
executeStatement(s);
}
private static PreparedStatement buildItemInsersionStatement(ItemData id) throws SQLException
{
PreparedStatement s = con.prepareStatement("Insert into Items(orderId,quantity,sku) VALUES (?, ?, ?);");
s.setInt(1,id.orderId);
s.setInt(2,id.qty);
s.setString(3, id.sku);
return s;
}
public static ItemData[] getItemsForOrder(int orderId) throws Exception
{
PreparedStatement s = buildItemsForOrderQueryStatement(orderId);
ResultSet rs = s.executeQuery();
ItemData[] id = extractItemDataFromResultSet(rs);
rs.close();
s.close();
return id;
}
private static PreparedStatement buildItemsForOrderQueryStatement(int orderId) throws SQLException
{
PreparedStatement s = con.prepareStatement("SELECT * FROM Items WHERE orderid = ?;");
s.setInt(1, orderId);
return s;
}
private static ItemData[] extractItemDataFromResultSet(ResultSet rs) throws SQLException
{
LinkedList l = new LinkedList();
for (int row = 0; rs.next(); row++)
{
ItemData id = new ItemData();
id.orderId = rs.getInt("orderid");
id.qty = rs.getInt("quantity");
id.sku = rs.getString("sku");
l.add(id);
}
return (ItemData[]) l.toArray(new ItemData[l.size()]);
}
public static OrderData newOrder(String customerId) throws Exception
{
int newMaxOrderId = getMaxOrderId() + 1;
PreparedStatement s = con.prepareStatement("Insert into Orders(orderId,cusid) Values(?,?);");
s.setInt(1, newMaxOrderId);
s.setString(2,customerId);
executeStatement(s);
return new OrderData(newMaxOrderId, customerId);
}
private static int getMaxOrderId() throws SQLException
{
Statement qs = con.createStatement();
ResultSet rs = qs.executeQuery("Select max(orderId) from Orders;");
rs.next();
int maxOrderId = rs.getInt(1);
rs.close();
return maxOrderId;
}
public static OrderData getOrderData(int orderId) throws SQLException
{
PreparedStatement s = con.prepareStatement("Select cusid from orders where orderid = ?;");
s.setInt(1, orderId);
ResultSet rs = s.executeQuery();
OrderData od = null;
if (rs.next())
od = new OrderData(orderId, rs.getString("cusid"));
rs.close();
s.close();
return od;
}
private static void executeStatement(PreparedStatement s) throws SQLException
{
s.execute();
s.close();
}
public static void close() throws Exception
{
con.close();
}
public static void clear() throws Exception
{
Statement s = con.createStatement();
s.execute("delete * from orders;");
s.execute("delete * from items;");
s.execute("delete * from products;");
s.close();
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
《敏捷软件开发》源代码
共371个文件
java:153个
h:114个
cpp:96个
4星 · 超过85%的资源 需积分: 22 96 下载量 125 浏览量
2008-11-27
15:38:04
上传
评论 1
收藏 213KB ZIP 举报
温馨提示
《敏捷软件开发——原则、模式与实践》一书的源代码。
资源详情
资源评论
资源推荐
收起资源包目录
《敏捷软件开发》源代码 (371个子文件)
datetest.cc 2KB
PayrollTest.cpp 25KB
TestRunnerDlg.cpp 8KB
CoffeeMakerIDlg.cpp 7KB
CoffeeMaker.cpp 7KB
MulticasterTest.cpp 5KB
eval.cpp 5KB
featgrp.cpp 5KB
HostApp.cpp 4KB
coffeeMakerFSM.cpp 4KB
Date.cpp 3KB
ProgressBar.cpp 3KB
TextTestResult.cpp 3KB
TestRunner.cpp 3KB
TestRunner.cpp 3KB
TestCase.cpp 3KB
TestRunner.cpp 3KB
TestRunner.cpp 3KB
TestRunner.cpp 3KB
MainFrm.cpp 3KB
HostAppView.cpp 3KB
CoffeeMakerI.cpp 2KB
Multicaster.cpp 2KB
HostAppDoc.cpp 2KB
TestRunner.cpp 2KB
Employee.cpp 2KB
ProductPersistenceTestCase.cpp 1KB
UnionAffiliation.cpp 1KB
ExampleTestCase.cpp 1KB
CommissionedClassification.cpp 1KB
HourlyClassification.cpp 1KB
PayrollDatabase.cpp 1KB
TestSuite.cpp 951B
PaydayTransaction.cpp 929B
ActiveTest.cpp 906B
SalesReceiptTransaction.cpp 862B
QueryTestCase.cpp 811B
TimeCardTransaction.cpp 806B
AddEmployeeTransaction.cpp 786B
ChangeUnaffiliatedTransaction.cpp 729B
ServiceChargeTransaction.cpp 678B
AddCommissionedEmployee.cpp 659B
GUITestResult.cpp 644B
ChangeCommissionedTransaction.cpp 616B
Paycheck.cpp 602B
ChangeMemberTransaction.cpp 600B
Pollable.cpp 592B
MonthlySchedule.cpp 568B
MIVAPI.cpp 564B
persistentAssembly.cpp 551B
ChangeHourlyTransaction.cpp 542B
ChangeSalariedTransaction.cpp 540B
AddSalariedEmployee.cpp 539B
AddHourlyEmployee.cpp 535B
IHotWaterSourceClient.cpp 488B
BiweeklySchedule.cpp 472B
IHotWaterSource.cpp 446B
IVesselClient.cpp 432B
ChangeEmployeeTransaction.cpp 417B
persistentProduct.cpp 415B
ChangeDirectTransaction.cpp 406B
IUIClient.cpp 404B
IVessel.cpp 390B
ChangeClassificationTransaction.cpp 377B
agileCopy1.cpp 363B
IUI.cpp 362B
DirectMethod.cpp 353B
DeleteEmployeeTransaction.cpp 351B
ChangeMailTransaction.cpp 342B
TestResult.cpp 342B
ChangeAffiliationTransaction.cpp 341B
SalariedClassification.cpp 339B
WeeklySchedule.cpp 338B
ChangeAddressTransaction.cpp 327B
ChangeHoldTransaction.cpp 294B
ChangeNameTransaction.cpp 293B
ChangeMethodTransaction.cpp 277B
MailMethod.cpp 222B
TestFailure.cpp 218B
StdAfx.cpp 214B
StdAfx.cpp 210B
StdAfx.cpp 207B
persistentObject.cpp 192B
NoAffiliation.cpp 176B
SalesReceipt.cpp 174B
ServiceCharge.cpp 167B
assembly.cpp 157B
HoldMethod.cpp 156B
xref.cpp 140B
TimeCard.cpp 139B
PaymentClassification.cpp 112B
product.cpp 106B
Affiliation.cpp 90B
PaymentSchedule.cpp 70B
m4.cpp 66B
PaymentMethod.cpp 64B
Transaction.cpp 58B
TestCase.h 6KB
TestResult.h 5KB
coffeeMakerFSM.h 3KB
共 371 条
- 1
- 2
- 3
- 4
Lanjerl_Osarru
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论6