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