3 posts in topic
Flat View  Flat View

Posted By:   Jan_Thomas
Posted On:   Friday, December 6, 2002 03:17 AM

I'm trying to display a number of names stored in a database

i wanted to initialize an object for each name

my problem is that i don't know when and where to
initialize it, either in the perform method or outside
i'm very thankfull for any suggestions

Re: html:options

Posted By:   Jan_Thomas  
Posted On:   Tuesday, December 10, 2002 03:03 AM

Thanks for your replies, but i had to get the values for the collection

out of a database.here is my code:

my idea was to make an object(JavaBean) for each value from the database:

package de.tan.bo;

import java.io.Serializable;

public class Contract implements Serializable


private String contractName;

public Contract(String c)


contractName = c;


public String getContractName()


return contractName;


public void setContractName(String c)


contractName = c;


public String toString()


return contractName;



then i declared a string and made up a method to create
an object for each value(rs from the DB):


private String[] contractName = null;

private Contract[] createContracts(){

Contract[] contracts = new Contract[contractName.length];

for(int i=0; i
contracts[i] = new Contract(contractName[i]);


return contracts;


After that, i got the data from the database:


Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/WVA_DB","root","root");

Statement stmt = con.createStatement();

ResultSet rs = stmt.executeQuery("SELECT Contractname FROM contract ORDER BY Contractname");

int count = rs.getRow();

contractName = new String[count];


for(int i=0;i
contractName[i] = rs.getString(1);



Then in the "preform"-method, i instanciated the Contract-object and set them in the FormModel:


Contract[] contracts = createContracts();


with the following entry it worked fine in my JSP :

i hope that this is helpfull

Re: html:options

Posted By:   Wiebe_deJong  
Posted On:   Monday, December 9, 2002 01:27 PM

Here is an example for you:

First, create a bean that contains your data:

public class DisplayItem {

public DisplayItem() { displayValue = null; idValue = 0; }

public DisplayItem(String displayValue, int idValue) {

this.displayValue = displayValue;

this.idValue = idValue;


private String displayValue = null;

public void setDisplayValue(String displayValue) { this.displayValue = displayValue; }

public String getDisplayValue() { return displayValue; }

private int idValue = 0;

public void setIdValue(int idValue) { this.idValue = idValue; }

public int getIdValue() { return idValue; }


In your load action, create an arraylist to hold all your data. You would do your database call here, then copy the resultset into the arraylist. Here, I am just loading some static data. When the list is complete, put it into the session:

countryList = new ArrayList();

countryList.add(0, new DisplayItem("Canada", 101));

countryList.add(1, new DisplayItem("USA", 102));

countryList.add(2, new DisplayItem("Mexico", 103));

session.setAttribute("countryList", countryList);

In your jsp, you get the array from the session and display it:

Note that while I am displaying a string (name of country) I am actually storing the id (101, 102 or 103) in integer format in the form field ('country').

Re: &lt;html:options&gt;

Posted By:   Brian_Kapellusch  
Posted On:   Friday, December 6, 2002 04:44 AM

It's basically a good idea to have an Action that leads into your page, and one that happens on the post of the page (if you're posting).

So in your ActionForm (or a bean that you put in the request in the action), read all of your names from the database, and just put them in a Collection as Strings.
About | Sitemap | Contact