hi guys......i want to view a piechart extracting items from a mysql database. i've created a button on a jsp page(view.jsp) which is calling another jsp page(viewrst.jsp) when i click that. the code for (viewrst.jsp) is given below:

<%@ page language="java"%>
 
<%@page import="org.jfree.chart.*;"%>
<%@page import="java.sql.DriverManager;"%>
<%@page import="java.sql.*;"%>
<%@page import="org.jfree.data.jdbc.*;"%>
<%@page import="org.jfree.data.general.*;"%>
 
<%!
[B]public[/B] [B]class[/B] votepie {
 
    /**
     * @param args
     */
    
    [B]private[/B] PieDataset readData() {
        JDBCPieDataset data = [B]null[/B];
        String url = "jdbc:mysql://localhost/vote";
        Connection con;
        [B]try[/B] {
        Class.forName("com.mysql.jdbc.Driver");
        }
        [B]catch[/B] (ClassNotFoundException e) {
        System.err.print("ClassNotFoundException: ");
        System.err.println(e.getMessage());
        }
        [B]try[/B] {
        con = DriverManager.getConnection(url, "vote", "vote001");
        data = [B]new[/B] JDBCPieDataset(con);
        String sql = "SELECT option_text, counter FROM VOTE_VOTES where poll_id=1;";
        data.executeQuery(sql);
        con.close();
        }
        [B]catch[/B] (SQLException e) {
        System.err.print("SQLException: ");
        System.err.println(e.getMessage());
        }
        [B]catch[/B] (Exception e) {
        System.err.print("Exception: ");
        System.err.println(e.getMessage());
        }
        [B]return[/B] data;
        }
        
    [B]public[/B] [B]static void[/B] main(String[] args) {
        // TODO Auto-generated method stub
        
        votepie pd = [B]new[/B] votepie();
        pd.readData();
        
        //creating the chart
        JFreeChart chart = ChartFactory.createPieChart(
                "Sample Pie Chart",
                pd.readData(),
                true, // legend?
                true, // tooltips?
                [B]false[/B] // URLs?
                );
                // create and display a frame...
                ChartFrame frame = [B]new[/B] ChartFrame("First", chart);
                frame.pack();
                frame.setVisible([B]true[/B]);
 
    }
 
}
%>

& i'm getting this error when I click the button:

[B]HTTP Status 500 - [/B]

[B]type[/B] Exception report
[B]message[/B] 
[B]description[/B] [U]The server encountered an internal error () that prevented it from fulfilling this request.[/U]
[B]exception[/B] 
org.apache.jasper.JasperException: Unable to compile class for JSP: 





An error occurred at line: 44 in the jsp file: /viewrst.jsp
The method main cannot be declared static; static methods can only be declared in a static or top level type
41:         }
42:         return data;
43:         }
44:     public static void main(String[] args) {
45:         // TODO Auto-generated method stub
46:         
47:         votepie pd = new votepie();


Stacktrace:
    org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:85)
    org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
    org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:415)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:308)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:286)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:273)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:566)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:308)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
[B]note[/B] [U]The full stack trace of the root cause is available in the Apache Tomcat/6.0.10 logs.[/U]
[B]Apache Tomcat/6.0.10[/B]

is the prob related to classpath variables?the java is working when i run it, but not in jsp. please suggest what to do?......thankz.

Recommended Answers

All 3 Replies

Hmmmmm interesting, I would never say that main method bellong to JSP...
...or is this new programming approach?

ok..but when I remove "static" from there the error is not shown anymore!...what is going on,what to do then???? i'm confused

JSP has no "main" method in its body!
If you wish to execute something you should use SERVLET and outcome pass to another JSP page. You may want to have go on this tutorial http://java.sun.com/javaee/5/docs/tutorial/doc/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.