Number of registrants on a monthly basis
Java class:
Package COM. spring. controller; import Java. io. ioexception; import Java. SQL. connection; import Java. SQL. drivermanager; import Java. SQL. preparedstatement; import Java. SQL. resultset; import Java. SQL. sqlexception; import Java. util. arraylist; import Java. util. arrays; import Java. util. date; import Java. util. hashmap; import Java. util. hashtable; import Java. util. iterator; import Java. util. list; import Java. util. map; impo RT Java. util. map. entry; import javax. servlet. HTTP. httpservletrequest; import javax. servlet. HTTP. httpservletresponse; import net. SF. JSON. jsonarray; import Org. springframework. stereotype. controller; import Org. springframework. web. BIND. servletrequestutils; import Org. springframework. web. BIND. annotation. requestmapping; import Org. springframework. web. BIND. annotation. requestmethod; import Org. springframework. we B. servlet. modelandview; @ controllerpublic class echartcontroller {/*** echart report statistics by month */@ requestmapping (value = "User/month", method = {requestmethod. post, requestmethod. get}) Public modelandview monthstatistics (httpservletrequest request, httpservletresponse response) throws ioexception {string year = servletrequestutils. getstringparameter (request, "year", "2014"); modelandview MAV = new modelandview (); Mysqldata = new mysqldata (); Map <object, Object> map = new hashmap <object, Object> (); map = mysqldata. selectmonth (year); List <integer> DATA = new arraylist <integer> (); List <string> month = new arraylist <string> (); // traverse MAP/** iterator <entry <object, Object> it = map. entryset (). iterator (); While * (it. hasnext () {** @ suppresswarnings ("rawtypes") map. entry entry = (map. entry) * It. next (); String key = (string) entry. getkey (); integer value = * integer. valueof (string) entry. getvalue (); * system. out. println ("--------" + key); data. add (value); // data * month. add (key); // month} * // traverse 2 mapfor (Object M: map. keyset () {integer value = integer. valueof (string) map. get (m); system. out. println ("--------" + M); data. add (value); // data month. add (m + "month"); // month} Mav. addobject ("time", new date (); // MAV. Addobject ("jsonmap", // jsonarray. fromobject (MAP); // convert map to a JSON array // when map is converted to JSON, the key of map must be of the string type Mav. addobject ("jsondata", jsonarray. fromobject (data); // converts a list to a JSON array Mav. addobject ("jsonmonth", jsonarray. fromobject (month); // converts a list to a JSON array Mav. setviewname ("echart/monthechart"); // return MAV ;} //////////////////////////////////////// //////////////////////////////////////// ////////////////////////// // Internal class mysqldata {private string username; private string password; private connection; private preparedstatement pS; // constructor public mysqldata () {// todo auto-generated constructor stubstring url = "JDBC: mysql: // 127.0.0.1: 3306/test? Useunicode = true & characterencoding = utf8 & zerodatetimebehavior = converttonull "; string username =" root "; string Password =" "; // load the driver to connect to the database try {class. forname ("com. mySQL. JDBC. driver "); connection = drivermanager. getconnection (URL, username, password);} // catch the exception catch (classnotfoundexception cnfex) {system. err. println ("failed to load JDBC/ODBC driver"); cnfex. printstacktrace ();} // catch the exception catch (sqlexception sqlex) {System. err. println ("unable to connect to Database"); sqlex. printstacktrace () ;}/// query method 1/** select date_format (date_added, '% Y-% m') as month, count (*) as shumu * from user where date_format (date_added, '% y') = 2014 group by month * order by month; */Public Map <object, Object> selectmonth (string year) {list <Object> maplist = new arraylist <Object> (); Map <object, Object> map = new hashmap <object, Object> (); try {PS = connectio N. preparestatement ("select date_format (ptime, '% m') as month, count (*) as shumu from cc_user" + "where date_format (ptime,' % y') =? Group by month order by month; "); PS. setstring (1, year); resultset rs = ps.exe cutequery (); While (RS. next () {map. put (RS. getint ("month"), RS. getstring ("shumu"); system. out. println ("month:" + Rs. getstring ("month") + "number:" + Rs. getstring ("shumu"); maplist. add (MAP) ;}} catch (sqlexception e) {// todo auto-generated catch blocke. printstacktrace () ;}return map ;}// internal class} // echart class
Page:
<% @ Page Language = "Java" Import = "Java. util. * "pageencoding =" UTF-8 "%> <% string Path = request. getcontextpath (); string basepath = request. getscheme () + ": //" + request. getservername () + ":" + request. getserverport () + path + "/"; %> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en"> <HTML>
Run: