Using a Collection of beans to store a result set help.
Does anyone have any sample code on the subject? This is something I wrote up, but I wanted feedback, and also an example of how to retreive data out of the array of beans in jsp
package beanpersonal;
import beandatabase.DBConnection;
import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import java.util.Collection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
public class PersonNew {
public PersonNew() { }
public Collection getPersondata( String alias, String password ) {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
Collection retValue = new ArrayList();
String query = "SELECT * FROM person WHERE (alias = ?, password = ?)";
try {
conn = DBConnection.getDBConnection();
stmt = conn.prepareStatement( query );
stmt.setString( 1, alias );
stmt.setString( 2, password );
rs = stmt.executeQuery();
while (rs.next()) {
PersonalInfo beanrow = new PersonalInfo();
beanrow.setAlias(rs.getString("alias"));
beanrow.setPassword(rs.getString("password"));
retValue.add(beanrow);
}
return retValue;
}
catch( SQLException sqle ) {
//sqle.printStackTrace();
//throw new ApplicationSpecficException("Cannot query db", sqle);
throw new RuntimeException(sqle);
}
finally {
try {if (rs != null) rs.close();} catch (SQLException e) {}
try {if (stmt != null) stmt.close();} catch (SQLException e) {}
try {if (conn != null) conn.close();} catch (SQLException e) {}
}
}
}
# 5
sorry i only view the codes, but don't read it as a whole maybe this codes can help you..
jsp:
<%@ page contentType="text/html;charset=windows-1252"%>
<%@page import="java.util.*,mypackage.TestBean"%>
<%
String name = request.getParameter("name")!=null?request.getParameter("name"):"";
String age = request.getParameter("age")!=null?request.getParameter("age"):"";
System.out.println(name);
System.out.println(age);
Collection col = new ArrayList();
col = (Collection)session.getAttribute("col")!=null?(Collection)session.getAttribute("col"):new ArrayList();
TestBean tBean = new TestBean();
if(!name.equals(""))
tBean.setName(name);
if(!age.equals(""))
tBean.setAge(age);
if(!age.equals("") || !name.equals(""))
col.add(tBean);
session.setAttribute("col",col);
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252"/>
<title>untitled</title>
</head>
<body>
<form>
<table cellspacing="0" cellpadding="0" border="1" width="200">
<tr>
<td>Name</td>
<td>
<input type="text" name="name"/>
</td>
</tr>
<tr>
<td>Age</td>
<td>
<input type="text" name="age"/>
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Submit"/>
</td>
</tr>
</table>
<%
if(col.size()>0){
Iterator iter = col.iterator();
%>
<table cellspacing="0" cellpadding="0" border="1" width="200">
<tr>
<td>
NAME
</td>
<td>
AGE
</td>
</tr>
<%while(iter.hasNext()){
tBean = (TestBean)iter.next();
%>
<tr>
<td>
<%=tBean.getName()%>
</td>
<td>
<%=tBean.getAge()%>
</td>
</tr>
<%}%>
</table>
<%}%>
</form>
</body>
</html>
TestBean.java
package mypackage;
public class TestBean
{
private String name;
private String age;
public TestBean()
{
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getAge()
{
return age;
}
public void setAge(String age)
{
this.age = age;
}
}
# 11
the following method getPerssondata.it supposed to return array of collection right? like if i have so each column of result set is a property of a bean, and each row is just another instance of a bean. im not sure how the syntax should look when i call it in my jsp page
public Collection getPersondata( String alias, String password ) {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
Collection retValue = new ArrayList();
String query = "SELECT * FROM person WHERE (alias = ?, password = ?)";
try {
conn = DBConnection.getDBConnection();
stmt = conn.prepareStatement( query );
stmt.setString( 1, alias );
stmt.setString( 2, password );
rs = stmt.executeQuery();
while (rs.next()) {
PersonalInfo beanrow = new PersonalInfo();
beanrow.setAlias(rs.getString("alias"));
beanrow.setPassword(rs.getString("password"));
retValue.add(beanrow);
}