Java Reference
In-Depth Information
Listing 7.3
Splitting the initialization and cleanup
package splitcleaner;
import java.sql.*;
public class CostProcessor { o The master process controller
public static void main(java.lang.String[] args) {
String url = "jdbc:db2:shop";
try {
Class.forName ("COM.ibm.db2.jdbc.app.DB2Driver").newInstance();
Connection con = DriverManager.getConnection (url);
Adder adder = new Adder();
float total = adder.addCosts(con); o Calling phase 1
System.out.println("Total cost: " + total);
o Making the
connection
} catch (Exception e) {
throw new RuntimeException(e.toString());
}
}
}
o Implementation
public class Adder {
public static final int COST_COLUMN = 1;
public float addCosts(Connection con) {
float sum = 0;
float total = 0;
try {
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT cost from cart");
while (rs.next()) {
float cost = rs.getFloat(Adder.COST_COLUMN);
sum = sum + cost;
}
Taxer taxer = new Taxer();
total = taxer.addTaxes(con, sum);
} catch (Throwable theException) {
theException.printStackTrace();
}
return total;
}
}
for phase 1
o Implementation
public class Taxer {
for phase 2
public final static int COST_COLUMN = 1;
public float addTaxes(Connection con, float total) {
float sum = total;
try {
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT cost from cart");
while (rs.next()) {
Search WWH ::




Custom Search