0

JSPから取得した結果セットの値をJavascriptで配列に渡したい。私を助けてください。

JSP code that fetches data from database:
ResultSet resultset12 = 
            statement12.executeQuery("select * from(select HOST_NAME,INSTANCE_NAME,PID,PCPU, to_char(TIME, 'yyyy-mm-dd hh24:mi:ss') from ORA_CPU_STATUS where trunc(TIME)=trunc(sysdate) order by PCPU desc) where rownum<=10");

Following are the 2 resultsets, whose values I want to put in 2 arrays in javascript:
 resultset12.getString(2) and resultset12.getString(4)

javascript array format that i want:
var input = [34.4, 62.5, 80.1, 70, 69.6, 69.5, 89.1, 68.4, 18, 17.3] and
categories = ['orcl1 ','orcl2 ','orcl3 ','orcl4 ','orcl5 ','orcl6 ','orcl7 ','orcl8 ','orcl9 ','orcl10 ']
4

2 に答える 2

0
<%@ page language="java" import="java.sql.*, java.io.*, java.util.Date, java.util.*,javax.servlet.*, java.text.SimpleDateFormat, java.util.Calendar " %>
<%  Class.forName("oracle.jdbc.driver.OracleDriver"); %>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>Top 10 CPU</title>
<script type="text/javascript" src="/DBdashboard/1.js"></script>
<script type="text/javascript" src="/DBdashboard/2a.js"></script>
<script type="text/javascript" src="/DBdashboard/3a.js"></script>
<script type="text/javascript" src="/DBdashboard/highcharts-more.js"></script>
<script type="text/javascript" src="/DBdashboard/json2.js"></script>
 <script>
 <%
     Connection connection=DriverManager.getConnection ("jdbc:oracle:thin:@RAC1.dinu.com:1521:orcl2","cog","cog123");
     Statement statement12 = connection.createStatement();
          ResultSet resultset12 = 
            statement12.executeQuery("select * from(select HOST_NAME,INSTANCE_NAME,PID,PCPU, to_char(TIME, 'yyyy-mm-dd hh24:mi:ss') from ORA_CPU_STATUS where trunc(TIME)=trunc(sysdate) order by PCPU desc) where rownum<=10");
 %>
$(function () {
    var chart;

<%
List<String> list = new ArrayList<String>();
List<String> list2 = new ArrayList<String>();
while(resultset12.next())
{
    String val = resultset12.getString(1);
    list.add(val);
    String val2 = resultset12.getString(2);
    list2.add(val2);

}

String csv = list2.toString().replace("[", "").replace("]", "").replace(", ", ",");

String csvWithQuote = list.toString().replace("[", "'").replace("]", "'").replace(", ", "','");


%>

   var input = JSON.parse("[" + <%out.print(csv);%> + "]"),
    data = [],
    categories = JSON.parse("[" + <%out.print(csvWithQuote);%> + "]");
    $.each(input, function(index, value){
        var color;
        if (value > 80) color = 'red';
        else if (value > 60) color = 'Orange';
        else color = 'green';
        data.push({y:value, color: color, url:'https://www.google.com'});
    });

    $(document).ready(function() {
        chart = new Highcharts.Chart({
            chart: {
                renderTo: 'COL',
                type: 'column'
            },
            title: {
                text: 'Current Top 10 CPU Consumers',
                    style: {fontSize: '10px'}
            },
            xAxis: {
                categories: JSON.parse(categories),
                labels: {
                    rotation: -35,
                    align: 'center'
                }
            },
           yAxis: {
                title: {
                    text: 'Percentage',
                    style: {fontSize: '11px'}
                }
            },
            exporting: { enabled: false },
             legend: {
                enabled: false,
            },
            tooltip: {
                formatter: function() {
                    return '<b>'+ this.x +'</b>' +'- Oracle User Process CPU Consumed :'+'<b>'+ this.y +' % ' +'</b>' ;
                }
            },
       plotOptions: {
            series: {
                cursor: 'pointer',
                point: {
                    events: {
                        click: function() {
                            location.href = this.options.url;
                        }
                    }
                }
            }
        },
                series: [{
                name: 'CPU Consumed',
                pointWidth: 28,
                data: JSON.parse(data)

            }]
        });
    });
});


        </script>
    </head>
    <body>
<div id="COL" style="min-width: 100px; height: 300px; margin: 0 auto"></div>

    </body>
</html>

私の問題は、以下に示すように、JSP 配列を javascript 変数に渡すことができないことです。

   var input = JSON.parse("[" + <%out.print(csv);%> + "]"),
    data = [],
    categories = JSON.parse("[" + <%out.print(csvWithQuote);%> + "]");

上記の JavaScript コードには、以下のような値が含まれている必要があります。その後、私のグラフのみが機能します。

   var input = [34.4, 62.5, 80.1, 70, 69.6, 69.5, 89.1, 68.4, 18, 17.3],
    data = [],
    categories = ['orcl1 ','orcl2 ','orcl3 ','orcl4 ','orcl5 ','orcl6 ','orcl7 ','orcl8 ','orcl9 ','orcl10 '];

@Chad、それについてのガイダンスが必要です。

于 2013-08-10T04:22:00.743 に答える