Easily implement data pie charts on JSP pages (1)

Source: Internet
Author: User

What you need

To run the relevant examples correctly, you must have JDK 1.2 or later, a relational database management system, and a JSP network server. I debug these examples in Tomcat, and I also use com.sun.image.codec.jpeg classes of Sun Java 2 sdkmodules.

Database Design

Suppose you work in a company that sells fresh fruits. The fruits sold by the company include Apple, orange, and grape. Now, your boss wants to use a pie chart to display the total sales volume of each fruit. The pie chart can display the sales status of each product at a glance, and the boss can quickly grasp the company's product transactions.

Table A uses the list of two databases in this article. The first type of list (Products) contains the names of all Sales Products; the second type of list (Sales) contains the Sales volume of each product.

Listing A Database Design 
---------------
p_products table
----------------
productID int (number)  not null
productname  String (varchar)  not null p_sales table
-------------
saleID   int (number) not null
productID int (number) not null
amount   floatnot null
The product list contains two domains: productID and productname. The Sales list includes saleID, productID, and total amount. ProductID in the sales list provides associations between the two lists. The total amount in the sales list contains the amount of cash for each sale, which appears in floating point data.

The getProducts () method in Table B connects two databases and stores all product names in the array:

Listing B

////////////////////////////////////////////////////////////
//Get products from the database as a String array
////////////////////////////////////////////////////////////
public String[] getProducts()
{
 String[] arr = new String[0];
 Connection con;
 Statement stmt;
 ResultSet rs;
 int count = 0;
 String sql = "select * from p_products order by productID";
 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);
//Count the records
while(rs.next())
 {count++;}
//Create an array of the correct size
arr = new String[count];
//Get ResultSet (the portable way of using rs a second time)
rs = stmt.executeQuery(sql);
while(rs.next())
{
 arr[rs.getInt("productID")] = rs.getString("productname");
}
stmt.close();
con.close();
 }
 catch (java.lang.Exception ex)
 {
arr[0] = ex.toString();
 }
 return arr;
}

Configure the following Database Rules:

1. ProductID is the most unique and critical product list;

2. The value of ProductID for the first record is 0;

3. All subsequent continuous records are accumulated, so the productID of the second record is 1, The productID of the third record is 2, and so on.

These Database Rules allow data to be stored in the product array as follows:

arr[rs.getInt("productID")] = rs.getString("productname");


Some database management systems allow automatic data accumulation or sorting by default. When designing a database, you must first identify the rules that your database management system follows, such as automatic accumulation and automatic sorting.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.