Connecting JfreeChart with Database

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
prachi
Posts: 1
Joined: Fri Jan 13, 2012 6:24 am
antibot: No, of course not.

Connecting JfreeChart with Database

Post by prachi » Mon Jan 16, 2012 6:28 am

Hi
The code below is not getting executed when i m trying to make a line chart JFreeChart
May be theres some problem with the persistence file.Every time I try to run it shows Jasper Exception...
It shows the following errors:
**An error occurred at line: 30 in the jsp file: /lineChart.jsp
EntityManager cannot be resolved to a type
**EntityManagerFactory cannot be resolved to a type
**Persistence cannot be resolved
**Query cannot be resolved to a type
**EntityTransaction cannot be resolved to a type


-----------------------------------------------
Entity Class
package Entity;
import javax.persistence.*;
@Entity
@Table(name="day_data")
public class Entity1 {
@Id
int meter_reg_id;
String av_kw;
String timestamp;
public int getMeter_reg_id() {
return meter_reg_id;
}
public void setMeter_reg_id(int meter_reg_id) {
this.meter_reg_id = meter_reg_id;
}
public String getAv_kw() {
return av_kw;
}
public void setAv_kw(String av_kw) {
this.av_kw = av_kw;
}
public String getTimestamp() {
return timestamp;
}
public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}


}
--------------------------------------------------------------------------------------------------

Jsp Page

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="java.awt.*" %>
<%@ page import="java.io.*" %>
<%@ page import="org.jfree.chart.*" %>
<%@ page import="org.jfree.chart.axis.*" %>
<%@ page import="org.jfree.chart.entity.*" %>
<%@ page import="org.jfree.chart.labels.*" %>
<%@ page import="org.jfree.chart.plot.*" %>
<%@ page import="org.jfree.chart.renderer.category.*" %>
<%@ page import="org.jfree.chart.urls.*" %>
<%@ page import="org.jfree.data.category.*" %>
<%@ page import="org.jfree.data.general.*" %>
<%@ page import ="java.sql.ResultSet" %>
<%@ page import ="java.sql.SQLException" %>

<%@ page import ="javax.persistence.*" %>
<%@ page import ="Entity.Entity1" %>
<%@ page import ="Entity.jpa_conn" %>

<%
// final double[][] data = new double[][]{
// {210, 300, 320, 265, 299},
// {200, 304, 201, 201, 340}
// };


