2016-12-11 3 views
0

と仮定1つのレコードのみが表示されている、私はCAT、CAMEL ,, CAPを持って、それが一つのレコードではないすべてのレコードを表示しています。データベース内検索IDに基づいて複数のレコードを取得できません。私は「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; 

} 

:他のJSPで

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"); 
    } 

、私は「apname」のキーワードを渡すと、リストを使用して他のJSPページ内のレコードを表示する表示してセッションを渡していますサーブレットクラスから

しかし、出力では、1つのレコードのみが表示されています。あなたは結果を読んでいる間、あなたはif(rs.next())を使用している、まあこの

答えて

0

に私を助けてください。これは、最初の結果のみを表示します。代わりにwhile(rs.next())を使用する必要があります。そのため、すべての要素を反復処理できます。

編集:あなたは必ずあなたのクエリが1行以上を返しますか? 確認するために次のことを書く:

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 
} 
+0

私はまだ1つだけのレコード – Muskan

+0

を同じ取得し、しばらく(rs.nextを())を使用し、あなたが正しくあなたのメソッドを呼び出していますか? – Shirkam

関連する問題