database connectivity of chart values

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
srikanth

database connectivity of chart values

Post by srikanth » Thu Aug 31, 2000 11:25 am

i've made a multidimensional array of database values and also used sql package and passed the array to the datasourceobject.i'm able to compile the program correctly but during running nullpointer exception is coming.how could i solve this?i'm very urgent in solving
this problem.
thanks

Mark Rosche

RE: database connectivity of chart values

Post by Mark Rosche » Fri Sep 01, 2000 3:19 pm

Hi,

I cannot solve this problem, but I was wondering how you generated your multidimensional arrays from a database result set.

Any help would be appreciated..

Cheers,
Mark Rosche

Rawat Subodh Kumar

RE: database connectivity of chart values

Post by Rawat Subodh Kumar » Sat Sep 02, 2000 3:20 am

Dear Srikanth,

I beleive your result set is fetching some null values from database. What you can do:
1. use nvl function of oracle and make the value 0 (Zero) if it is a null value in your result set query.
2. (otherwise) After fetching data from database, check the return value and if it is null make it 0(Zero).

Alternativly, you can debug the your array after populating the data. This is dosen't look JFreeChart problem, better to post this in java news group.


Bye!

Subodh

Mark Rosche

RE: database connectivity of chart values

Post by Mark Rosche » Mon Sep 04, 2000 10:19 am

Hi all,

Sorry for the Newbie questions before...

Here I have a working Servlet that generates graphs on the fly from values retrieved from a database. I know it is a quick and dirty hack but it works...


Start code-----------------------------------------------------------

import java.awt.*;
import java.io.*;
import java.util.*;
import java.sql.*;
import java.math.*;
import javax.servlet.*;
import javax.servlet.http.*;

import Acme.JPM.Encoders.GifEncoder;

import com.jrefinery.chart.*;
import com.jrefinery.chart.ui.*;

public class SimpleChart extends HttpServlet {

private Connection con = null;

public void init(ServletConfig config) throws ServletException {
super.init(config);

try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection(
"jdbc:oracle:oci8:@(description=(address=(host=oracle)(protocol=tcp)(port=1521))(connect_data=(sid=oracle)))", "user", "passowrd");
}
catch (ClassNotFoundException e)
{
throw new UnavailableException(this, "Couldn't load the database driver");
}
catch (SQLException e)
{
throw new UnavailableException(this, "Couldn't get db connection");
}
}

static final int WIDTH = 250;
static final int HEIGHT = 200;

public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException ,IOException {

ServletOutputStream out = res.getOutputStream();




Frame frame = null;

Graphics g = null;

try
{
String request = req.getParameter("request");

Statement stmt = con.createStatement();

ResultSet rs = stmt.executeQuery("SELECT TO_CHAR(CURRENT_DATE,'HH24') AS HOUR, TO_CHAR(CURRENT_DATE,'MI') AS MINUTE, TO_CHAR(CURRENT_DATE,'SS') AS SECOND, TO_CHAR(VALUE, '9999990.00') AS PRICE FROM SCHEMA.\"" + request + "\" WHERE CURRENT_DATE LIKE SYSDATE");

Object[][][] results = new Object[1][78][78];

int i = 0;

while (rs.next())
{
int j = 0;
results[0][j] = new Time( rs.getInt("HOUR"),
rs.getInt("MINUTE"),
rs.getInt("SECOND")
);
j++;
results[0][j] = new Double(rs.getDouble("PRICE"));
i++;
}




rs.close();
stmt.close();

XYDataSource xyData = new DefaultXYDataSource(new String[] {requestWKN}, results);
JFreeChart myChart = JFreeChart.createTimeSeriesChart(xyData);
myChart.setTitle("");
myChart.setLegend(null);
myChart.setChartBackgroundPaint(new Color(58,110,165));
myChart.setAntiAlias(false);


Plot myPlot = myChart.getPlot();

Axis myVerticalAxis = myPlot.getAxis(Plot.VERTICAL_AXIS);
myVerticalAxis.setLabel("");

Axis myHorizontalAxis = myPlot.getAxis(Plot.HORIZONTAL_AXIS);
myHorizontalAxis.setLabel("");

frame = new Frame();
frame.addNotify();

// Get a graphics region of appropriate size, using the Frame
Image image = frame.createImage(WIDTH, HEIGHT);
g = image.getGraphics();

myChart.draw((Graphics2D) g, new Rectangle(WIDTH, HEIGHT));

// Encode and return what is painted
res.setContentType("image/gif");
GifEncoder encoder = new GifEncoder(image, out);
encoder.encode();
}
catch (SQLException e){
out.println("SQLException caught: " + e.getMessage());
}
finally {
if (g != null) g.dispose();
if (frame != null) frame.removeNotify();
}

}

public void destroy()
{
try
{
if (con != null) con.close();
}
catch (SQLException ignored){}
}

};

Locked