如何在javascript中使用结果集的值创建数组

时间:2013-08-09 13:25:16

标签: javascript arrays jsp

我想将从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 ']

2 个答案:

答案 0 :(得分:3)

@Chad,它与

一起使用
var dincpu = <%=csv%>;
var dinpcat = <%=csvWithQuote%>;

将整个代码作为参考。

<%@ 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"); %>
 <%
     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 INSTANCE_NAME,PCPU from ORA_CPU_STATUS order by PCPU desc) where rownum<=10");


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();
String csvWithQuote = list.toString().replace("[", "['").replace("]", "']").replace(", ", "','");

%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>Highcharts Example</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>
$(function () {
    var dincpu=<%=csv%>;
    var dinpcat = <%=csvWithQuote%>;
    var input = dincpu,
        data = [],
        categories =dinpcat;
        $.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'});
        });

    chart = new Highcharts.Chart({
        chart: {
            renderTo: 'COL',
            type: 'column'
        },
        title: {
            text: 'Current Top 10 CPU Consumers',
            style: {fontSize: '10px'}
        },
        xAxis: {
            categories: 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: data

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

答案 1 :(得分: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,需要你的指导。