Correction to JDBCPieDataset

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
MasterJ
Posts: 19
Joined: Fri Jun 23, 2006 8:12 am

Correction to JDBCPieDataset

Post by MasterJ » Wed Jul 19, 2006 11:42 am

Dear david,
iI observed that JDBCPieDataset ,by default takes two columns(one field name,the other its value) of sevevrel Rows.

But what if we need to draw a graph for a single row and severel columns ?
I have written a small variant of the said class for my convenience and it worked fine.
I would like to know whether this is the only way or is there any other simple solution than extending the JDBCPieDataset and overriding 'executeQuery ' method.

Wishing you good,
MJ
Master Java

angel
Posts: 899
Joined: Thu Jan 15, 2004 12:07 am
Location: Germany - Palatinate

Re: Correction to JDBCPieDataset

Post by angel » Thu Jul 20, 2006 7:16 am

MasterJ wrote:Dear I would like to know whether this is the only way or is there any other simple solution than extending the JDBCPieDataset and overriding 'executeQuery ' method.
There is a simple solution: don't use teh JDBCPieDataset. Use the DefaultPieDataset and populate it by yourself.

MasterJ
Posts: 19
Joined: Fri Jun 23, 2006 8:12 am

This may easy for a single graph!

Post by MasterJ » Fri Jul 21, 2006 6:32 am

Hello Angel,
This may easy for a single graph!
But what if we don't know how many columns we would have in the row ,and the type of data in them.
I'm using these graphs in a WebApp. So I cannot write so many servlets for each graph; and what I did is I generated a Template like servlet which takes the type of the graph, title , domain axis label , range axis label,and the SQL Query . So that generating graphs in JSP/jspx pages was very easy.

What I'm trying to tell you is Is there i any way to do this or Should I post my Class which is a subclass of JDBCPieDataset)here in this forum.so that others can benefit from it?
Thanks anyway for your response,
MJ
Master Java

angel
Posts: 899
Joined: Thu Jan 15, 2004 12:07 am
Location: Germany - Palatinate

Re: This may easy for a single graph!

Post by angel » Fri Jul 21, 2006 7:43 am

MasterJ wrote:Hello Angel,
This may easy for a single graph!
But what if we don't know how many columns we would have in the row ,and the type of data in them.
I'm using these graphs in a WebApp. So I cannot write so many servlets for each graph; and what I did is I generated a Template like servlet which takes the type of the graph, title , domain axis label , range axis label,and the SQL Query . So that generating graphs in JSP/jspx pages was very easy.
I don't understand your problem. You can do all with the DefaultPieDataset what you can do with JDBCPieDataset. You don't have to write a servlet for each graph.
MasterJ wrote: What I'm trying to tell you is Is there i any way to do this or Should I post my Class which is a subclass of JDBCPieDataset)here in this forum.so that others can benefit from it?
Posting your source code is never a bad idea!

MasterJ
Posts: 19
Joined: Fri Jun 23, 2006 8:12 am

Simplicity

Post by MasterJ » Fri Jul 21, 2006 10:00 am

The beauty of JDBCPieDataset is that I only pas the Query to an in instance of JDBCPieDataset and it does the rest. Is that possible with DefaultPieDataset ?
Thanks for your interest in this,
MJ
Master Java

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Fri Jul 21, 2006 11:06 am

The problem with JDBCPieDataset is that it will read data in one format only. Whereas if you write your own (JDBC) code to read the database, then populate a DefaultPieDataset from that, you can handle as many different table/query formats as you want.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

MasterJ
Posts: 19
Joined: Fri Jun 23, 2006 8:12 am

My Suggestion!

Post by MasterJ » Sat Jul 22, 2006 4:12 am

Dear David,
Thanks for your reply.

I prefer the automatic behaviour of JDBCPieDataset to Manually setting values after we ourselves retrieving the values from the dtabase.
Or May be I didn't understand what you are saying, anyway,
I would like to suggest you to make this change to the execute query method in the next version ,if possible.

It makes work a lot easier.
the code is as below:



public void executeQuery(Connection con, String query,int type) throws SQLException {

Statement statement = null;
ResultSet resultSet = null;
if(type==2){
try {
statement = con.createStatement();
statement.setFetchSize(1);

resultSet = statement.executeQuery(query);
ResultSetMetaData metaData = resultSet.getMetaData();

int columnCount = metaData.getColumnCount();



if (!resultSet.next())
throw new SQLException(
"Sorry, You Must Supply a Single row of Numerical Data" );





int count=1;
double value = Double.NaN;

while (count<=columnCount) {

Comparable key = metaData.getColumnName(count);

int columnType = metaData.getColumnType(count);

switch (columnType) {
case Types.NUMERIC:
case Types.REAL:
case Types.INTEGER:
case Types.DOUBLE:
case Types.FLOAT:
case Types.DECIMAL:
case Types.BIGINT:
value = resultSet.getDouble(count);
setValue(key, value);
count++;
break;

case Types.DATE:
case Types.TIME:
case Types.TIMESTAMP:
Timestamp date = resultSet.getTimestamp(count);
value = date.getTime();
setValue(key, value);
count++;
break;

default:
System.err.println(
"JDBCCustomPieDataSet - unknown data type"
);
break;
}
}

fireDatasetChanged();

}
}
// and here we may write the original behaviour of the JDBCPieDataset;



finally {

if (resultSet != null) {
try {
resultSet.close();
}
catch (Exception e) {
System.err.println("JDBCCustomPieDataSet: swallowing exception.");
}
}
if (statement != null) {
try {
statement.close();
}
catch (Exception e) {
System.err.println("JDBCCustomPieDataSet: swallowing exception.");
}
}
}
}



Won't it work?

I thought It would be nice to share what I have to be useful for me.
I'll be happy if it is useful for atleast one person.


Thaking you,
MJ
Master Java

Locked