Im using a web application ,which runs on Apache Tomcat and has Mysql Database as backend
I want to retrieve multiple rows for a particular column usin the Where clause
For Example If I give select * from xyz where type=abc
I should get all the rows having type abc.
Problem:
I use the JDBC connection to achieve this, however Only the 1st row matching the where condition is returned rather than all the rows (even though multiple rows match the criteria in database)
Kindly help me resolve this
Code:
<%# page import="java.sql.*" %>
<html>
<head><link href="style.css" rel ="stylesheet" type="text/css"></head>
<body bgcolor="white" >
<div id="container">
<div id="header">
<img src="logo.jpg">
<div class ="horiztext"><p> Order Tracker</p></div>
</div>
</div>
<br>
<img src="banner.jpg" width="1500 " height="5"><br>
<% if(session.getAttribute("username") !=null)
{
%>
<div id="navbar">
<ul>
<li>New Order</li>
<li>Update Order</li>
<li>Track Order</li>
<li>Track Delay</li>
<li>View Database</li>
<li>Delete Order</li>
<li>Logout</li>
</ul>
</div>
<br>
<br>
<form>
<TABLE cellpadding="15" border="1" style="background-color: #ffffcc;">
<%
String ProductNamez=request.getParameter("ProductName");
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3307
/test","root", "root");
Statement st=conn.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM inventory WHERE ProductName = '"+
ProductNamez +"' ");
if(rs.next()){
%>
<tr>
<tr><th>Serial No</th>
<th>Product Name</th>
<th>Product Type</th>
<th>Model</th>
<th>Make</th>
<th>License / Voucher</th>
<th>Location</th>
</tr>
<tr>
<td><%=rs.getString(1)%></td>
<td><%=rs.getString(2)%></td>
<td><%=rs.getString(3)%></td>
<td><%=rs.getString(4)%></td>
<td><%=rs.getString(5)%></td>
<td><%=rs.getString(6)%></td>
<td><%=rs.getString(7)%></td>
</tr>
<%
}
%>
</table>
</form>
<%
}
else { %>
you are not logged in click here to <b>login</b>
<%
} %>
</body>
</html>
You'll be wanting
while (rs.next()){
instead of
if (rs.next()){
It's because you just do a if(rs.next()){ so only the first row is shown.
You should do this instead
while (rs.next()) {
[...]
}
Related
I am able to float the two tables, one is on the left side and one is on the right side. However, the table size on the left is not symmetrical to the table on the right. I am unable to display the table size equally. What should i do to display the table side by side equally?
html
html file
<div class="floatleft">
<div class="left">
<div class="row">
<div class="col-md-3">
<%
Connection conn = null;
Class.forName("com.mysql.jdbc.Driver");
String connURL = "jdbc:mysql://localhost/test?user=1234&password=1234";
conn = DriverManager.getConnection(connURL);
String sql = "select * from test";
PreparedStatement pstmt=conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
%>
</div>
</div>
<div>
<p<u>Arm</u></a></font></p>
<table>
<tr>
<th >Arm Exercises</th>
<th >Number of times</th>
<th colspan='1'> </th>
<%
String sql1="";
{
sql1 = "select * from test";
PreparedStatement pstmt1=conn.prepareStatement(sql1);
ResultSet rs1 = pstmt1.executeQuery();
while(rs1.next()){
String id = rs1.getString("ID");
out.println("<tr>");
out.println("<td style = 'width: 85%'>");
out.println(arm);
out.println("</td>");
%>
<td>
<textarea cols="2"></textarea>
</td>
<%
out.println("</tr>");
}
conn.close();
}
%>
</table><br>
</div>
Just put the table in the div element with a class name.
<div class="floatleft">
<table>
</table>
<div class="floatright">
<table>
</table>
i am working in an old application developed using classic ASP and VB, i am trying to export an HTML table from an asp page to word, i searched the internet for this and i found a good code that its working perfectly, he problem is that it export the whole page to word, i need to export only a part of the page which is the <table></table>
<%# Language=VBScript %>
<%
Option Explicit
Response.Buffer = TRUE
Response.ContentType = "application/vnd.ms-word"
Response.AddHeader "Content-Disposition", "attachment; filename = MyDoc.doc"
%>
<html>
<head>
</head>
<body>
<%
Dim Conn,strSQL,objRec
Set Conn = Server.Createobject("ADODB.Connection")
Conn.Open "DRIVER=Microsoft Access Driver (*.mdb);DBQ=" & Server.MapPath("shotdev/mydatabase.mdb"),"" , ""
strSQL = "SELECT * FROM customer "
Set objRec = Server.CreateObject("ADODB.Recordset")
objRec.Open strSQL, Conn, 1,3
%>
<table width="600" border="1">
<tr>
<th width="91"> <div align="center">CustomerID </div></th>
<th width="98"> <div align="center">Name </div></th>
<th width="198"> <div align="center">Email </div></th>
<th width="97"> <div align="center">CountryCode </div></th>
<th width="59"> <div align="center">Budget </div></th>
<th width="71"> <div align="center">Used </div></th>
</tr>
<%
While Not objRec.EOF
%>
<tr>
<td><div align="center"><%=objRec.Fields("CustomerID").Value%></div></td>
<td><%=objRec.Fields("Name").Value%></td>
<td><%=objRec.Fields("Email").Value%></td>
<td><div align="center"><%=objRec.Fields("CountryCode").Value%></div></td>
<td align="right"><%=objRec.Fields("Budget").Value%></td>
<td align="right"><%=objRec.Fields("Used").Value%></td>
</tr>
<%
objRec.MoveNext
Wend
%>
</table>
<%
objRec.Close()
Conn.Close()
Set objRec = Nothing
Set Conn = Nothing
%>
</body>
</html>
you can check the link here
can you please help me to solve this issue
thank you
Okay I have a huge problem with this. I have spend hours with my group trying to solve this small problem we have on the group project and cant find the answer
I either get null pointer exeption or something with org.apache.jasper.jasperexception or the most common ones is the try with out catch or finally where I use try and catch ..
Please help me o
Form for team name
<body>
<h2>Search Team</h2>
<form action="searchteam.jsp" method="post">
<table border="0">
<tr>
<td>Team Name</td>
<td><input type="text" name="tm"/> </td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Search"/>
</td>
</tr>
</table>
</form>
</body>
and this here is the searchteam.jsp
<%#page import="livescore_pack.*"%>
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<jsp:useBean id="bean" class="livescore_pack.Accessor" scope="session"/>
<%
String team = request.getParameter("tm");
Team me = bean.getTeams(team);
if(me == null)
response.sendRedirect("index.jsp");
else {
session.setAttribute("tm", me);
response.sendRedirect("doSearchTeam.jsp");
}
%>
and the dosearchteam.jsp
<%#page import="livescore_pack.Team"%>
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<jsp:useBean id="bean" class="livescore_pack.Accessor" scope="session"/>
<%
String me = request.getParameter("tm");
Team tm =(Team) session.getAttribute("tm");
if(me == null)
response.sendRedirect("index.jsp");
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<link rel="stylesheet" type="text/css" href="st1.css">
</head>
<body>
<div id="menu">
</div>
<div id="main">
<%
Team team = bean.getTeams(tm.getTm_name()); //dame eshi null
%>
<b> <%= tm.getTm_name() %> </b>
for <%= tm.getCountry_pk() %>
<p> <%= tm.getFounded()%> </p>
<p> <%= tm.getStadium() %> </p>
<p> <%= tm.getManager()%> </p>
<p> <%= tm.getLeagues() %> </p>
<p> <%= tm.getCups() %> </p>
<%
}
%>
</div>
</body>
</html>
and also the part getTeams in our accessor .java
public Team getTeams(String tm_name) {
try{
Connection cn = getGetDATABASE().getConnection();
String sql = "SELECT * FROM Team WHERE tm_name = ? ";
PreparedStatement pst = cn.prepareStatement(sql);
pst.setString(1, tm_name);
ResultSet rs = pst.executeQuery();
while(rs.next()) {
Team tm = new Team();
tm.setTm_pk(rs.getLong("team_id"));
tm.setCountry_pk(rs.getLong("country_id"));
tm.setTm_name(rs.getString("tm_name"));
tm.setFounded(rs.getLong("founded"));
tm.setStadium(rs.getString("stadium"));
tm.setManager(rs.getString("manager"));
tm.setLeagues(rs.getInt("leagues"));
tm.setCups(rs.getInt("cups"));
return tm;
}
}
catch(Exception e) {
String msg = e.getMessage();
}
return null;
}
Your try catch statement has the wrong syntax. it has an extra } before the catch block try deleting that. If that fails and you are still getting errors can you paste in a copy of the jasper exception that is returned
Also as far as i can see you are calling getTeams() twice with the same name. Once in searchteam.jsp and the second time in dosearchteams.jsp. You have set all the variables in serachteam.jsp you do not need to call it again in dosearchteams.jsp this appears to be a redundant call.
Okay sorry for the delaid answer
I have fixed my problem it seems i was connecting to a wrong part of the database.
once that was fixed the problem was solved.
How do I go about displaying the results of a query from MYSQL in a jsp using html tags? My instructor said to do the query in the .jsp file and use html tags to display the results. What is the correct syntax for this. Can I get an example or a link to a page that will show me. I have no problem writing the query, just unsure about the jsp/html display. Thanks
<BODY>
<H2>JSP Expressions</H2>
<UL>
<LI>Current time: <%= new java.util.Date() %>
<LI>Your hostname: <%= request.getRemoteHost() %>
<LI>Your session ID: <%= session.getId() %>
<LI>The <CODE>testParam</CODE> form parameter:
<%= request.getParameter("testParam") %>
</UL>
</BODY>
</HTML>
in jsp we have scriptlet tags ( <% %> ).you can use the jdbc statements between these jsp tags, The below sample code shows the display query in jsp using html,
<%#page import="java.sql.*,java.util.*,java.io.*"%>
<html>
<head>
<body background="1.jpg">
<title>ehealthcare advisor</title>
</head>
<body><center>
<h1><center>E-HEALTH CARE ADVISOR</center></h1>
<h4><center>get best solutions in minuites...</center></h4>
<table border="0" cellspacing="50" cell padding="10">
<tr>
<td> HOME</td>
<td> LOGOUT</td>
<td>ABOUT US</td>
</table>
</center>
<center>
<h2>PATIENT LIST</h2>
<table border="1" width="40%">
<thead>
<th>UserId</th>
<th>Name</th>
<th>Age</th>
<th>Gender</th>
<th>Occupation</th>
<th>Height</th>
<th>Weight</th>
</thead>
<tbody>
<%
Statement st = null;
ResultSet rs = null;
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con =DriverManager.getConnection("jdbc:mysql://localhost:3306/ehealthcare","root","root");
st = con.createStatement();
String qry ="select * from user";
rs = st.executeQuery(qry);
while(rs.next()){ %>
<tr>
<td><%=rs.getString(1)%></td>
<td><%=rs.getString(2)%></td>
<td><%=rs.getString(3)%></td>
<td><%=rs.getString(4)%></td>
<td><%=rs.getString(5)%></td>
<td><%=rs.getString(6)%></td>
<td><%=rs.getString(7)%></td>
</tr>
<%
}
con.close();
st.close();
}
catch(Exception ex){
out.println(ex);
}
%>
</tbody>
</table>
</body>
</html>
Here I give the link you refer it.
Jsp Sample Database Access, it is like core java
Jsp using JSTL tags
Database from Jsp
If you use No. 2 . Jsp using JSTL tags, that will be good programming.
JSTL tags are simple to learn.
I have run into a problem where I have to display certain data that is present in the Ratings table of MYSQL depending upon the user that is logged in.
The heirarchy is as below :
login.jsp --> check.jsp --> welcome.jsp
I have to display data on Welcome.jsp depending on the user who is logged in. I am unable to get the user parameter to server as an input for the sql query that displays the user specific data. Here are my files.
login.jsp
<%--
Document : login
Created on : May 15, 2012, 10:36:24 AM
Author : Diaa
--%>
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<script>
function validate(){
var username=document.form.user.value;
var password=document.form.pass.value;
if(username==""){
alert("Enter Username!");
return false;
}
if(password==""){
alert("Enter Password!");
return false;
}
return true;
}
</script>
<div align="center">
<div class='cssmenu'>
<ul>
<li class='active '><a href='index.jsp'><span>Home</span></a></li>
<li><a href='login.jsp'><span>Login</span></a></li>
<li><a href='index.jsp'><span>About</span></a></li>
<li><a href='#'><span>Contact</span></a></li>
</ul>
</div>
<form name="form" method="post" action="check.jsp" onsubmit="javascript:return validate();">
<div align="center">
<h1>USER LOGIN</h1>
<table>
<tr><td bgcolor="#FF9900">Username:</td><td bgcolor="#33CCCC"><input type="text" name="user"></td></tr>
<tr><td bgcolor="#FF9900">Password:</td><td bgcolor="#33CCCC"><input type="password" name="pass"></td></tr>
<tr><td></td><td><input type="submit" value="Submit"></td></tr>
</table>
</div>
check.jsp
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%#page import="java.sql.*"%>
<%
String user=request.getParameter("user");
String pass=request.getParameter("pass");
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/recommend","root","root");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select UserID,password from Users where UserID='"+user+"' and password='"+pass+"'");
int count=0;
while(rs.next())
{
count++;
}
if(count>0)
{
out.println("welcome "+user);
response.sendRedirect("welcome.jsp?msg=welcome "+user +" logout");
session.removeAttribute("user");
}
else
{
response.sendRedirect("login.jsp?msg=Invalid Username or Password");
}
%>
welcome.jsp
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%#taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>
<%-- Database Interaction Queries Start --%>
<% String user=(String)session.getAttribute(user); %>
<sql:setDataSource var="genretype" driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost/recommend"
user="root" password="root"/>
<sql:query dataSource="${genretype}" var="result">
SELECT * from Genres;
</sql:query>
<sql:query dataSource="${genretype}" var="result1">
SELECT * from Ratings where UserID = ?
<sql:param value="${user}"/>
</sql:query>
<%-- Database Interaction Queries End --%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Heading</title>
</head>
<body>
<div align="center">
<div class='cssmenu'>
<ul>
<li class='active '><a href='index.jsp'><span>Home</span></a></li>
<li><a href='login.jsp'><span>Login</span></a></li>
<li><a href='index.jsp'><span>About</span></a></li>
<li><a href='#'><span>Contact</span></a></li>
</ul>
</div>
<%String msg=request.getParameter("msg");
if(msg!=null){
%>
<label><font color="red"><%=msg%></font></label>
<%
}
%>
<h1>Sample Title</h1>
<%--For Displaying Genre List Start --%>
<div>
<div align="left" position:absolute left:1200px>
<c:forEach var="row" items="${result.rows}">
<ul id="tab_nav">
<li><c:out value="${row.GenreName}"/></li>
</ul>
</c:forEach>
</div>
<div id="box" >
<table border="1">
<c:forEach var="row" items="${result1.rows}">
<tr>
<td><c:out value="${row.Rating}"/></td>
<td><c:out value="${row.UserID}"/></td>
<td><c:out value="${row.GenreName}"/></td>
<td><c:out value="${row.MovieName}"/></td>
</tr>
</c:forEach>
</table>
</div>
</div>
</body>
</html>
Can anyone please look into this and let me know what I am missing here :( Please feel free to ask back any other information apart from the code posted.
Fingers Crossed.. Awaiting for help!!
Cheers!
You cannot append HTML to the URL parameter of your sendRedirect() method call. There could be encoding issues with it and so it may be failing:
response.sendRedirect("welcome.jsp?msg=welcome "+user +" logout");
This should simply be
response.sendRedirect("welcome.jsp?user=" + user);
with the message being constructed in the JSP as
<c:if test="${not empty user}">
<label><font color="red">Welcome ${user}</font></label><br />
logout
</c:if>
If this doesn't solve the issue please update your question with how's the application behaving. Do you see any errors? Are you logging the exceptions anywhere?
I did a sample code here for you to check out.
Here is a sample login.jsp form:
<form action="check.jsp" method="post">
<table border="0">
<tr>
<input type="text" name="user" id="user"/>
</tr>
<tr>
<input type="password" name="pass" id="pass"/>
</tr>
<td>
<input type="submit" id="submit"/>
</td>
</table>
</form>
Then,the check.jsp could look this way:
<%#page import="java.sql.*" %>
<%
Connection con;
ResultSet rs;
PreparedStatement ps;
String query;
String user = request.getParameter("user");
String pass = request.getParameter("pass");
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/recommend","root","root");
query = "select userID,password from Users where userID='"+user+"' and password='"+pass+"'";
ps = con.prepareStatement(query);
//ps.setString(1,request.getParameter("user"));
//ps.setString(1,request.getParameter("pass"));
rs = ps.executeQuery();
if(rs.next()){
session.setAttribute("user", user);
%>
<jsp:forward page="welcome.jsp"/>
<%
}else{
response.sendRedirect("login.jsp");
}
}catch(Exception e){
out.println(e);
}
%>
from the code above,you can see that after the querying of the database the attribute of the user is set and also the check.jsp forward that to the welcome.jsp page with that attribute of the user that has been set,else if the user or password is Incorrect,the person is being redirected to the login.jsp page.
The welcome.jsp page looks this way:
<body>
<% String user = (String)session.getAttribute("user");%>
<% if(user == null){
response.sendRedirect("login.jsp");
}else{
out.println("Welcome "+user);
}
%>
</body>
In the welcome.jsp page,it retrieves the session attribute of the user that is logged in via the session.getAttribute() method.It then checks if the session attribute is null and then redirects back to the user,else it prints Welcome + the user.Note that the user being printed is from the session.getAttribute which is gotten from the session.setAttribute in the check.jsp,that way you are able to get the user parameter as input from the login. I hope this helps.