2

In a (using mysql)table one of my column(i.e. colour) is like :

|Color|
======
|Red  |
|Blue |
|Green|
|Red  |
|Green|
|Green|
|Red  |
|Red  |

After establishing,jdbc connection in my current jsp page: I'm trying to show the value in a pie chart(it's wriien in jscript,where I'll putting the value from mysql) like :

|Color|no. of Times|
====================
|Red  |   4        |
|Blue |   1        |
|Green|   3        |

Buy I am not able to put values into the graph(I guess there is problem in by code):

<%
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/apps","root","root");
Statement stmt = con.createStatement();
String sql2="select distinct(color) as COLOR from user_management order by device;";
ResultSet rs2 = stmt.executeQuery(sql2);
String s = rs2.getString("color");
while(rs2.next()){
    String device = rs2.getString("color");
    List<String> list=new ArrayList<String>();
    list.add(device);
    }
// I guess here will be some code for device value in String, say "s" 

String sql1 = "select count(case when color='"+s+"' then 1 end) as COLOR from user_management";
ResultSet rs1 = stmt.executeQuery(sql1);
System.out.println(sql1);
%>

then putting the resultset in piechart table

<table id="chartData">
  <% while(rs2.next()){ %>
  <% while(rs1.next()){ %>
    <tr>
      <th>DEVICE</th><th>NUMBER</th>
     </tr>

    <tr style="color: #0DA068">
      <td><%= rs2.getString("device")%></td><td><%= rs1.getString(1)%></td>
    </tr>

   <% }     
   %>
   <% }     
   %>
 </table>

Any inputs that how to fetch the column value in a chart where color-name as well as no. of color happens at a same time.

4

1 回答 1

1

使用GROUP BY

SELECT Color, count(*) AS times FROM colors GROUP BY Color;
于 2012-08-16T10:36:31.277 回答