Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: how to plot a line graph by reading the values from database?

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default how to plot a line graph by reading the values from database?

    hello
    I have a database consisting of datetime of type timestamp and memory used of int.
    Database(test4.test) consists of
    15 2012-02-03 15:14:43
    12 2012-05-06 16:17:46

    But we are unable to plot a graph showing the errors saying
    Exception in thread "main" java.lang.ClassCastException: java.sql.Timestamp cannot be cast to java.sql.Date
    at org.jfree.data.jdbc.JDBCCategoryDataset.executeQue ry(JDBCCategoryDataset.java: at org.jfree.data.jdbc.JDBCCategoryDataset.executeQue ry(JDBCCategoryDataset.java: at Chart1.main(Chart1.java:26)
    Java Result: 1
    How to resolve this error.
    the following code we have used

    import java.sql.*;
    import java.io.*;
    import org.jfree.ui.*;
    import org.jfree.chart.ChartPanel;
    import org.jfree.chart.ChartFactory;
    import org.jfree.chart.ChartUtilities;
    import org.jfree.chart.JFreeChart;
    import org.jfree.chart.plot.PlotOrientation;
    import org.jfree.data.*;
    import org.jfree.data.jdbc.JDBCCategoryDataset;

    public class Chart1 {
    public static void main(String[] args) throws Exception {
    String query = "SELECT * from test";
    JDBCCategoryDataset dataset = new JDBCCategoryDataset( "jdbc:mysql://localhost:3306/test4", "com.mysql.jdbc.Driver","root", "root");
    dataset.executeQuery(query);
    JFreeChart chart = ChartFactory.createLineChart("Test", "Id", "Score", dataset, PlotOrientation.VERTICAL, true, true, false);
    ChartPanel chartPanel = new ChartPanel(chart);
    chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
    ApplicationFrame f = new ApplicationFrame("Chart");
    f.setContentPane(chartPanel);
    f.pack();
    f.setVisible(true);
    }
    }

    please tel what is the error and how to resolve it


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,358
    Thanks
    182
    Thanked 834 Times in 777 Posts
    Blog Entries
    5

    Default Re: how to plot a line graph by reading the values from database?

    A Timestamp cannot be cast to a Date...your database appears to have Timestamp. If you want a java.sql.Date, then a) change the Timestamp data type b) Add a column of type Date c) Convert the Timestamp to a java.sql.Date in your code. I have never used JDBCCategoryDataset, but the exception is thrown from within. Perhaps try to manually populate a Dataset using the last option

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: how to plot a line graph by reading the values from database?

    how to convert Timestamp to java.sql.date ?please tell me

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,358
    Thanks
    182
    Thanked 834 Times in 777 Posts
    Blog Entries
    5

    Default Re: how to plot a line graph by reading the values from database?

    Timestamp extends java.util.Date. java.sql.Date extends java.util.Date. You can construct a instance of java.sql.Date using long representation, which can be retrieved using the getTime() method provided by java.util.Date. If you have not already, I recommend diving head first into the API to study these classes and methods
    Date (Java Platform SE 6)
    Timestamp (Java Platform SE 6)

  5. The Following User Says Thank You to copeg For This Useful Post:

    priti (April 4th, 2012)

  6. #5
    Junior Member
    Join Date
    Mar 2012
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: how to plot a line graph by reading the values from database?

    if we put data type as DATE .date value is not storing in database..it shows above error
    Value '0000-00-00' can not be represented as java.sql.Date
    please resolve this error.

  7. #6
    Junior Member
    Join Date
    Mar 2012
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: how to plot a line graph by reading the values from database?

    if we put data type as DATE .date value is not storing in database..it shows below error
    Value '0000-00-00' can not be represented as java.sql.Date
    please resolve this error.

  8. #7
    Junior Member
    Join Date
    Mar 2012
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: how to plot a line graph by reading the values from database?

    we resolved this problems...

  9. #8
    Junior Member
    Join Date
    Mar 2012
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: how to plot a line graph by reading the values from database?

    how to construct a instance of java.sql.Date using long representation.please give some sample code of it

Similar Threads

  1. How to plot line graph using jfreechart reading from text file
    By priti in forum Java Theory & Questions
    Replies: 9
    Last Post: March 31st, 2012, 02:39 PM
  2. how to plot the line graph using jfreechart reading from text file
    By priti in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 31st, 2012, 07:53 AM
  3. [SOLVED] compare form values with database values
    By VaniRathna in forum Java Servlet
    Replies: 2
    Last Post: October 24th, 2011, 03:48 AM
  4. Trouble Reading Line in File
    By Mandraix in forum What's Wrong With My Code?
    Replies: 9
    Last Post: April 4th, 2011, 11:47 PM
  5. Java Bar graph takes user input to create graph?
    By kenjiro310 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 31st, 2011, 08:37 AM