Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

loading a combobox dynamically

Status
Not open for further replies.

sweng

Programmer
Jun 2, 2003
10
0
0
IN
hello,

I need to know the method to load the combobox dynamically from the array obtained from a bean.
I have written a bean to get the data from database and store it in an array .

sunitha.k
 
Put your array in your session and use for loop to fill your combobox.You can find more information at Just type servlets,tutorial

Salih Sipahi
Software Engineer.
City of Istanbul Turkey
s.sipahi@sahinlerholding.com.tr
 
hi hattusas,
Thanks for the reply... Iam not using servlets... I'll paste the code below and explain so that u can understand my problem.

I have a file called bkp.jsp
the path is
C:\Program Files\Apache Tomcat4.0\webapps\examples\jsp\select

The code is given below

<title>Select everything from a database</title>
</head>

<body>

</SCRIPT>

<jsp:useBean id=&quot;select&quot; class=&quot;select.DataBaseSelect&quot; scope=&quot;page&quot;>
<jsp:setProperty name=&quot;select&quot; property=&quot;*&quot; />
</jsp:useBean>

<% out.print(select.connect()); %>
<br>
<% out.print(select.select()); %>

<p>Format results

<br>

<%@ page import=&quot;java.util.Vector&quot; %>
<% Vector aResult = select.getResult(); %>

<table>
<% for (int i=0; i < aResult.size(); i++) { %>
<tr>
<td>
<% out.print(aResult.elementAt(i)); %>


</td>

</tr>


<% } %>
</table>

</body>
</html>


I have a bean defined as follows. The path is
C:\Program Files\Apache Tomcat 4.0\webapps\examples\WEB-INF\classes\select
DSN name : Studentdb , table : stud , fields sno,sname

package select;

import java.sql.*;
import java.util.Vector;

/**
* DataBaseSelect.java
* Written by Morgan Catlin
* August 19, 1999
*
* Variables:
* Vector result = where I put the results of a select query
*
* Methods:
* Vector getResult() = returns result
* void setResult() = sets result
* String connect() = connects to a database
* String select() = selects information from a database
*/

public class DataBaseSelect {

private Vector result;

public DataBaseSelect() {
result = new Vector();
} // constructor DataBaseSelect

public String connect() {
try {
Class.forName(&quot;sun.jdbc.odbc.JdbcOdbcDriver&quot;).newInstance();
return &quot;Driver Loaded!&quot;;
} catch (Exception E) {
return &quot;Unable to load driver.&quot;;
}
}

public String select() {
try {

String url = &quot;Jdbc:eek:dbc:studentdb&quot;;
Connection C = DriverManager.getConnection(url,&quot;admin&quot;,&quot;sa&quot;);

Statement Stmt = C.createStatement();

ResultSet myResult = Stmt.executeQuery(&quot;select sno,sname from stud&quot;);

while (myResult.next()) {
result.addElement(myResult.getString(1));
}

// Clean up
myResult.close();
Stmt.close();
C.close();
return &quot;Connection Success!&quot;;
} catch (SQLException E) {
return &quot;SQLException: &quot; + E.getMessage();
}
}

/**
* Accessor for result
**/
public Vector getResult() {
return result;
}

/**
* Mutator for result
**/
public void setResult(Vector avector) {
result = avector;
}

} // class DataBaseSelect

The problem is

In the jsp file after the statement
<% out.print(aResult.elementAt(i)); %>
I want to add the elements in the array into a combo box
Iam not able to do so..


I tried to use the following code

<select name=&quot;id&quot;>
<table>
<% for (int i=0; i < aResult.size(); i++) { %>

<option value=&quot;<% =i %>&quot; > <% =aResult.elementAt(i) %> </option>

<% } %>

It gives me an error.
 
<table>
<% for (int i=0; i < aResult.size(); i++) { %>
<tr>
<td>
<% out.print(aResult.elementAt(i)); %>


</td>

</tr>


<% } %>
</table>

instead of this
use this syntax
<table>
<tr>
<td>
<select name=&quot;combo&quot; >
<% for (int i=0; i < aResult.size(); i++) { %>
<option value=&quot;&quot; ><%=aResult.elementAt(i)%>
</option>


</td>

</tr>


<% } %>
</table>

that'd fill your combobox


Salih Sipahi
Software Engineer.
City of Istanbul Turkey
s.sipahi@sahinlerholding.com.tr
 
hi,

Thanks a lot... it works!!!!!!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top