简体   繁体   English

使用Where子句从Mysql数据库中获取多个记录

[英]Fetch Multiple Records from Mysql Database Using Where Clause

Im using a web application ,which runs on Apache Tomcat and has Mysql Database as backend 我正在使用一个Web应用程序,它运行在Apache Tomcat上,并将Mysql数据库作为后端

I want to retrieve multiple rows for a particular column usin the Where clause 我想在Where子句中检索特定列的多个行

For Example If I give select * from xyz where type=abc 例如如果我从xyz给出select *,其中type = abc

I should get all the rows having type abc. 我应该得到所有类型为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) 我使用JDBC连接来实现这一点,但只返回匹配where条件的第一行而不是所有行(即使多行符合数据库中的条件)

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>

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. 这是因为你只做一个if(rs.next()){所以只显示第一行。

You should do this instead 你应该这样做

while (rs.next()) {

        [...]

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM