i trying retrieve value database using java file , storing hashmap. please find below code (sample.java):
import java.sql.*; import java.util.hashmap; public class sample { static connection conn; static preparedstatement stmt; static resultset rs; string sql; static string project="project1"; public static hashmap< string, string> map = new hashmap< string, string>(); public static void main(string[] args) { try{ class.forname("com.mysql.jdbc.driver"); conn=drivermanager.getconnection("jdbc:mysql://localhost:3309/graphvalue","root","root"); stmt=conn.preparestatement("select * testcase projectname= ?"); stmt.setstring(1,project); rs=stmt.executequery(); while(rs.next()) { system.out.println(rs.getstring(1)+" "+rs.getint(2)+" "+rs.getint(3)+" "+rs.getint(4)+" "+rs.getint(5)); map.put("projectname", rs.getstring(1)); map.put("total testcase", string.valueof(rs.getint(2))); map.put("testcase executed", string.valueof(rs.getint(3))); map.put("failed testcase", string.valueof(rs.getint(4))); map.put("testcase not executed", string.valueof(rs.getint(5))); system.out.println("projectname "+map.get("projectname")); } conn.close(); } catch(exception e) { system.out.println(e);} } }
please find below data retrieving databse:
projectname totaltestcase testcaseexecuted testcasefailed testcasenotexecuted project1 50 30 8 20
i want pass value javascript , able draw chart using these values. please find html/javascript code below (test.html):
<html> <head> </head> <body> <select id="charttype" name="charttype" onchange="drawchart()"> <option value = "piechart">select chart type <option value="piechart">piechart <option value="histogram">histogram <option value="linechart">linechart <option value="barchart">barchart </select> <div id="chart_div" style="border: solid 2px #000000;"></div> <p id="demo"></p> <p id="demo1"></p> <script type="text/javascript" src="https://www.google.com/jsapi"></script> <script type="text/javascript"> var row = []; var temp; var stri; google.load('visualization', '1.0', {'packages':['corechart']}); google.setonloadcallback(getvalues); function getvalues() { var xmlhttp = new xmlhttprequest(); xmlhttp.onreadystatechange = function() { if (xmlhttp.readystate == 4 && xmlhttp.status == 200) { stri = xmlhttp.responsetext; drawchart(); } }; xmlhttp.open("get", "sample.java", true); xmlhttp.send(); } function drawchart() { var data = new google.visualization.datatable(); str = stri.split(","); // how call value java file able draw below graph passing value. data.addrows(row); var = document.getelementbyid("charttype").value; document.getelementbyid("demo1").innerhtml = "you selected: " + a; var options = {'title':'how pizza ate last night', 'width':400, 'height':300 }; var chart = new google.visualization[document.getelementbyid("charttype").value](document.getelementbyid('chart_div')); chart.draw(data, options); } </script> </body> </html>
please let me know how proceed or if have other example. please share me. thank you
you can convert map
json
. instead of helloworld class, can convert service returns `jsonimport java.sql.*; import java.util.hashmap; public class sample { static connection conn; static preparedstatement stmt; static resultset rs; string sql; static string project = "project1"; public static hashmap < string, string > map = new hashmap < string, string > (); //notice how main class converted service public static string getprojects() { try { class.forname("com.mysql.jdbc.driver"); conn = drivermanager.getconnection("jdbc:mysql://localhost:3309/graphvalue", "root", "root"); stmt = conn.preparestatement("select * testcase projectname= ?"); stmt.setstring(1, project); rs = stmt.executequery(); while (rs.next()) { system.out.println(rs.getstring(1) + " " + rs.getint(2) + " " + rs.getint(3) + " " + rs.getint(4) + " " + rs.getint(5)); map.put("projectname", rs.getstring(1)); map.put("total testcase", string.valueof(rs.getint(2))); map.put("testcase executed", string.valueof(rs.getint(3))); map.put("failed testcase", string.valueof(rs.getint(4))); map.put("testcase not executed", string.valueof(rs.getint(5))); system.out.println("projectname " + map.get("projectname")); /*______________ new code ______________*/ jsonobject resultmap = new jsonobject(map); return resultmap.tostring(); } } catch (exception e) { system.out.println(e); } { conn.close(); } return ""; } }
now convert
test.html
test.jsp
, call service we've created in previous step , output resultantjson
javascript variable.test.jsp
<%@page import="com.path.to.sample"%> <html> <head> <script> <!-- call service , output json javascript variable --> var resultantjson = <%= sample.getprojects() %> <!-- that's left parse json --> var projects = json.parse(resultantjson); </script> </head> <body> ... ... </body> </html>
now results fetched database in
projects
variable intest.jsp
. can use them conventionaljavascript
object in jsp file.
Comments
Post a Comment