How to access data of MySql database from javascript -
i developing web application in use javascript, normal servlet classes , jsp page. want javascript file data of database table. please tell me how can achieve this. below code of jsp page.
<head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1"> <title>calendar</title> <link type="text/css" rel="stylesheet" href="css/style.css"> </head> <body> <% //logindetails login = new logindetails(); username = logindetails.username; password = logindetails.password; system.out.println(username+" * "+password); %> <input type="hidden" id="hidden-user" value="<%= username%>"></br> <input type="hidden" id="hidden-pass" value="<%= password%>"></br> <div style= "float:right;"> <div id="calendar-container"> <input type="button" style="margin-left:5px; margin-top: 10px; height: 41px; width: 34px; background: url(images/left.png);" onclick="previous()";> <input type="button" value="n" style="margin-left:380px; margin-top:-40px; height: 41px; width: 34px; background: url(images/right.png);" onclick="next()";> <div id="calendar-header"> <span id = "calendar-month-year"></span><span id = "calendar-month-year"> </span> </div> <div id="calendar-dates" onclick="getcontext()";> </div> </div> </div> <form name = "field_details" action="logic" method= "post"> <input type="text" name="text-fields" id="field" style="height:50px; width:300px; border-color:black"></br> absent : <input type="checkbox" name="absent" value="absent" ></br> approved leave : <input type="checkbox" name="approved-leave" value="approved leave" ></br> sick leave : <input type="checkbox" name="sick-leave" value="sick leave" ></br> pending claim : <input type="checkbox" name="pending-claim" value="pending claim " ></br> rejected claim : <input type="checkbox" name="rejected-claim" value="rejected claim " ></br> </div> <input type="submit" value="submit"></form> <script type="text/javascript" src="script.js"></script> </body> </html>
below script.js file
var username1 = document.getelementbyid("hidden-user").value; var password1 = document.getelementbyid("hidden-pass").value; var table; var cal; var monthval = 0; var month_name; var month; var year; var imgsrc = "untitled.png"; var value = 0; var nextval; var nextyear = 0; window.onload = function(){ var d = new date(); month_name = ['january','february','march','april','may','june','july','august','september','october','november','december']; month = d.getmonth(); //0-11 monthval = month; value = 11-monthval; year = d.getfullyear(); //2014 var first_date = month_name[month] + " " + 1 + " " + year; //september 1 2014 var tmp = new date(first_date).todatestring(); //mon sep 01 2014 ... var first_day = tmp.substring(0, 3); //mon var day_name = ['sun','mon','tue','wed','thu','fri','sat']; var day_no = day_name.indexof(first_day); //1 var days = new date(year, month+1, 0).getdate(); //30 //tue sep 30 2014 ... var calendar = get_calendar(day_no, days); document.getelementbyid("calendar-month-year").innerhtml = month_name[month]+" "+year; document.getelementbyid("calendar-dates").appendchild(calendar); } function next(){ monthval++; if(monthval > 11){ monthval = 0; nextyear++; } var d = new date(); month_name = ['january','february','march','april','may','june','july','august','september','october','november','december']; month = monthval; //0-11 year = d.getfullyear()+nextyear; //2014 var first_date = month_name[month] + " " + 1 + " " + year; //september 1 2014 var tmp = new date(first_date).todatestring(); //mon sep 01 2014 ... var first_day = tmp.substring(0, 3); //mon var day_name = ['sun','mon','tue','wed','thu','fri','sat']; var day_no = day_name.indexof(first_day); //1 var days = new date(year, month+1, 0).getdate(); //30 //tue sep 30 2014 ... var calendar1 = get_calendar(day_no, days); document.getelementbyid("calendar-month-year").innerhtml = month_name[month]+" "+year; document.getelementbyid("calendar-dates").innerhtml = ""; document.getelementbyid("calendar-dates").appendchild(calendar1); } function previous(){ monthval--; if(monthval < 0){ monthval = 11; nextyear--; } var d = new date(); month_name = ['january','february','march','april','may','june','july','august','september','october','november','december']; month = monthval; //0-11 year = d.getfullyear()+nextyear; //2014 var first_date = month_name[month] + " " + 1 + " " + year; //september 1 2014 var tmp = new date(first_date).todatestring(); //mon sep 01 2014 ... var first_day = tmp.substring(0, 3); //mon var day_name = ['sun','mon','tue','wed','thu','fri','sat']; var day_no = day_name.indexof(first_day); //1 var days = new date(year, month+1, 0).getdate(); //30 //tue sep 30 2014 ... var calendar1 = get_calendar(day_no, days); document.getelementbyid("calendar-month-year").innerhtml = month_name[month]+" "+year; document.getelementbyid("calendar-dates").innerhtml = ""; document.getelementbyid("calendar-dates").appendchild(calendar1); } function get_calendar(day_no, days){ table = document.createelement('table'); var tr = document.createelement('tr'); //row day letters for(var c=0; c<=6; c++){ var td = document.createelement('td'); td.innerhtml = "smtwtfs"[c]; tr.appendchild(td); } table.appendchild(tr); //create 2nd row tr = document.createelement('tr'); var c; for(c=0; c<=6; c++){ if(c == day_no){ break; } var td = document.createelement('td'); td.innerhtml = ""; tr.appendchild(td); } var count = 1; for(; c<=6; c++){ var td = document.createelement('td'); td.innerhtml = count; count++; tr.appendchild(td); } table.appendchild(tr); //rest of date rows for(var r=3; r<=7; r++){ tr = document.createelement('tr'); for(var c=0; c<=6; c++){ if(count > days){ table.appendchild(tr); return table; } var td = document.createelement('td'); td.innerhtml = count; count++; tr.appendchild(td); } table.appendchild(tr); } return table; } function getcontext(){ if (table != null) { (var = 0; < table.rows.length; i++) { (var j = 0; j < table.rows[i].cells.length; j++) table.rows[i].cells[j].onclick = function () { tabletext(this); }; } } function tabletext(tablecell) { //alert(tablecell.innerhtml); tablecell.setattribute('style', 'background-color: #fff'); var index = tablecell.cellindex; var day_name1 = ['sun','mon','tue','wed','thu','fri','sat']; var pressday = day_name1[index]; var data = tablecell.innerhtml; document.getelementbyid("field").value = pressday + ", " +data+"- "+ month_name[month]+"- "+year; } return fields; }
you should use search before asking query. have query: can javascript connect mysql?
it not normal way, connecting browser , server side database. can use 1 of new approaches:
http://www.clusterdb.com/mysql/mysql-cluster-with-node-js
https://github.com/sidorares/nodejs-mysql-native
Comments
Post a Comment