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.
Related
I am trying to add a search function for every table in my page using jquery and ejs format. It is working when I use 2 same function with different ID. But it seems pointless and waste of time if I repeat the whole function for every table in the page. What do I need to do for this?
This is the code for search function.
$(document).ready(function(){
$('#search').keyup(function(){
search_table($(this).val());
});
function search_table(value){
$('#table_body tr').each(function(){
var found = 'false';
$(this).each(function(){
if($(this).text().toLowerCase().indexOf(value.toLowerCase()) >= 0)
{
found = 'true';
}
});
if(found == 'true')
{
$(this).show();
}
else
{
$(this).hide();
}
});
}
});
And this is the ejs code
<div class="dropdown">
<button class="button button1">Add</button>
<button class="button button2">Edit</button>
<form action="" class="search-form">
<input type="search" id="search" name="search" class="search-input" />
<i class="fa fa-search"></i>
</form>
</div>
<br />
<table class="table table-sortable" id="pagination">
<thead>
<tr>
<th>Name</th>
<th>Category</th>
<th>Description</th>
<th>Date</th>
</tr>
</thead>
<tbody id="table_body">
<% for(var i=0; i < data.employeeProfile.length; i++) { %>
<tr>
<td>
<%= data.employeeProfile[i].name %>
</td>
<td>
<%= data.employeeProfile[i].category %>
</td>
<td>
<%= data.employeeProfile[i].description %>
</td>
<td>
<%= data.employeeProfile[i].date %>
</td>
</tr>
<% } %>
</tbody>
</table>
Yes, you're right, it should be a reusable code. It's known as DRY ( Do not Repeat Yourself )
So one way of doing this is to create a function which handles the search functionality
Example :
function setSearch(inputEle, rowEle){
$(inputEle).keyup(function(){
search_table(rowEle, $(this).val());
});
}
function search_table(rowEle, value){
$(rowEle).each(function(){
var found = 'false';
$(this).each(function(){
if($(this).text().toLowerCase().indexOf(value.toLowerCase()) >= 0)
{
found = 'true';
}
});
if(found == 'true')
{
$(this).show();
}
else
{
$(this).hide();
}
});
}
Now you can set search functionality by calling setSearch for all the tables like :
setSearch('#search', '#table_body tr');
So this is the EJS code
<div class="topnav">
<img class="logo-img" src="img/cyberview.png" alt="AVATAR" />
Home
<a class="active" href="http://localhost:3000/employee">Employee</a>
Attendance
Benefit
Calendar
Medical Portal
Contracts
Feedback
Rewards
Log Out
</div>
<div class="grid-container">
<div>
<h1>Performance</h1>
<div class="dropdown">
<button class="button button1">Add</button>
<button class="button button2">Edit</button>
<form action="" class="search-form">
<input type="search" id="search" class="search-input" />
<i class="fa fa-search"></i>
</form>
</div>
<br />
<table class="table table-sortable" id="pagination">
<thead>
<tr>
<th>Name</th>
<th>Category</th>
<th>Description</th>
<th>Date</th>
</tr>
</thead>
<tbody id="table_body">
<% for(var i=0; i < data.employeeProfile.length; i++) { %>
<tr>
<td>
<%= data.employeeProfile[i].name %>
</td>
<td>
<%= data.employeeProfile[i].category %>
</td>
<td>
<%= data.employeeProfile[i].description %>
</td>
<td>
<%= data.employeeProfile[i].date %>
</td>
</tr>
<% } %>
</tbody>
</table>
</div>
</div>
<div class="grid-container">
<div>
<h1>Personal Information</h1>
<div class="dropdown">
<button class="button button1">Add</button>
<button class="button button2">Edit</button>
<form action="" class="search-form">
<input type="search" id="search" class="search-input" />
<i class="fa fa-search"></i>
</form>
</div>
<br />
<table class="table table-sortable" id="pagination2">
<thead>
<tr>
<th>Name</th>
<th>Phone</th>
<th>Email</th>
<th>Emergency Contact</th>
<th>Joined Date</th>
</tr>
</thead>
<tbody id="table_body">
<% for(var i=0; i < data.employeeResponse.length; i++) { %>
<tr>
<td>
<%= data.employeeResponse[i].name %>
</td>
<td>
<%= data.employeeResponse[i].contact %>
</td>
<td>
<%= data.employeeResponse[i].email %>
</td>
<td>
<%= data.employeeResponse[i].emergencyContact %>
</td>
<td>
<%= data.employeeResponse[i].joinedDate %>
</td>
</tr>
<% } %>
</tbody>
</table>
</div>
</div>
And this is the code you have provided
function setSearch(inputEle, rowEle){
$(inputEle).keyup(function(){
search_table(rowEle, $(this).val());
});
}
function search_table(rowEle, value){
$(rowEle).each(function(){
var found = 'false';
$(this).each(function(){
if($(this).text().toLowerCase().indexOf(value.toLowerCase()) >= 0)
{
found = 'true';
}
});
if(found == 'true')
{
$(this).show();
}
else
{
$(this).hide();
}
});
}
setSearch('#search', '#table_body tr');
Whenever I run the program, it works perfectly. However, as soon as I hit the "Login" button it tells me
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /LoginController/Authorize
I checked everything and the spelling is correct. I am new to this and cannot figure out what I am doing wrong. Any guidance would be highly appreciated it.
Inside of the App_Start folder I have my RouteConfig.cs file. It contains the following:
namespace CoffeeShop_Web_App
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "LoginController", action = "Index", id = UrlParameter.Optional }
);
}
}
}
I have one controller which is my LoginController.cs which contains the following.
namespace CoffeeShop_Web_App.Controllers
{
public class LoginController : Controller
{
// GET: Login
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Authorize()
{
return View();
}
}
}
Lastly, my only view Index.cshtml which contains the following.
#model CoffeeShop_Web_App.Models.OwnerLogin
#{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Login</title>
<style>
#login-div {
position: absolute;
left: 40%;
top: 40%;
border: 1px solid #ccc;
padding: 10px 10px;
}
</style>
</head>
<body>
<div id="login-div">
#using (Html.BeginForm("Authorize", "LoginController", FormMethod.Post))
{
<table>
<tr>
<td></td>
<td style="text-decoration:underline">Coffee Shop</td>
</tr>
<tr>
<td>
#Html.LabelFor(model => model.USERNAME)
</td>
<td>
#Html.EditorFor(model => model.USERNAME)
</td>
</tr>
<tr>
<td></td>
<td>#Html.ValidationMessageFor(model => model.USERNAME)</td>
</tr>
<tr>
<td>
#Html.LabelFor(model => model.PASSWORD)
</td>
<td>
#Html.EditorFor(model => model.PASSWORD)
</td>
</tr>
<tr>
<td></td>
<td>#Html.ValidationMessageFor(model => model.PASSWORD)</td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" name="name" value="Login" />
<input type="reset" name="name" value="Clear" />
</td>
</tr>
</table>
}
</div>
<script src="~/Scripts/jquery-3.4.1.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
</body>
</html>
Your form is posting back to the Authorize method of LoginController controller:
#using (Html.BeginForm("Authorize", "LoginController", FormMethod.Post))
You don't have to specify controller suffix for the name of the controller. The following should fix it:
#using (Html.BeginForm("Authorize", "Login", FormMethod.Post))
Also you did the same mistake when setting up the routing too:
defaults: new { controller = "LoginController", action = "Index",
id = UrlParameter.Optional }
which should have been just:
defaults: new { controller = "Login", action = "Index",
id = UrlParameter.Optional }
My database: MYSQL SCREENSHOT
I have inserted the values from HTML to the MySQL database. How can I use the insert command for autoincrement column?
Servlet code:
import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.sql.*;
#WebServlet(name = "Register")
public class Register extends HttpServlet {
static String username="root";
static String password="root";
static String dburl= "jdbc:mysql://localhost:3306/conferencesystem";
static String mydriver = "com.mysql.cj.jdbc.Driver";
protected void doPost(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String Firstname = request.getParameter("firstname");
String Lastname = request.getParameter("lastname");
int Mobilenumber =
Integer.parseInt(request.getParameter("mobilenumber"));
String Email = request.getParameter("emailid");
String Pass = request.getParameter("password");
out.println("<html>");
out.println("<body>");
out.println("<h3>Guest Details </h3>");
out.println("" + Firstname);
out.println("" + Lastname);
out.println("" + Email);
out.println("</body>");
out.println("</html>");
int id = 1101;
Connection con = null;
try {
Class.forName(mydriver);
con = DriverManager.getConnection(dburl, username, password);
String query = "INSERT INTO author values(?,?,?,?,?,?)";
PreparedStatement stmt = con.prepareStatement(query);
stmt.setInt(1,id);
stmt.setString(2,Firstname);
stmt.setString(3,Lastname);
stmt.setInt(4,Mobilenumber);
stmt.setString(5,Email);
stmt.setString(6,Pass);
out.println("Your Record has been successfully inserted");
int res= stmt.executeUpdate();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
h3{
font-family: Calibri;
font-size: 25pt;
font-style: normal;
font-weight: bold;
color:SlateBlue;
text-align: center;
text-decoration: underline
}
table{
font-family: Calibri;
color:white;
font-size: 11pt;
font-style: normal;
font-weight: bold;
text-align:center;
background-color: SlateBlue;
border-collapse: collapse;
border: 2px solid navy
}
table.inner{
border: 0px
}
<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<html>
<head>
<title> Author Registration Form </title>
</head>
<link rel="stylesheet" href="styles.css">
<body>
<form method="post" action="http://localhost:8080/Register">
<h3>AUTHOR REGISTRATION FORM</h3>
<table align="center" cellpadding = "10">
<!----- First Name ---------------------------------------------------------->
<tr>
<td>FIRST NAME</td>
<td><input type="text" name="firstname" maxlength="30"/>
(max 30 characters a-z and A-Z)
</td>
</tr>
<!----- Last Name ---------------------------------------------------------->
<tr>
<td>LAST NAME</td>
<td><input type="text" name="lastname" maxlength="30"/>
(max 30 characters a-z and A-Z)
</td>
</tr>
<!----- Mobile Number ---------------------------------------------------------->
<tr>
<td>MOBILE NUMBER</td>
<td>
<input type="text" name="mobilenumber" maxlength="10" />
(10 digit number)
</td>
</tr>
<!----- Email Id ---------------------------------------------------------->
<tr>
<td>EMAIL ID</td>
<td><input type="text" name="emailid" maxlength="100" /></td>
</tr>
<!----- Choose password ---------------------------------------------------------->
<tr>
<td>PASSWORD</td>
<td><input type="password" name="password" maxlength="100" /></td>
</tr>
<!----- Submit and Reset ------------------------------------------------->
<tr>
<td colspan="2" align="center">
<input type="submit" value="Submit">
<input type="reset" value="Reset">
</td>
</tr>
</table>
</form>
</body>
</html>
I'm trying to insert data from the form data, but it doesn't work. How can I fix it?
In this updated question, I have successfully fixed some problems in inserting the values, but how can I deal with the autoincrementing column?
INSERT INTO author
(firstname
,lastname
,mobilenumber
,email
, pass) values(?,?,?,?,?);
And remove id from the prepared strings
This question already has answers here:
Browser can't access/find relative resources like CSS, images and links when calling a Servlet which forwards to a JSP
(9 answers)
Closed 5 years ago.
my code is
AddBookCategory.java
package com.bhim.admin;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.bhim.dbConnection.DBConnection;
#WebServlet(name = "category", urlPatterns = "/addCategory")
public class AddBookCategory extends HttpServlet {
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)throws
ServletException, IOException {
// for checking
System.out.println("reached here...");
DBConnection dbConnection=new DBConnection();
try {
dbConnection.open();
String query="insert into `bookcategory`(`c_name`) values(?)";
PreparedStatement preparedStatement=dbConnection.gePreparedStatement(query);
preparedStatement.setString(1, req.getParameter("category"));
int i=preparedStatement.executeUpdate();
if(i>0) {
System.out.println("Insert Successfully");
// req.getRequestDispatcher("admin/addCategory.jsp").forward(req, resp);
resp.sendRedirect("admin/addCategory.jsp");
}
else{
System.out.println("insertion Failed...");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e)
e.printStackTrace();
}
}
}
addCategory.jsp
<jsp:include page="adminHeader.jsp" />
<center>
<div class="content">
<form action="${pageContext.request.contextPath}/addCategory" method="get">
<table class="full" border="0">
<tr>
<td><h2>Category Name</h2></td>
</tr>
<tr>
<td><input type="text" name="category" /></td>
</tr>
<tr>
<td><input type="submit" value="Add Category" /></td>
</tr>
</table>
</form>
</div>
</center>
<jsp:include page="../footer.jsp" />
adminheader.jsp
<!DOCTYPE html>
<html>
<head>
<link href="../css/style.css" rel="stylesheet" type="text/css" />
<title>Bus Booking System</title>
</head>
<body>
<div id='wrapper'>
<div class="banner">
<h1>Online Library Management System</h1>
<% // Using session...
HttpSession session1 = request.getSession();
String user = (String) session1.getAttribute("user");
%>
<span class="session"> Welcome:<%=user%>
</span>
</div>
<div class="menu">
<ul>
<li>Home</li>
<li>Book Category</li>
<li>Books</li>
<li> User Mgmt</li>
<li>Search Book </li>
<li>Check allocated Book</li>
<li> Notification</li>
<li>Logout
</li>
</ul>
</div>
I am trying to forward page from servlet to jsp using RequestDispatcher CSS is not work but i also try sendRedirect css is working.Give me suggestion why css is not working when using requestDispatcher.
The problem I found was in the way you include your css file. If you debug it using Chrome you should see a 404 error loading the style.css file. Try this:
addCategory.jsp
<!DOCTYPE html>
<html>
<head>
<link href="../css/style.css" rel="stylesheet" type="text/css" />
<title>Bus Booking System</title>
</head>
<body>
<jsp:include page="adminHeader.jsp" />
<center>
<div class="content">
<form action="${pageContext.request.contextPath}/addCategory" method="get">
<table class="full" border="0">
<tr>
<td><h2>Category Name</h2></td>
</tr>
<tr>
<td><input type="text" name="category" /></td>
</tr>
<tr>
<td><input type="submit" value="Add Category" /></td>
</tr>
</table>
</form>
</div>
</center>
<jsp:include page="../footer.jsp" />
adminHeader.jsp
<div id='wrapper'>
<div class="banner">
<h1>Online Library Management System</h1>
<% // Using session...
HttpSession session1 = request.getSession();
String user = (String) session1.getAttribute("user");
%>
<span class="session"> Welcome:<%=user%>
</span>
</div>
<div class="menu">
<ul>
<li>Home</li>
<li>Book Category</li>
<li>Books</li>
<li> User Mgmt</li>
<li>Search Book </li>
<li>Check allocated Book</li>
<li> Notification</li>
<li>Logout
</li>
</ul>
</div>
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;