1

I have the following code for the scatter plot. Is it okay or have i donee something wrong. I am getting error. My code is below:

    public void display3() throws SQLException, ClassNotFoundException{

        //DefaultCategoryDataset dataset = new DefaultCategoryDataset();
         XYSeriesCollection dataset = new XYSeriesCollection();
         XYSeries series = new XYSeries("Production");
        String JDBC_DRIVER="com.mysql.jdbc.Driver";
    String DB_URL="jdbc:mysql://localhost/data2";
    Connection conn;
    Statement stmt;
    String USER = "root";
    String PASS = "";
        try{
            Class.forName(JDBC_DRIVER);
            conn=DriverManager.getConnection(DB_URL,USER,PASS);
            System.out.println("Creating statement...");
            stmt = conn.createStatement();
                        String sql;
                        sql="SELECT * FROM `production` WHERE crop_id = 1 AND location_id = 1";
                        ResultSet rs=stmt.executeQuery(sql);

                        while (rs.next()){
                            //String student = rs.getString("studentname");
                            String yeartext = rs.getString("year_of_production");
                            //double value = Double.parseDouble(text);
                            String productiontext = rs.getString("production_amount");
                            double production = Double.parseDouble(productiontext);
                            double year = Double.parseDouble(yeartext);
                            series.add(year,production) ;





                        }
                        dataset.addSeries(series);

                        JFreeChart chart = ChartFactory.createScatterPlot("Scatter Plot","Year","Paddy Production", dataset);

                        CategoryPlot p = chart.getCategoryPlot();
                        ChartPanel chartPanel = new ChartPanel(chart, false);
                        jPanel9.setLayout(new BorderLayout());
                        jPanel9.add(chartPanel, BorderLayout.EAST);
                        jPanel9.add(chartPanel);
                        SwingUtilities.updateComponentTreeUI(this);
                        p.setRangeGridlinePaint(blue);

                        System.out.println("Database created successfully...");

                }catch(SQLException se){
                    //Handle errors for JDBC
                    System.out.println("Connect failed ! ");
                    se.printStackTrace();
//                    JOptionPane.showMessageDialog(MajorUI.this, err.getMessage());
                    }

    }

The error i got is below. The project doesnot contain any error. I think i am making wrong in syntax but could not figure out what is wrong

    run:
[EL Info]: 2014-07-05 22:08:06.369--ServerSession(2052101)--EclipseLink, version: Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5
[EL Info]: connection: 2014-07-05 22:08:06.874--ServerSession(2052101)--file:/D:/agriculture project neabeans/UI/build/classes/_data2?zeroDateTimeBehavior=convertToNullPU login successful
Creating statement...
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: org.jfree.chart.plot.XYPlot cannot be cast to org.jfree.chart.plot.CategoryPlot
nzy
  • 854
  • 2
  • 15
  • 28
  • 1) Use a consistent and logical indent for code blocks. The indentation of the code is intended to help people understand the program flow. 2) A single blank line of white space in source code is *always* enough. Blank lines after `{` or before `}` are also typically redundant. 3) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete and Verifiable Example). 4) Please use code formatting for code, **input/output** & structured documents like HTML or XML. To do that, select the sample and click the `{}` button above the messaged posting/editing form. – Andrew Thompson Jul 06 '14 at 03:17
  • you are adding the series multiple times in the loop. you should build it and add only once. – MK. Jul 06 '14 at 03:47
  • @MK and how can i do that??? Can u suggest me what code should i alter.? – nzy Jul 06 '14 at 03:49
  • try moving dataset.addSeries(series); out of the loop? – MK. Jul 06 '14 at 03:50
  • @MK i did that and the error is different. Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: org.jfree.chart.plot.XYPlot cannot be cast to org.jfree.chart.plot.CategoryPlot – nzy Jul 06 '14 at 03:53
  • @trashgod i have updated my code and error,too. Maybe you could help now – nzy Jul 06 '14 at 05:10
  • Not without a complete example, a shown [here](http://stackoverflow.com/a/24592754/230513). – trashgod Jul 06 '14 at 13:23

0 Answers0