// -------------------------------------------------------
EntityManager em=null;
int i=0;
try{
EntityManagerFactory emf=Persistence.createEntityManagerFactory("JPQL");
em=emf.createEntityManager();
EntityTransaction et=em.getTransaction();
et.begin();
Query q=em.createQuery("Select k.av_kw,k.timestamp from day_data k");
ResultSet rs=(ResultSet) q.getResultList();
final String series1 = "Data";
// column keys...
final String type1 = "Type 1";
final String type2 = "Type 2";
final String type3 = "Type 3";
final String type4 = "Type 4";
final String type5 = "Type 5";
final String type6 = "Type 6";
final String type7 = "Type 7";
final String type8 = "Type 8";

// create the dataset...
final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
// jpa_conn obj=new jpa_conn();

while(rs!=null){
Entity1 en=(Entity1)rs.getArray(i);
final String timeStamp=en.getTimestamp();
String kw=en.getAv_kw();
int a=Integer.parseInt(kw);



// dataset.addValue(a, series1, timeStamp);
dataset.addValue(a, kw, timeStamp);


// final CategoryDataset dataset =
// DatasetUtilities.createCategoryDataset(
// "Team ", "", data);



final JFreeChart chart = ChartFactory.createLineChart(
"Line Chart", "", "Value", dataset,
PlotOrientation.VERTICAL,
true, true, false);

final CategoryPlot plot = chart.getCategoryPlot();
plot.setForegroundAlpha(10.0f);

chart.setBackgroundPaint(new Color(249, 231, 236));

try {
final ChartRenderingInfo info = new ChartRenderingInfo
(new StandardEntityCollection());

final File file1 = new File("C:/Users/prachi01/Desktop/New folder/areachart.png");

ChartUtilities.saveChartAsPNG(file1, chart, 600, 400, info);}

catch (Exception e) {
out.println(e);
}

}


}
catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{

}
// -------------------------------------------------------------******
/* final String series1 = "First";


// column keys...
final String type1 = "Type 1";
final String type2 = "Type 2";
final String type3 = "Type 3";
final String type4 = "Type 4";
final String type5 = "Type 5";
final String type6 = "Type 6";
final String type7 = "Type 7";
final String type8 = "Type 8";

// create the dataset...
final DefaultCategoryDataset dataset = new DefaultCategoryDataset();

dataset.addValue(1.0, series1, type1);
dataset.addValue(4.0, series1, type2);
dataset.addValue(3.0, series1, type3);
dataset.addValue(5.0, series1, type4);
dataset.addValue(5.0, series1, type5);
dataset.addValue(7.0, series1, type6);
dataset.addValue(7.0, series1, type7);
dataset.addValue(8.0, series1, type8);

///dataset.addValue(5.0, series2, type1);
// dataset.addValue(7.0, series2, type2);
// dataset.addValue(6.0, series2, type3);
// dataset.addValue(8.0, series2, type4);
/// dataset.addValue(4.0, series2, type5);
// dataset.addValue(4.0, series2, type6);
// dataset.addValue(2.0, series2, type7);
// dataset.addValue(1.0, series2, type8);

// dataset.addValue(4.0, series3, type1);
// dataset.addValue(3.0, series3, type2);
//dataset.addValue(2.0, series3, type3);
//dataset.addValue(3.0, series3, type4);
//dataset.addValue(6.0, series3, type5);
//dataset.addValue(3.0, series3, type6);
// dataset.addValue(4.0, series3, type7);
// dataset.addValue(3.0, series3, type8);


//final CategoryDataset dataset =
//DatasetUtilities.createCategoryDataset(
// "Team ", "", data);



final JFreeChart chart = ChartFactory.createLineChart(
"Line Chart", "", "Value", dataset,
PlotOrientation.VERTICAL,
true, true, false);

final CategoryPlot plot = chart.getCategoryPlot();
plot.setForegroundAlpha(0.5f);

chart.setBackgroundPaint(new Color(249, 231, 236));

try {
final ChartRenderingInfo info = new ChartRenderingInfo
(new StandardEntityCollection());

final File file1 = new File("C:/Users/prachi01/Desktop/New folder/areachart.png");

ChartUtilities.saveChartAsPNG(file1,chart, 600, 400, info);
} catch (Exception e) {
out.println(e);
}*/

%>



<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<IMG SRC="C:/Users/prachi01/Desktop/New folder/areachart.png" WIDTH="600" HEIGHT="400" BORDER="0"
USEMAP="#chart">
<h1>hiiii huraaahhh</h1>

</body>
</html>
--------------------------------------------------------
Persitence FIle

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/ ... ce_2_0.xsd">
<persistence-unit name="JPQL" transaction-type="RESOURCE_LOCAL">

<class>com.infosys.enr.user.entity.UserEntity</class>
<class>com.infosys.enr.user.entity.UserRoleEntity</class>
<class>com.infosys.enr.user.entity.RoleEntity</class>
<class>com.infosys.enr.user.entity.PrivilegeEntity</class>
<class>com.infosys.enr.user.entity.RolePrivilegeEntity</class>
<class>Entity.Entity1</class>
<properties>
<property name="toplink.jdbc.url" value="jdbc:mysql://localhost:3306/emi"/>
<property name="toplink.jdbc.user" value="root" />
<property name="toplink.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="toplink.jdbc.password" value="root123" />
<property name="toplink.ddl-generation" value="create-tables" />
<property name="toplink.jdbc.read-connections.max" value="1" />
<property name="toplink.jdbc.read-connections.min" value="1" />
<property name="toplink.jdbc.write-connections.max" value="1" />
<property name="toplink.jdbc.write-connections.min" value="1" />
<property name="toplink.logging.level" value="SEVERE" />
</properties>
</persistence-unit>
</persistence>

Locked