【问题标题】:I am not able to get multiple records based on the Search id. It is displaying only one record我无法根据搜索 ID 获取多条记录。它只显示一条记录
【发布时间】:2016-12-11 11:43:44
【问题描述】:

假设在我的表中,我有 CAT、CAMEL、CAP 当我按“CA”搜索时,它显示的是一条记录而不是所有记录。

在数据库中:

public List<Application> getAllapplicationDetails(String name)
 {   
List<Application> app=new ArrayList<Application>();
try
{

 Connection con = null;

  String dbUrl = "jdbc:oracle:thin:@localhost:1521:XE";
  System.out.println("1 step");
  Class.forName("oracle.jdbc.driver.OracleDriver");
    System.out.println("2 step");
    con = DriverManager.getConnection (dbUrl, "system", "system");
    System.out.println("3 step");      
PreparedStatement ps = con.prepareStatement("select * from Application where LOWER(name) like '"+ name.toLowerCase()+"%'");
System.out.println("Entering in to the fucntion 2");
ps.execute();
System.out.println("Entering in to the fucntion 13");
ResultSet rs  =ps.getResultSet();
if(rs.next())
{
System.out.println("Entering in to the fucntion 4");
Application abean=new Application();
abean.setName(rs.getString(1));
abean.setApplicationName(rs.getString(2));
abean.setContactNumber(rs.getInt(3));
app.add(abean);
System.out.println("Entering in to the fucntion 1" + app);
}
}
catch(SQLException e)
    {
     e.printStackTrace();
} catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
return app;

}

在 servlet 中:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        DBConnection db=new DBConnection();
        HttpSession hs = request.getSession();
        String name=request.getParameter("appname");
        List<Details> dbean=db.getAllDetails(name);

        hs.setAttribute("detbean",dbean);
        if(dbean.isEmpty())
        {
            response.setContentType("text/html");  
            PrintWriter out = response.getWriter();
            out.print("<caption>Sorry, No Record Found</caption>");
        }
        else
        response.sendRedirect("Application.jsp");
    }

在其他 jsp 中,我正在显示传递“apname”关键字并使用列表并通过从 servlet 类传递会话在其他 jsp 页面中显示记录

但在 output 中,它只显示一条记录。请帮我解决这个问题

【问题讨论】:

    标签: java


    【解决方案1】:

    好吧,当您阅读结果时,您正在使用if(rs.next())。这只会显示第一个结果。您应该改用while(rs.next()),这样它就可以遍历所有元素。

    编辑:您确定您的查询返回多于一行吗? 写下以下内容以确保:

    int rowcount=0;
    if(rs.last())
        rowcount=rs.getRow();
    
    rs.beforeFirst(); //To not miss first item
    System.out.println("N rows: "+rowcount);
    while(rs.next()) {
        //Do your stuff
    }
    

    【讨论】:

    • 我使用了 while(rs.next()) ,仍然只获得了 1 条记录
    • 你确定你的方法调用正确吗?
    猜你喜欢
    • 2021-09-17
    • 2010-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多