使用Google Chart API从数据库生成图表

时间:2012-03-14 00:11:50

标签: java database servlets charts

我有一个servlet应该从谷歌图表api生成图像图表,但它不是.. 这是我的servlet的代码:

package Graph;

public class Graph extends HttpServlet {

    private static final String DatabaseConnection = null;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Graph() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        // TODO Auto-generated method stub
                OutputStreamWriter writer = new OutputStreamWriter(response.getOutputStream());
                System.out.println("SensorGraph");

                response.setContentType("text/html");
                PrintWriter out = response.getWriter();
                //sensor Id
                String id = request.getParameter("id");
                // limit in the query
                String limit = request.getParameter("limit");
                DatabaseConnection db_connection =  new DatabaseConnection()._instance;
                Connection connection = null;
                try {
                    connection = db_connection.getConnection();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                Statement statement;
                ResultSet resultSet;
                try {
                    statement = (Statement) connection.createStatement();
                    resultSet = statement.executeQuery("select * from Sensor_Entries order by EntryTime asc limit 5)");
                    String values = "";
                    while(resultSet.next()) {
                        String value = resultSet.getString("EntryValue");
                        values += (value) + ",";
                    }
                    System.out.println(values);
                    values = values.substring(0, values.length()-1);


                    String image = "http://chart.apis.google.com/chart?" +
                            "chxr=0,0,1000" +
                            "&chxs=0,676767,12.167,-0.5,l,676767" +
                            "&chxt=y" +
                            "&chs=300x225" +
                            "&cht=ls" +
                            "&chco=3D7930" +
                            "&chds=0,1000" +
                            "&chd=t:" + values+
                            "&chg=14.3,-1,1,1" +
                            "&chls=2,4,0" +
                            "&chma=0,6%7C3" +
                            "&chm=B,C5D4B5BB,0,0,0" +
                            "&chtt=Sensor+Values" +
                            "&chts=F00D0D,14.5";
                    if(request.getParameter("img") != null)
                        out.write("<img src=\"" + image + "\" />");

                    statement = (Statement) connection.createStatement();
                    resultSet = statement.executeQuery("select * from Sensors join " +
                            "(select * from Sensor_Entries order by EntryTime desc) as Sensor_Entries on " +
                            "(Sensors.SensorID=Sensor_Entries.SensorID) where Sensors.SensorID='" + id + "' " +
                            "group by Sensors.SensorID;");

                    ResultSetMetaData resultSetMetaData = (ResultSetMetaData) resultSet.getMetaData();
                    if(resultSet.next()) {
                        writer.write("<table>");
                        for(int i = 1; i < resultSetMetaData.getColumnCount() + 1; i++) {
                            String value = "";
                            int type = resultSetMetaData.getColumnType(i);
                            if(type == Types.INTEGER || type == Types.TINYINT) {
                                value = "" + resultSet.getInt(i);
                            } else if(type == Types.VARCHAR) {
                                value = resultSet.getString(i);
                            } else if(type == Types.TIMESTAMP) {
                                value = resultSet.getTimestamp(i).toString();
                            } else if(type == Types.DOUBLE) {
                                value = "" + resultSet.getDouble(i);
                            }
                            writer.write("<tr>" +
                                    "<td>" + resultSetMetaData.getColumnName(i) + "</td>" +
                                    "<td>" + value + "</td>" +
                                    "</tr>");
                        }
                        writer.write("</table></center>");
                    }
                    else
                        out.write(image);
                    out.flush();
                    out.close();
                } catch (SQLException ignored) { ignored.printStackTrace();  }
            }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
}
}

你能帮我解决这个问题吗? ..我正在使用eclipse ganymede和tomcat 6 ... DatabaseConnection db_connection = new DatabaseConnection()._ instance;只是一个连接实例,我从一个有意创建的类连接到数据库...

1 个答案:

答案 0 :(得分:0)

...此问题现已解决...不需要Graph的构造函数中的super()。这解决了图表的主要问题没有出现......

我现在需要能够刷新从EntryValues表中获取数据的整个过程并重新生成图表...有没有人可以选择如何处理这样的问题? ..说每10秒刷新一次? ...请注意,我不是要刷新已经生成的grahp,我想每10秒检索一次数据,将值存储在谷歌图形链接中,最终会生成图形图像...

感谢您的支持,如果有人不得不处理此类问题,请尽快通知我......