2012-03-15 112 views
-2

我有一個連接到mysql數據庫的servlet,結果集從數值列中獲取一些值,這些值在谷歌鏈接中產生圖表作爲結果...這是我的代碼它:如何每10秒刷新一次servlet write.out方法?

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(); 
      //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 100"); 
       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=1000x300" + 
         "&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;");*/ 

       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()) { 
        out.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); 
         } 

         //out.write("<tr>" + 
           // "<td>" + resultSetMetaData.getColumnName(i) + "</td>" + 
           // "<td>" + value + "</td>" + 
           //"</tr>"); 
        } 
        out.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); 

}

如何才能得到一個新的圖每10秒提供我知道的價值觀會改變在列使用值? ...請幫助我...

回答

1

您無法多次刷新ServletOutputStream。它會抱怨。你必須創建一個具有某種標識符的後臺處理線程,並使用客戶端的ajax來獲取當前的一組計算並進行渲染。

+0

我不知道該怎麼做那個先生......你能幫助我嗎? ..代碼什麼的? ...請幫我先生... – Becks 2012-03-15 21:07:16