1

i have data in mysql DB, i want to plot it in line charts i'm using highcharts, i want to insert those data into array , i'm using this code , the problem is the data wont be presented in graph,help me plz

<div id="container"
                style="min-width: 310px; height: 400px; margin: 0 auto"></div>
            <script type="text/javascript">
            <%
    String hostdb = "localhost:3306"; // MySQl host
        String userdb = "root"; // MySQL username
        String passdb = "Jenkov31994"; // MySQL password
        String namedb = "akbd"; // MySQL database name

        // Establish a connection to the database
        DriverManager.registerDriver(new com.mysql.jdbc.Driver());
        Connection con = DriverManager.getConnection("jdbc:mysql://" + hostdb + "/" + namedb, userdb, passdb);

        String sql = "SELECT * FROM climdata";
        PreparedStatement pt = con.prepareStatement(sql);
        ResultSet rs = pt.executeQuery();
        ResultSet rsize = rs;
        int i = 0;
        int j = 0;
        while (rsize.next()) {
    j++;
        }

        String cat[] = new String[j];
        int ht[] = new int [j];
        int lt[] = new int[j];
        int at[] = new int[j];

        while (rs.next()) { 

    java.sql.Date date = rs.getDate("dateinsert");
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    String dateStr = dateFormat.format(date);
    cat[i] = dateStr; 
   ht[i] = rs.getInt("hightemp");
   lt[i] = rs.getInt("lowtemp");
   at[i] = rs.getInt("avgtemp");
        i++;
    }
    out.print("chart();");
%>
function chart (){
                Highcharts.chart('container', {
                    chart : {
                        type : 'line'
                    },
                    title : {
                        text : 'daily Temperature'
                    },
                    subtitle : {
                        text : 'ZONE H'
                    },
                    xAxis : {
                        categories :'<%=cat%>'

                },
                    yAxis : {
                        title : {
                            text : 'emperature (C)'
                        }
                    },
                    plotOptions : {
                        line : {
                            dataLabels : {
                                enabled : true
                            },
                            enableMouseTracking : false
                        }
                    },
                    series : [
                            {
                                name : 'MaxTemperature',
                                data : '<%=ht%>'
                            },
                            {
                                name : 'AVGTemperature',
                                data : '<%=at%>'
                            },
                            {
                                name : 'MinTemperature',
                                data : '<%=lt%>'
                            }, ]
                });}
            </script>
lina20
  • 25
  • 5
  • I think that your code has problem at the line `ResultSet rsize = rs;` when you call `rsize.next()` the `rs` also next too. So that the `while(rs.next())` does not have any things to loop. There are many ways to get the resultset size, [here](https://stackoverflow.com/questions/19829123/how-get-length-or-size-of-resultset-in-java) is the way to get it instead. – TuyenNTA Jun 11 '17 at 02:11
  • 1
    what, if any, errors are you getting? where? in console? ide? details are critical here. – albert Jun 11 '17 at 02:35
  • sorry , i use javascript and java with eclipse ide ,, i solved the problem , i have used arraylist in place of array ,, but now i'm looking for how to limit the number of elements in X axis ,i don't want all the days just the 10 last days – lina20 Jun 11 '17 at 23:21
  • so trim your array to the last 10 values – morganfree Jun 12 '17 at 11:55

0 Answers0