【问题标题】:store a database record to a list将数据库记录存储到列表中
【发布时间】:2012-06-30 19:20:07
【问题描述】:

如何将数据库记录放入 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

非常感谢你帮助我。

最好的问候,

法扎尔·里茨基

【问题讨论】:

    标签: java database jsp resultset


    【解决方案1】:

    改变

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

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

    这将为每行创建新实例,这是必需的,否则它将继续覆盖单个实例中的数据

    【讨论】:

      【解决方案2】:

      你需要把这个IPBean ipBean = new IPBean();放在while循环中

      所以你的代码会变成-

      IPBeanMapper.java

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

      同一对象引用被添加到列表中的问题。所以所有初始条目都与您的最后一条记录相同。

      【讨论】:

        【解决方案3】:

        我认为你不必把整个

        ipBean bean = new IPBean();
        

        进入循环。那将是一个不好的做法。只需声明

        ipBean ipBean;

        while(rs.next()){
              bean = new ipBean();
        } 
        

        【讨论】:

          【解决方案4】:
              IPBean ipBean = new IPBean();
          
              while (rs.next()){
          
                  ipBean.setIp(rs.getString("IP"));
          

          您需要为每一行创建一个新实例。将new IPBean 移动到循环内。否则,您最终会在列表中多次使用相同的实例,并使用您在上次迭代中设置的值。

          【讨论】:

          • 嗯非常感谢@Thilo 我现在明白了:)
          猜你喜欢
          • 2011-07-19
          • 1970-01-01
          • 2013-05-06
          • 2015-05-26
          • 2013-05-14
          • 2012-03-15
          • 2012-07-17
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多