JFreeChart & JDBC Problem

Hi everyone ,

I have a problem while drawing a JFreeChart's line chart... I want to draw a line chart which has it's datas from a database but i don't know how i can exactly do it! I'm using JFreeChart lib to do so. This is my code which reach the database and get the values into database , in the Main class i have a initDataSet function to get the datas from database to dataset. I also have 3 other class called FileDataSet , MysqlDbConnection and Parser.

If you can help me i'll be really appreciate.

import org.jfree.chart.ChartPanel;

import org.jfree.chart.JFreeChart;

import org.jfree.data.category.DefaultCategoryDataset;

import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.File;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

publicclass Mainextends JFrame{

private File source;

private JTextField dbName;

private JLabel dbNameLabel;

private JTextField userName;

private JLabel userNameLabel;

private JPasswordField password;

private JLabel passwordLabel;

private JButton openFileButton;

private JButton connectDatabaseAndLoadButton;

private JPanel panel1;

MysqlDbConnection conn;

DefaultCategoryDataset dataset;

JFreeChart chart;

ChartPanel chartPanel;

public Main(){

super("main");

setUpGUI();

openFileButton.addActionListener(new ActionListener(){

publicvoid actionPerformed(ActionEvent e){

openFile();

}

});

connectDatabaseAndLoadButton.addActionListener(new ActionListener(){

publicvoid actionPerformed(ActionEvent e){

if (dbName.getText().equals("") || userName.getText().equals("") || password.getPassword().equals("")){

JOptionPane.showMessageDialog(null,"Please fill the necessary fields properly!!!");

return;

}

connectDatabaseAndLoadButton.setEnabled(false);

parseAndInsert();

}

});

}//end of constructor

privatevoid openFile(){

JFileChooser fileChooser =new JFileChooser(new File("."));

fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);

//show file chooser

int result = fileChooser.showOpenDialog(this);

if (result == JFileChooser.CANCEL_OPTION){

return;

}

source = fileChooser.getSelectedFile();

if (source.getName().equalsIgnoreCase("dataset.txt")){

connectDatabaseAndLoadButton.setEnabled(true);

openFileButton.setEnabled(false);

}else{

JOptionPane.showMessageDialog(null,"Wrong File!!!");

}

}//end of method openFile

privatevoid parseAndInsert(){

Parser parser =new Parser(source);

conn =new MysqlDbConnection(dbName.getText(), userName.getText(),new String(password.getPassword()));

int frames;

float seconds;

float xCoord;

float yCoord;

boolean isCamera1;

Statement sqlStatement = conn.getStatement();

String tables[] =new String[7];

String tokens[] = parser.parseNextLine();

for (int i = 2, j = 0; i < 23; i++){

if ((i - 2) % 3 == 0){

//JOptionPane.showMessageDialog(null, createTable(tokens[i]));

tables[j++] = tokens[i];

try{

sqlStatement.executeUpdate(createTable(tokens[i]));

//tables[ (int)((i-2)/7) ] = tokens[ i ];

}catch (SQLException e){

e.printStackTrace();

}

}

}

parser.parseNextLine();

while ((tokens = parser.parseNextLine()) !=null){

frames = Integer.parseInt(tokens[0]);

seconds = Float.parseFloat(tokens[1]);

for (int i = 2; i < 23; i += 3){

xCoord = Float.parseFloat(tokens[i]);

yCoord = Float.parseFloat(tokens[i + 1]);

if (Integer.parseInt(tokens[i + 2]) == 1)

isCamera1 =true;

else

isCamera1 =false;

//JOptionPane.showMessageDialog( null, insertTable( frames, seconds, xCoord, yCoord,isCamera1,tables[(int)((i-2)/3)] ) );

try{

sqlStatement.executeUpdate(insertTable(frames, seconds, xCoord, yCoord, isCamera1, tables[(int) ((i - 2) / 3)]));

}catch (SQLException e){

e.printStackTrace();

}

}

//break;

}

//

JOptionPane.showMessageDialog(null,"File parsed and inserted to database");

}//end of method parseAndInsert

private String createTable(String tableName){

String sqlSentence ="CREATE TABLE " + tableName +" ";

sqlSentence +="( FRAMES INTEGER,";

sqlSentence +=" SECONDS FLOAT,";

sqlSentence +=" X_COORD FLOAT,";

sqlSentence +=" Y_COORD FLOAT,";

sqlSentence +=" IS_CAMERA_1 BOOLEAN )";

return sqlSentence;

}//end of method createTable

