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 2 of 2

Thread: Google Chart from database Servlet not working

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

    Default Google Chart from database Servlet not working

    Hi all I am trying to produce a google line chart with the following classes but its not producing anything so far .. I dont see why this code desnt work .. I am using eclipse indigo and tomcat 6 and I retrieve data values from a mysql table...

    My database connection class :

    package Graph;
    import java.sql.DriverManager;
    import java.sql.SQLException;
     
    import com.mysql.jdbc.Connection;
     
    public final class DatabaseConnection {
     
        private static final String HOST = "localhost:3306",
                DATABASE = "database name",
                USERNAME = "username",
                PASSWORD = "password";
     
        private Connection connect;
     
        public static final DatabaseConnection _instance = new DatabaseConnection();
     
        public DatabaseConnection() {
            try {
                Class clazz = Class.forName("com.mysql.jdbc.Driver");
                connect();
            } catch (SQLException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
     
        public Connection getConnection() throws SQLException {
            if(!connect.isValid(100))
                connect();
        	return connect;
        }
     
        public void connect() throws SQLException {
            connect = (Connection) DriverManager.getConnection("jdbc:mysql://" + HOST
                    + "/" + DATABASE, USERNAME, PASSWORD);
        }
    }


    my Chart Servlet that should basically form a google link that produces an image ( the chart) .. and no image is shown. code below:
    package Graph;
     
    import java.io.IOException;
    import java.io.OutputStreamWriter;
    import java.io.PrintWriter;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Types;
     
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
     
    import com.mysql.jdbc.Connection;
    import com.mysql.jdbc.ResultSetMetaData;
    import com.mysql.jdbc.Statement;
     
    /**
     * Servlet implementation class Graph
     */
    public class Graph extends HttpServlet {
    	private static final long serialVersionUID = 1L;
    	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 {
     
    		        OutputStreamWriter writer = new OutputStreamWriter(response.getOutputStream());
    		        System.out.println("SensorGraph");
     
    		        response.setContentType("text/html");
    		        PrintWriter out = response.getWriter();
     
    		        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+  \\ magic line - gives the values to the link 
    		                    "&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);
    }
    }


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Google Chart from database Servlet not working

    What are the errors/exceptions? Paste the full error/exception message here.
    Anyone who stops learning is old, whether at twenty or eighty. Anyone who keeps learning stays young. The greatest thing in life is to keep your mind young.

    - Henry Ford

Similar Threads

  1. Replies: 0
    Last Post: March 11th, 2012, 04:57 PM
  2. servlet refresh problem after database query
    By adec in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 12th, 2012, 12:29 PM
  3. how to searching data from database using servlet??
    By mr.nash in forum Java ME (Mobile Edition)
    Replies: 1
    Last Post: December 21st, 2011, 02:09 PM
  4. scatterplot chart
    By anikal in forum AWT / Java Swing
    Replies: 2
    Last Post: August 23rd, 2011, 11:47 PM
  5. How to create pie or other diagrams in jsp page?
    By sundarjothi in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: July 8th, 2009, 06:44 AM