1

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><a href="newoder.jsp">New Order</a></li> 
        <li><a href="updateorder.jsp">Update Order</a></li> 
        <li><a href="trackorder.jsp">Track Order</a></li> 
        <li><a href="trackdelay.jsp">Track Delay</a></li> 
        <li><a href="vieworder.jsp">View Database</a></li> 
        <li><a href="delete.jsp">Delete Order</a></li> 
        <li><a href="logout.jsp">Logout</a></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 <a href="eric.jsp"><b>login</b></a>

<%
} %> 
</body>
</html>

2 Answers 2

3

You'll be wanting

    while (rs.next()){ 

instead of

    if (rs.next()){ 
Sign up to request clarification or add additional context in comments.

Comments

0

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()) {

        [...]

    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.