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) {}

}

}

}

[1831 byte] By [masago1225a] at [2007-11-26 14:54:13]
# 1
Seem that you code is ok1. Store the collection in session2. Retrieve the collection and iterate the collection with the type of the bean
jgalacambraa at 2007-7-8 8:42:39 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
do you have any example code for that?
masago1225a at 2007-7-8 8:42:39 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
1.Do the codes that you posted is you help class?2. Are you calling the methods on jsp or servlet?
jgalacambraa at 2007-7-8 8:42:39 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

im using jsp.

i plan to usebean the PersonNew so i can use the method getPersondata.

the bean i use to store the rows of the result set is

PersonalInfo beanrow = new PersonalInfo();

i just never use collection of bean before, thats why im asking for examples. i think i may need to use iteration but i never use that before either.

i want to print out the parameters in my PersonalInfo bean array resulted from getPersondata in JSP.

masago1225a at 2007-7-8 8:42:39 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 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;

}

}

jgalacambraa at 2007-7-8 8:42:39 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
oh yes, that is what im looking for, (i think)mainly how to output the data from each bean of the collection array.thx a lot man. i just wish there was more material on this subject on the internet.
masago1225a at 2007-7-8 8:42:39 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7
if there's is any case you have some problem on the codes, just tell me.. and i will help if i know it
jgalacambraa at 2007-7-8 8:42:39 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8
hmm im having trouble getting it to work
mikeni1225a at 2007-7-8 8:42:39 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9

im never worked with collection of objects before like bean, im not sure how the syntax works my jsp page look like this

<%@ page import="java.util.Collection"%>

<jsp:useBean id="peep" scope="page" class="beanpersonal.PersonNew" />

<%

Collection col = new ArrayList();

// not sure how to use the collection of bean

col = peep.getPersondata("johnsmith","password123");

%>

mikeni1225a at 2007-7-8 8:42:39 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 10
based on your codes, it seems that it is not a collection of beans
jgalacambraa at 2007-7-8 8:42:39 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 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);

}

mikeni1225a at 2007-7-8 8:42:39 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 12
yes, but what is jsp:useBean tag for?.. it represents only one bean
jgalacambraa at 2007-7-8 8:42:39 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...