将数据库记录存储到列表中 [英] store a database record to a list
问题描述
如何将数据库记录放入ArrayList?我想在一个jsp文件中查看它。但是,ArrayList中的所有对象的值与数据库中选择的最后一个记录相同。
How can I put database record into a ArrayList? I want to view it in a jsp files. But, the value of all object in ArrayList is same as the last record in database I selected.
我的项目中有这些代码:
I have these code in my project:
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 / p>
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 / p>
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>
,我的浏览器中的结果显示第一和第二条记录是缩进的, 。因此,请有人帮我显示数据库中的真实记录。
and the result in my browser shows that the first and second record is indentic when in fact not the same. So, please somebody help me to show the real records from database.
可查看我的结果: http://i.stack.imgur.com/2lIM9.png
非常感谢帮助我。
最好的问候,
Faizal Rizky
Faizal Rizky
推荐答案
更改
IPBean ipBean = new IPBean();
while (rs.next()){
到
while (rs.next()){
IPBean ipBean = new IPBean();
这将为每行创建一个新的实例,这是必需的,否则它将继续覆盖单个数据实例
This will create new instance per row, which is required, otherwise it will keep overriding data in a single instance
这篇关于将数据库记录存储到列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!