本,并更新辅助表中的数据来反映这一点。然后我们能够使 用这些数据来计算出我们需要的信息。
所有这些工作具有类似于在我们的 servlet 加入嵌套循环连接所具有的实际效果。最 终我们有五条 SELECT 语句、一条 INSERT 语句、一条 UPDATE 语句和一条 DELETE 语句 。将这些和我们为这个查询的 DB2 Information Integrator 版本所发出的单个 SELECT 语句进行比较,您对这种方法所增加的成本会有所了解。当然,这还没有考虑我们为了正 确执行这些查询并得到正确结果所必须设计和实现的额外逻辑。
以下是我们从使用直接数据访问来实现查询 5 的 servlet 中抽取的 SQL 代码:
// statements for query #5
q3db2fetch = conn[1].prepareStatement("SELECT COUNT(*) AS num_order, " +
" SUM(o_totalprice) AS total_order, c_name, c_custkey, c_acctbal " +
" FROM tpcd.customer, tpcd.orders, tpcd.nation " +
" WHERE c_custkey = o_custkey " +
" AND c_nationkey = n_nationkey " +
" AND n_name = ? AND c_mktsegment = ? " +
" AND c_acctbal >= ? AND c_acctbal <= ? " +
" GROUP BY c_custkey, c_name, c_acctbal + +
" ORDER BY total_order, num_order");
q3orafetch = conn[2].prepareStatement("SELECT COUNT(*) AS num_order, " +
" SUM(o_totalprice) AS total_order, o_custkey " +
" FROM orders WHERE o_custkey = ? " +
" GROUP BY o_custkey");
q3xlsfetch = conn[3].prepareStatement("SELECT COUNT(*) AS num_order, " +
" SUM(o_totalprice) AS total_order, o_custkey " +
" FROM [orders$] WHERE o_custkey = ? " +
" GROUP BY o_custkey");
q3tempfetch = conn[0].prepareStatement("SELECT c_custkey FROM temp_jquery3");
q3insertfromdb2 = conn[0].prepareStatement("INSERT INTO temp_jquery3 " +
" (c_custkey, c_name, c_acctbal, total_order, num_order) " +
" VALUES (?, ?, ?, ?, ?)");
q3update = conn[0].prepareStatement("UPDATE temp_jquery3 " +
" SET num_order = num_order + ?, total_order = total_order + ? " +
" WHERE c_custkey = ?");
q3tempselect = conn[0].prepareStatement("SELECT AVG(total_order/num_order) " +
" AS avg_order, c_name, c_custkey, c_acctbal " +
" FROM temp_jquery3 " +
" GROUP BY c_custkey, c_name, c_acctbal " +
" ORDER BY avg_order DESC");
q3tempdelete = conn[0].prepareStatement("DELETE FROM temp_jquery3");
跨多个数据源的J2EE开发: 细节探讨(11)
时间:2011-04-11 IBM C. M. Saracco
执行查询
如果您还在,恭喜您。我们知道这需要了解很多东西。但是您几乎要通过最后一关了。 我们只想简要地谈谈用什么来执行我们在上一节展示的这些查询。
同样的,您可能差不多预料到了这种情况:使用 DB2 Information Integrator 的 servlet 只需要做很少的工作,而直接访问每个数据源的 servlet 要做多得多的工作。我 们不会在这里一个接着一个地向您展示查询,因为很多代码是重复的。相反,我们只是首 先从 DB2 Information Integrator 开始讨论一些有趣的案例。
使用 DB2 Information Int |