পৃষ্ঠাসমূহ

Search Your Article

CS

 

Welcome to GoogleDG – your one-stop destination for free learning resources, guides, and digital tools.

At GoogleDG, we believe that knowledge should be accessible to everyone. Our mission is to provide readers with valuable ebooks, tutorials, and tech-related content that makes learning easier, faster, and more enjoyable.

What We Offer:

  • 📘 Free & Helpful Ebooks – covering education, technology, self-development, and more.

  • 💻 Step-by-Step Tutorials – practical guides on digital tools, apps, and software.

  • 🌐 Tech Updates & Tips – simplified information to keep you informed in the fast-changing digital world.

  • 🎯 Learning Support – resources designed to support students, professionals, and lifelong learners.

    Latest world News 

     

Our Vision

To create a digital knowledge hub where anyone, from beginners to advanced learners, can find trustworthy resources and grow their skills.

Why Choose Us?

✔ Simple explanations of complex topics
✔ 100% free access to resources
✔ Regularly updated content
✔ A community that values knowledge sharing

We are continuously working to expand our content library and provide readers with the most useful and relevant digital learning materials.

📩 If you’d like to connect, share feedback, or suggest topics, feel free to reach us through the Contact page.

Pageviews

Friday, January 20, 2017

JFreeChart - Database Interface

This chapter explains how you can read simple data from a database table and then use JFreeChart to create a chart of your choice.

Business Data

Consider we have following MySQL table mobile_tbl(mobile_brand VARCHAR(100) NOT NULL, unit_sale INT NO NULL);
Consider this table is having following records :
Mobile Brands Unit Sales
IPhone5S 20
Samsung Grand 20
MotoG 40
Nokia Lumia 10

Chart Generation Using Database

Following is the code to create a Pie Chart based on the information provided in mobile_tbl table available in test_db in a MySQL database. Based on your requirements, you can use any other database.
import java.io.*; 
import java.sql.*; 
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.ChartFactory; 
import org.jfree.chart.JFreeChart; 
import org.jfree.data.general.DefaultPieDataset;

public class PieChart_DB
{
   public static void main( String[ ] args )throws Exception
   {
      String mobilebrands[] = {
      "IPhone 5s",   
      "SamSung Grand",   
      "MotoG",            
      "Nokia Lumia" 
      };
      
      /* Create MySQL Database Connection */
      Class.forName( "com.mysql.jdbc.Driver" );
      Connection connect = DriverManager.getConnection( 
      "jdbc:mysql://localhost:3306/jf_testdb" ,     
      "root",     
      "root123");
      
      Statement statement = connect.createStatement( );
      ResultSet resultSet = statement.executeQuery("select * from dataset_tb" );
      DefaultPieDataset dataset = new DefaultPieDataset( );
      while( resultSet.next( ) ) 
      {
         dataset.setValue( 
         resultSet.getString( "brandname" ) ,
         Double.parseDouble( resultSet.getString( "datavalue" )));
      }
      JFreeChart chart = ChartFactory.createPieChart(
         "Mobile Sales",  // chart title           
         dataset,         // data           
         true,            // include legend          
         true,           
         false );

      int width = 560; /* Width of the image */
      int height = 370; /* Height of the image */ 
      File pieChart = new File( "Pie_Chart.jpeg" );
      ChartUtilities.saveChartAsJPEG( pieChart , chart , width , height );
   }
}
Let us keep the above Java code in PieChart_DB.java file, and then compile and run it from command prompt as follows:
$javac PieChart_DB.java 
$java PieChart_DB 
If everything is fine , it will compile and run to create JPEG image file named Pie_Chart.jpeg having the following chart.
JFreeChart Database Interface

No comments:

Post a Comment