Easily implement a pie chart in a JSP page

Source: Internet
Author: User
Tags stmt

JSP provides a number of simple and useful tools, including reading data from a database, sending data, and being able to display the results in a pie shape. Now let's take a look at this simple and practical approach.

All the things you need

In order to properly run the relevant examples of this article, you must need JDK 1.2 or higher version, a relational database management system, a JSP network server. I've been debugging these examples at Tomcat, and I've also used the Sun Java 2 SDK release com.sun.image.codec.jpegclasses.

Database design

Let's say you work for a company that sells fresh fruit, and the fruits that the company sells include apples, oranges, and grapes. Now your boss wants to use a pie shape to show the total sale of each fruit, the pie shape can make each product sales situation at a glance, the boss can quickly grasp the company's product transactions.

Table A uses two of the database lists in this article. The first list (products) contains the names of all the items sold, and the second list (sales) contains the corresponding sales for 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 float NOT null

The Products list contains ProductID and ProductName two domains. The sales list contains Saleid, ProductID, and total. The ProductID in the Sales list provides an association between the two lists. The total amount in the Sales list contains the amount of cash sold per time, which appears as floating-point data.

The GetProducts () method in table B connects two databases and saves all the 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;
}

Related Article

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.