private String insertTable(int fra,float sec,float x,float y,boolean cam, String table){

String sqlSentence ="INSERT INTO " + table +" VALUES(";

sqlSentence +=" " + fra +", " + sec +", " + x +", " + y +", " + cam +" )";

return sqlSentence;

}//end of method insertTable

publicvoid initDataSet(){

// row keys...

String series1 ="GREEN";

dataset =new DefaultCategoryDataset();

Statement sql = conn.getStatement();

ResultSet rs;

try{

rs = sql.executeQuery("SELECT * from green");

int i = 0;

while (rs.next()){

dataset.addValue(rs.getDouble("X_COORD"), series1,new String("" + (i++)));

}

}catch (SQLException e){

e.printStackTrace();//To change body of catch statement use File | Settings | File Templates.

}

}

publicvoid setUpGUI(){

panel1 =new JPanel();

panel1.setLayout(new GridLayout(6, 2));

panel1.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(),null));

dbName =new JTextField();

dbNameLabel =new JLabel();

dbNameLabel.setText("Database Name to Connect");

panel1.add(dbNameLabel);

panel1.add(dbName);

userName =new JTextField();

userNameLabel =new JLabel();

userNameLabel.setText("Database User Name");

panel1.add(userNameLabel);

panel1.add(userName);

password =new JPasswordField();

passwordLabel =new JLabel();

passwordLabel.setText("Current User Password");

panel1.add(passwordLabel);

panel1.add(password);

connectDatabaseAndLoadButton =new JButton();

connectDatabaseAndLoadButton.setEnabled(false);

connectDatabaseAndLoadButton.setText("Connect Database and Load");

connectDatabaseAndLoadButton.setMnemonic('C');

connectDatabaseAndLoadButton.setDisplayedMnemonicIndex(0);

openFileButton =new JButton();

openFileButton.setEnabled(true);

openFileButton.setText("Open File \"dataset.txt\"");

openFileButton.setMnemonic('O');

openFileButton.setDisplayedMnemonicIndex(0);

panel1.add(openFileButton);

panel1.add(connectDatabaseAndLoadButton);

dbNameLabel.setLabelFor(dbName);

userNameLabel.setLabelFor(userName);

passwordLabel.setLabelFor(password);

}

publicstaticvoid main(String[] args){

Main frame =new Main();

frame.setContentPane(frame.panel1);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.pack();

frame.setVisible(true);

}

}//End Of Class Main

Message was edited by:

jannisery

Message was edited by:

jannisery

[14083 byte] By [janniserya] at [2007-10-3 9:27:13]
# 1
1. Separate your Data Access Logic from your GUI 2. What is your specific question.3. Make sure your examples are Short,Self Contained, Compilable and Executable4. If this is a JFreeChart question you might want to put it on that forum instead.
zadoka at 2007-7-15 4:41:34 > top of Java-index,Java Essentials,Java Programming...
# 2

In fact the problem is that (mainly not directly related with JFreeChar):

I want to draw a line chart (or pie char etc... any possible) which has it's datas from a specific database. For example ; i have a speed column in my database and want to draw a line chart (x-axis is time and y-axis is speed)

I have this code to get the datas into dataset :

public void initDataSet() {

// row keys...

String series1 = "GREEN";

dataset = new DefaultCategoryDataset();

Statement sql = conn.getStatement();

ResultSet rs;

try {

rs = sql.executeQuery("SELECT * from green");

int i = 0;

while (rs.next()) {

dataset.addValue(rs.getDouble("X_COORD"), series1, new String("" + (i++)));

}

} catch (SQLException e) {

e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.

}

}

Now , the problem is that how can i make a chart from this datas? How can i draw a chart from this datas?

janniserya at 2007-7-15 4:41:34 > top of Java-index,Java Essentials,Java Programming...
# 3

The question is directly related to JFreeChart. You are asking how to create a chart based on data from a database.

That is not very different than if the data came from a text file.

You are asking a more specific question then before but you still need to:

Separate your Data Access Logic from your GUI

(I know you think this is not important and are ignoring it)

Make sure your examples are Short,Self Contained, Compilable and Executable

(It is much easier to help if we can run your example, but since you are using JFreeChart we can't).

JFreeChart has something like a JDBCCategoryDataset.

zadoka at 2007-7-15 4:41:34 > top of Java-index,Java Essentials,Java Programming...