2012-06-29 36 views
2

如何將數據庫記錄放入ArrayList中?我想在一個jsp文件中查看它。但是,ArrayList中所有對象的值與我選擇的數據庫中的最後一條記錄相同。將數據庫記錄存儲到列表中

我在我的項目中的這些代碼:

IPBean.java

public class IPBean { 
    private String ip; 
    private String userName; 
    private String password; 
    private int maxRetry;  

    public String getIp() { 
     return ip; 
    }  
    protected void setIp(String ip) { 
     this.ip = ip; 
    }  
    public String getUserName() { 
     return userName; 
    }  
    protected void setUserName(String userName) { 
     this.userName = userName; 
    }  
    public String getPassword() { 
     return password; 
    }  
    protected void setPassword(String password) { 
     this.password = password; 
    }  
    public int getMaxRetry() { 
     return maxRetry; 
    }  
    protected void setMaxRetry(int maxRetry) { 
     this.maxRetry = maxRetry; 
    }  
} 

IPBeanMapper.java

import java.sql.*; 
import java.util.ArrayList; 

public class IPBeanMapper { 
    public ArrayList<IPBean> getIPList() throws SQLException, ClassNotFoundException { 
     ArrayList<IPBean> ipList = new ArrayList<IPBean>(); 
     Connection conn = null; 
     conn = ConnectionTools.getConnection(); 
     String SQL = "SELECT * FROM LIST_IPM"; 
     Statement statement = conn.createStatement(); 
     ResultSet rs = statement.executeQuery(SQL); 
     IPBean ipBean = new IPBean(); 

     while (rs.next()){ 

      ipBean.setIp(rs.getString("IP")); 
      ipBean.setMaxRetry(rs.getInt("NUM_OF_RETRY")); 
      ipBean.setPassword(rs.getString("PASSWORD")); 
      ipBean.setUserName(rs.getString("USERNAME")); 

      ipList.add(ipBean); 

     } 
     ConnectionTools.attemptClose(rs); 
     ConnectionTools.attemptClose(statement); 
     ConnectionTools.attemptClose(conn); 
     System.out.print(ipList.size()); 
     return ipList; 
    } 
} 

view.jsp的

<%@ page contentType="text/html;charset=UTF-8" language="java" import="DSIP.*" import="java.util.ArrayList" %> 
<html> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
    <title>DSIP.Insert</title> 
</head> 

<body> 
<jsp:useBean id="ipList" scope="application" class="IPBeanMapper"/> 
<jsp:useBean id="bean" scope="application" class="IPBean"/> 
<form name="form1" method="post" action="viewServlet"> 
    <table width="" border=""> 
     <tr bgcolor="#0099FF"> 
      <td width="90"><div align="center">ip</div></td> 
      <td width="90"><div align="center">username</div></td> 
      <td width="90"><div align="center">password</div></td> 
      <td width="90"><div align="center">maxRetry</div></td> 
     </tr> 
     <% 
      ArrayList<IPBean> list; 
      list = ipList.getIPList(); 
      for (int i = 0; i < list.size(); i++){ 
       bean = list.get(i); 
     %> 
     <tr> 
      <td><input name="ip"  type="text" size="15" value="<%=list.getIp()%>"></td> 
      <td><input name="userName" type="text" size="15" value="<%=bean.getUserName()%>"></td> 
      <td><input name="password" type="text" size="15" value="<%=bean.getPassword()%>"></td> 
      <td><input name="maxRetry" type="text" size="15" value="<%=bean.getMaxRetry()%>"></td> 
     </tr> 
     <% 
      } 
     %> 
    </table> 
</form> 
</body> 
</html> 

和我的瀏覽器中的結果顯示,第一個和第二個記錄實際上並不相同。所以,請有人幫我展示數據庫中的真實記錄。

看我的結果:http://i.stack.imgur.com/2lIM9.png

非常感謝你對我的幫助。

最好的問候,

Faizal Rizky

回答

4

變化

IPBean ipBean = new IPBean(); 

    while (rs.next()){ 

while (rs.next()){ 

IPBean ipBean = new IPBean(); 

這將創建每行,這是需要新的實例,否則將繼續壓倒一切的數據,在單實例

+0

非常感謝@Jigar,它解決了我的問題:) – faizal

+0

歡迎您:) –

2

你需要把這個IPBean ipBean = new IPBean();內while循環

因此您的代碼將成爲 -

IPBeanMapper.java

while (rs.next()){ 
IPBean ipBean = new IPBean(); 
. 
. 

問題相同的對象引用正在被添加到列表中。因此,所有初始條目都與您的最後一條記錄相同。

+0

非常感謝@Kshitij,現在我明白了。:) – faizal

+0

我歡迎...! – Kshitij

0
IPBean ipBean = new IPBean(); 

    while (rs.next()){ 

     ipBean.setIp(rs.getString("IP")); 

你需要爲每一行創建一個新實例。在環路內移動new IPBean。否則,最終在列表中多次使用相同的實例,並使用最後一次迭代中設置的值。

+0

嗯非常感謝@Thilo我現在明白了:) – faizal

1

我想你沒有把整個

ipBean bean = new IPBean(); 

進入循環。這將是一個不好的做法。只要聲明

ipBean ipBean;

while(rs.next()){ 
     bean = new ipBean(); 
} 
相關問題