获取总销售量
在多数情况下,销售列表中会有很多个记录,所以访问数据库的快捷性和高效性显得非常重要。现在我们只需要访问数据库中每一种产品的总额销售量。
表C中的getSales()方法与数据库连接并返回一个数组,这个数组包含每一种产品的总额出售量。
Listing C
| //////////////////////////////////////////////////////////// //Get the sales totals from the database //////////////////////////////////////////////////////////// public float[] getSales(int products) { float[] arr = new float[products]; Connection con; Statement stmt; ResultSet rs; int count = 0; String sql = "select productID, amount from p_sales"; try { //Load Driver: Class.forName(driver); //Connect to the database with the url con = DriverManager.getConnection(dburl , dbuid , dbpwd); stmt = con.createStatement(); //Get ResultSet rs = stmt.executeQuery(sql); while(rs.next()) { int product = rs.getInt("productID"); //Check that the productID is valid if (product >= 0 && product < products) { //Add to product total arr[product] += rs.getFloat("amount"); count++; } } stmt.close(); con.close(); } catch (java.lang.Exception ex) { arr[0] = -1.0f; } return arr; } |
| int product = rs.getInt("productID"); arr[product] += rs.getFloat("amount"); |
| Color pieColorArray[] = {new Color(210,60,60), new Color(60,210,60)…} |
| curPieColor++; if(curPieColor >= pieColorArray.length) {curPieColor = 0;} |

| RenderingHints renderHints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.setRenderingHints(renderHints); |