How to get data between two dates? - mysql

I am having a date picker issue in a jsp file.
When i click on hyperlink in Allmeters.jsp file,it will open the meteridinfo.jsp. In this file i have one label called meterid. I get this meterid value from allmeters.jsp and also i have two fields called fromdate and todate. By using these dates i am getting data from mysql table but the problem is when i choose fromdate as 2012-05-1 and todate as 2012-05-11 it is not retreiving the data but when i change todate as 2012-05-31 then it is displaying data from mysql table,it means when i select the lastdate in a month as todate then only it is coming.Please help me.
The codings of AllMeters.jsp is given below:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%#page import="java.sql.*"%>
<html>
<head>
<title>All Meter's</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style>
td{FONT: 13px Arial,sans-serif;}
th{FONT:bold 13px Arial,sans-serif;color:white;background-color:#3366ff}
input{FONT: 13px Arial,sans-serif;border: solid 1;}
select{font: 13px Arial,sans-serif}
#button{cursor:hand}
A:hover {COLOR: red}</style>
<script type="text/javascript">
window.history.forward();
function noback() { window.history.forward(); }
</script>
</head>
<body>
<table border=1 style="" width="100%" align="center" cellpadding="5" cellspacing="0">
<tr>
<th>S No</th>
<th>Meter ID</th>
<th>Consumer ID</th>
<th>Consumer Name</th>
<th>Reading</th>
<th> Date</th>
<th>Time</th>
<th>Status</th>
<th>Sub Zone</th>
<th>Zone</th></tr>
<%
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/suwatermeter","root","sumith");
Statement st=con.createStatement();
int mid;
int SNo=1;
ResultSet rs=st.executeQuery("select * from Meter_List");
while(rs.next()){
%>
<tr><td align="center" bgcolor="#ededed"><%=SNo%></td><td align="center" bgcolor="#ededed"><%=rs.getInt("Meterid")%></td><td align="center" bgcolor="#ededed"><%=rs.getInt("Consumerid")%></td><td bgcolor="#ededed"><%=rs.getString("Consumername")%></td><td align="center" bgcolor="#ededed"><%=rs.getInt("LastReading")%></td><td align="center" bgcolor="#ededed"><%=rs.getDate("Date")%></td><td align="center" bgcolor="#ededed"><%=rs.getTime("Time")%></td><td align="center" bgcolor="#ededed"><%=rs.getString("Status")%></td><td align="center" bgcolor="#ededed"><%=rs.getString("Subzone")%></td><td align="center" bgcolor="#ededed"><%=rs.getString("zone")%></td></tr>
<%
SNo++;
}
rs.close();
st.close();
con.close();
}
catch(Exception e){}
%>
</tbody>
<form action="" method="POST" name="frm_pages">
<table style="width: 100%;" align="center" border="0">
</table>
</body>
</html>
code of file meteridinfo.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%#page import="java.sql.*"%>
<%#page import="java.util.Date"%>
<%#page import="java.text.SimpleDateFormat"%>
<%
Connection con;
String sqlQuery="";
Statement st;
ResultSet rs=null;
%>
<html>
<head>
<title>Meterid Information
</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" type="text/css" href="style.css" />
<style>
td{FONT: 13px Arial,sans-serif;}
th{FONT:bold 13px Arial,sans-serif;color:white;background-color:#3366ff}
input{FONT: 13px Arial,sans-serif;border: solid 1;}
select{font: 13px Arial,sans-serif}
#button{cursor:hand}
A:hover {COLOR: red}</style>
<link rel="stylesheet" type="text/css" media="all" href="jsDatePick_ltr.min.css" />
<script type="text/javascript" src="jquery.1.4.2.js"></script>
<script type="text/javascript" src="jsDatePick.jquery.min.1.3.js"></script>
<script type="text/javascript">
function calendarSetup() {pick("inputField1");pick("inputField2");} //initialize calendar on each date input field
function pick(inputField){new JsDatePick({useMode:2,target:inputField,dateFormat:"%d-%m-%Y"});} //display calendar for a given date input field
</script>
</head>
<body onload=calendarSetup()>
<body onload=calendarSetup()>
<form action="./meteridinfon.jsp" method="get" name="search">
<table width="500px;"align="center" border="0" cellpadding="4" cellspacing="3" bgcolor="#cccccc" bordercolor="#000000">
<tbody><tr>
<th colspan="3" align="middle"><strong>Meter ID Readings</strong></th></tr>
<tr><td>
<input type=hidden name="mid" value="<%=request.getParameter("mid")%>">
<label name="name" value="<%=request.getParameter("mid")%>"><font size="4">Meter ID : <%=request.getParameter("mid")%></font></label></td>
</tr>
<tr><td class="options1" colspan="3">
<font size="4">From</font>
<input type="text" size="20" maxlength="10" id="inputField1" name="fromDate">
<font size="4">To</font>
<input type="text" size="20" id="inputField2" name="toDate">
</font>
</td></tr>
<tr><td style="height: 30px;" colspan="4" align="center">
<input name="Search" value="Search" style= "HEIGHT:30px" style="width: 90px; border: 1px solid rgb(13, 31, 78);" type="submit">
</td></tr>
<%
try
{
int mid=Integer.parseInt(request.getParameter("mid"));
out.println("mid ===> +"+mid);
String fromDate=request.getParameter("fromDate");
String toDate=request.getParameter("toDate");
out.println("fromDate ===> +"+fromDate);
out.println("toDate ===> +"+toDate);
String query="select LastReading,Date,Time from Meter_List where Date between str_to_date('"+fromDate+"', '%Y-%m-%d') and str_to_date('"+toDate+"', '%Y-%m-%d') and Meterid="+mid;
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/suwatermeter","root","sumith");
st=con.createStatement();
if(fromDate !=null){
rs=st.executeQuery(query);
}
//ResultSet rs=st.executeQuery("select LastReading,Date,Time from Meter_List where Meterid="+meterid);
while(rs.next()){
%>
<table width="65%" align="center">
<tr bgcolor="lightblue">
<td>Reading</td>
<td bgcolor="lightblue"><%=rs.getInt("LastReading")%></td>
</tr>
<tr bgcolor="lightblue">
<td>Installation Date</td>
<td bgcolor="lightblue"><%=rs.getDate("Date")%></td>
</tr>
<tr bgcolor="lightblue">
<td>Inastallation Time</td>
<td bgcolor="lightblue"><%=rs.getTime("Time")%></td>
</tr>
<%
}
rs.close();
st.close();
con.close();
}
catch(Exception e){
e.printStackTrace();
}
%>
</table>
</tbody>
</table>
</div>
</body>
</html>
In mysql table i have the date values as follows:
2012-05-10,
2012-03-04,
2012-03-04,
2012-09-12,
2012-09-12,
2012-09-12,
2012-09-12.

SELECT
*
FROM my_table
WHERE date(date_column1) >= DATE(NOW())
AND date(date_column2) <= DATE(NOW())

you can use
select * from my_table where date_coumn between '2012-05-01' AND '2012-05-11'

select LastReading,Date,Time from Meter_List
where str_to_date(date, '%Y-%m-%d')
between str_to_date('"+fromDate+"', '%Y-%m-%d') and
str_to_date('"+toDate+"', '%Y-%m-%d') and Meterid="+mid;

Related

data from selected rows to servlet

This code is written in jsp.it is a dynamic table with rows and columns .When clicked on the submit button,only the selected rows and the data in that row should be fetched into the servlet.
``<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%# page import="java.io.*,java.util.*,java.sql.*" %>
<%# page import="javax.servlet.http.*,javax.servlet.*" %>
<%# taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Saravana Bhavan</title>
<link rel="stylesheet" href="css/lavish-bootstrap.css"/>
<link rel="stylesheet" href="css/font-awesome.css"/>
</head>
<body>
<form name="f1" method="post" action="Insertcheck.jsp">
<sql:setDataSource var="snapshot"
driver="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:#localhost:1521:xe"
user="system" password="pswd"
/>
<sql:query dataSource="${snapshot}" var="result">
select * from vendormenu
</sql:query>
<center><h3>Saravana Bhavan</h3>
<h4 class="text-muted">Menu</h4></center>
<div class="panel">
<table class="table table-bordered table-responsive" border="2" width="100%">
<tr>
<td>ITEM CODE</td>
<td>ITEM NAME</td>
<td>RATE</td>
<td></td>
</tr>
<c:forEach var="row" items="${result.rows}">
<center>
<tr>
<td><c:out value="${row.mid }"/>
</td>
<td><c:out value="${row.mname }"/>
</td>
<td><c:out value="${row.cost }"/></td>
<td><div class="checkbox">
<label><input type="checkbox" name="yes"></label></div></td>
</tr>
</center>
</c:forEach>
</table>
<br><br>
<center><input type="submit" /></center>
</div>
</form>
</body>
</html>
I need to print the values in each row selected using checkboxes by the user into the servlet from jsp.The rows are added dynamically. How to retrieve all the values from the rows selected?
Suggesting that ${row.mid} is unique, add value attribute to your checkbox:
<input type="checkbox" name="yes" value="${row.mid}" />
And then, in the servlet/JSP processing the form (btw. I do not see any <form> tag in your page!), use
String[] selectedMIDs = request.getParameterValues('yes');
to get an array of selected mids.

Take ids of text boxes in an html growing table

I have made code for auto generated text boxes in an HTML growing table. I want to save values from these text boxes to a mysql database using netbeans but I don't know how I get the ids of each text box while they are auto generating..
My code is here..please help me if there is any solution for this problem..
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="generator" content="CoffeeCup HTML Editor (www.coffeecup.com)">
<meta name="dcterms.created" content="Tue, 24 Mar 2015 06:41:28 GMT">
<meta name="description" content="">
<meta name="keywords" content="">
<title></title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
var rowCount = 1;
function addMoreRows(frm) {
rowCount ++;
var recRow = '<p id="rowCount'+rowCount+'"><tr><td><input name="name" type="text" size="17%" maxlength="120" /></td><td><input name="email" type="text" maxlength="120" style="margin: 4px 5px 0 5px;"/></td><td><input name="mobile" type="text" maxlength="120" style="margin: 4px 10px 0 0px;"/></td></tr> <img src="./image/close.png" /></p>';
jQuery('#addedRows').append(recRow);
}
function removeRow(removeNum) {
jQuery('#rowCount'+removeNum).remove();
}
</script>
</head>
<body>
<table rules="all" style="background:#fff;">
<tr>
<td style="font-size:14px;" >Name</td>
<td style="font-size:14px;">Email</td>
<td style="font-size:14px;">Mobile</td>
<td><span style="font:normal 12px agency, arial; color:blue; text-decoration:underline; cursor:pointer;" onclick="addMoreRows(this.form);">
<img style=" padding-left: 12px;" src="./image/add.png" />
</span>
</td>
</tr>
<form action="" method="POST">
<tr id="rowId">
<td><input type="text" name="name" size="17%"/></td>
<td><input type="text" name="email" /></td>
<td><input type="text" name="mobile" /></td>
</form>
</table>
<div id="addedRows"></div>
</td>
</tr>
<td><input type="submit"></td>
</table>
</body>
</html>

pass a parameter into named query

I have the following query in my model(Supplier) class :
#NamedQuery(name = "Supplier.findSupplierKeyId", query = "SELECT s FROM Supplier s WHERE s.supplierid LIKE (':supplieridkey%')")
I have following function in my Controller(SupplierSerivce) class:
public List<Supplier> findSupplierKeyId(String supplierkeyid){
List<Supplier> supplierList = mgr.createNamedQuery("Supplier.findSupplierKeyId").setParameter("supplieridkey", supplierkeyid).getResultList();
return supplierList;
}
I want to get in a html textfield :
<form action="SearchSupplierIdKey.jsp" method="POST">
<div>
<input type="text" name="supIdKey"/>
<input type="submit" value="Search" name="button"/>
</div>
</form>
then get parameter from the textfield and pass it into supService.findSupplierKeyId through servlet:
public class SearchSupplierIdKey extends HttpServlet {
#PersistenceContext
EntityManager em;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
SupplierService supService = new SupplierService(em);
HttpSession session = request.getSession();
String supId = (String) session.getAttribute("supId");
String button = (String) session.getAttribute("button");
List<Supplier> supplierListResult = supService.findSupplierKeyId(supId);
session.setAttribute("supplierListResult", supplierListResult);
if (button.equals("Search")) {
response.sendRedirect("ViewSupplierByIdKey.jsp");
}
} catch (Exception ex) {
Logger.getLogger(AddSupplier.class.getName()).log(Level.SEVERE, null, ex);
}
}
Then show the result in ViewSupplierByIdKey.jsp :
<%#page import="java.util.List"%>
<%#page import="model.Supplier"%>
<!-- retrieve session object, itemList -->
<%
List<Supplier> supplierListResult = (List)session.getAttribute("supplierListResult");
%>
<html>
<head>
<title>Supplier Search Result</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<table cellspacing="0" cellpadding="0" style="margin-left:auto; margin-right:auto; border:solid; width: 1000px">
<tr style="border:solid">
<td style="border:solid ">
<h3 style="text-align: center">ABC Health Supplement Shop System</h3>
</td>
</tr>
<tr style="border: solid">
<td style="border: solid">
<center><h1><i><b><font face="Segoe Script" color="#FF0000">Supplier Search Result(By ID Key)</font></b></i></h1></center>
</td>
</tr>
<tr style="border:solid">
<td style="border:solid">
<center><div>
<table border="1">
<tr>
<th>Supplier ID</th>
<th>Supplier Name</th>
<th>Manufacturer</th>
<th>Contact Num</th>
<th>Address</th>
</tr>
<% for (Supplier supplier: supplierListResult){ %>
<tr>
<td><%= supplier.getSupplierid() %></td>
<td><%= supplier.getSuppliername()%> </td>
<td><%= supplier.getManufacturer()%> </td>
<td><%= supplier.getContactnum()%> </td>
<td><%= supplier.getAddress()%> </td>
</tr>
<% } %>
</table>
<br><br>
<p>Back to Menu page</p>
</div>
</center>
</td>
</tr>
</table>
</body>
</html>
but i dont knw why i cant proceed to ViewSupplierByIdKey.jsp, it stuck at the controller class
(SearchSupplierIdKey.java). Please Help :( :(
One thing that I notice is the request parameter is retrieved using the name supId:
String supId = (String) session.getAttribute("supId");
but specified as supIdKey in the HTML:
<input type="text" name="supIdKey"/>
The name attribute used on the input should match the key being used to retrieve the attribute.

HTML table settings not working

I'm trying to tamper with my html table settings to set borders but I think my syntax is wrong. I've looked around online and tried multiple things with no success. Any suggestions?
For example, this code is applied to the first table but does not appear correctly on page.
<table BORDER="30" CELLPADDING="10" CELLSPACING="3" BORDERCOLOR="00FF00" width='80%' style="margin: 0 auto;">
</table>
HTML code is applied here: http://macrorevolution.com/calculators/bmr/
The only table settings that seem to work are "width='80%' style="margin: 0 auto;""
<?php
$answer = "";
$agev = "";
$feetv = "";
$inchesv = "";
$weightv = "";
$sex = "";
if(isset($_POST['agev']) && isset($_POST['feetv']) && isset($_POST['inchesv']) && isset($_POST['weightv']) && isset($_POST['sex'])) {
$agev = $_POST['agev'];
$feetv = $_POST['feetv'];
$inchesv = $_POST['inchesv'];
$weightv = $_POST['weightv'];
$sex = $_POST['sex'];
$totalheightv = $inchesv + ($feetv*12);
$heightcm = $totalheightv*2.54;
$weightkg = $weightv/2.2;
if($sex=='male') $answer = 66.47 + (13.75*$weightkg) + (5*$heightcm) - (6.75*$agev);
if($sex=='female') $answer = 665.09 + (9.56*$weightkg) + (1.84*$heightcm) - (4.67*$agev);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Basal Metabolic Rate Calculator</title>
</head>
<body>
<div class="box pt20">
<table BORDER="30" CELLPADDING="10" CELLSPACING="3" BORDERCOLOR="00FF00" width='80%' style="margin: 0 auto;">
<td colspan="4">
<h4 style="background: #99FF99;">
<strong>BMR = Basal Metabolic Rate</strong> (similar to RMR = Resting Metabolic Rate). Your BMR represents the number of calories your body burns at rest. Regular routine of cardiovascular exercise can increase your BMR, improving your health and fitness when your body's ability to burn energy gradually slows down.
</h4>
</td>
</table>
<form method='post' action=''>
<table border='5' width='80%' font-family:Georgia; font-size:1; class="table" style="margin: 0 auto;" bgcolor="FFFFFF">
<tr class="calcheading">
<td colspan="2"><font size="10">MacroRevolution BMR Calculator</font></td>
</tr>
<tr class="calcrow">
<td>Age:</td>
<td><input type='text' name='agev' value="<?php echo $agev; ?>"/>Years</td>
</tr>
<tr class="calcrow2">
<td>Height:</td>
<td align="justify"><input type='text' name='feetv' value="<?php echo $feetv; ?>"/>Ft<input type='text' name='inchesv' value="<?php echo $inchesv; ?>"/>In</td>
</tr>
<tr class="calcrow">
<td>Weight:</td>
<td align="left"><input type='text' name='weightv' value="<?php echo $weightv; ?>"/>lbs</td>
</tr>
<tr class="gender">
<td colspan="2"><input type='radio' name='sex' value='male'>Male
<input type='radio' name='sex' value='female'>Female</td>
</tr>
<tr class="submit">
<td colspan="2"><input type='submit' class="button highlight small" value='Calculate'/></td>
</tr>
<tr class="calcrow">
<td colspan="2">Your BMR is <span style="background-color: #00CC33"><?php echo $answer?></span></td>
</tr>
</table>
</form>
<table border='0' width='80%' class="table" align="center" style="margin: 0 auto;">
<td colspan="4">
<h2 style="background: #99FF66;">Formula for BMR</h2>
<h4 style="background: #99FF66;">
If you want to manually calculate your BMR, use the (Harris-Benedict formula) <br> below. <br><br>
Men: BMR=66.47+ (13.75 x W) + (5.0 x H) - (6.75 x A) <br>
Women: BMR=665.09 + (9.56 x W) + (1.84 x H) - (4.67 x A) <br><br>
W = Weight in kilograms (lbs/2.2)<br>
H = Height in centimeters (inches x 2.54)<br>
A = Age in years
</h4>
</td>
</table>
</div>
</body>
</html>
<!-- ///////////////////////////////////////////////////////////////////////////////////////////-->
Avoid HTML attributes like border="30" cellpadding="10" cellspacing="3" bordercolor="#00ff00" and use CSS instead:
<style>
table {
border: 30px solid #00ff00;
}
</style>
Even better, move the styles to another file
<link rel="stylesheet" type="text/css" href="stylesheet.css">

load() a page that contains ckeditor in jquery

i have a problem with load() a page that contains ckeditor.and the problem is that when im loading my page , after load the page does not contains ckeditor anymore.my code is here:
i have a problem with load() a page that contains ckeditor.and the problem is that when im loading my page , after load the page does not contains ckeditor anymore.my code is here:
$("#editpost").click(function(){
$("#postbodycenter").load("editepost.php");
});
<?php
include 'php/auth.inc';
include 'manage.php';
if(!empty($_POST["title"]) && !empty($_POST["txfpost"]))
{
connect();
if(isset($_POST["select_page_name"]))
{
$p_name = mysql_real_escape_string($_POST['select_page_name']);
$query = 'SELECT `id` FROM `pages` WHERE `page_name` ='.'"$p_id"'.'';
$get_p_id = mysql_query($query) or die(mysql_error());
while ($result = mysql_fetch_array($get_p_id))
{
$p_id = $result[id];
$query ="insert into post (page_id,header,category,date,author,body,more) values
($p_id,'$_POST[title]','$_POST[txfcat]','$_POST[date]','admin','$_POST[txfpost]','$_POST[txfmore]')";
mysql_query($query) or die(mysql_error());
}
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf8">
<title>new post</title>
<link rel="stylesheet" href="cssadmin.css" type="text/css" />
<script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="ckeditor/ckeditor.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#btnsub").click(function(){
var cat = $("#txfcat").val();
var title = $("#title").val();
var editor1 = $("#editor1").val();
var editor2 = $("#editor").val();
if(!(cat == "" || title == "" || editor1 =="" || editor2 == "")){
alert("Fill all fields!");
}else{
$('#form1').submit();
alert("done");
}
});
});
</script>
</head>
<body id ="body">
<center>
<form id="form1" name="form1" method="post" action="newpost.php">
<table id = "tb_post">
<tbody>
<tr>
<td>cat</td>
<td><input type="text" name="txfcat" id="txfcat" size="130" /></td>
</tr>
<tr>
<td>title:</td>
<td><input type="text" name="title" id="title" size="130" /></td>
</tr>
<tr>
<td>refers to :</td>
<td>
<select name="select_page_name">
<option>select the page</option>
<option value="home">home</option>
<?php pages();?>
</select>
</td>
</tr>
<tr>
<td><input type = "hidden" name = "date" value="<?php echo date("H:i:s d-m-Y")?> "/></td>
<td><textarea class="ckeditor" cols="95" id="editor1" name="txfpost" rows="10"></textarea>
</td>
</tr>
<tr>
<td>more</td>
<td><textarea class="ckeditor" cols="95" id="editor" name="txfmore" rows="10"></textarea>
</td>
</tr>
<tr>
<td></td>
<td> <button type="submit" name="btnsub" id="btnsub" value="update">send</button> </td>
</tr>
</tbody>
</table>
</form>
</center>
</body>
</html>
Your page does not contain the script to load CKEditor.
Simply add the following code into one of your <script> tags in your page:
$(document).ready(function(){
CKEDITOR.replace('editor1');
}