aspose file tools*
The moose likes JSP and the fly likes how to populate value in combo box Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "how to populate value in combo box" Watch "how to populate value in combo box" New topic
Author

how to populate value in combo box

sarika Gupta
Greenhorn

Joined: Apr 27, 2005
Posts: 18
hi!

I m developing an JSP application, can someone please help me to retrieve values from a (SQL) database and enable those retrieved values to populate the comboBox component of my form. In addition, if a user selects a certain value through populated comboBox,that also insert into database pls. give an example of code that would some how solve my problem. please help me as soon as possible!!!

thank you!!!
Kutub Shaikh
Greenhorn

Joined: Apr 04, 2005
Posts: 13
Just search it out with "populate combo" you'll get lots of material on this forum...


The Man With The Golden Gun...
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I've just posted an example project that does this:
http://simple.souther.us/not-so-simple.html

It's written with an MVC architecture but it should give you the basic idea.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Karthik Rallapalli
Greenhorn

Joined: Jul 01, 2004
Posts: 13
This is the simplest way...
My sample code..


buf1.append("<select name=\"LocationB\">");
buf1.append("<option value=\"\">-Location-</option>");
db.setResultSet("SELECT distinct CLEAR_LOCATION FROM PMS.VW_BILL_PROJECTREQUEST order by CLEAR_LOCATION");
rs = db.getResultSet();
while(rs.next()) {
buf1.append("<option value=\"" + rs.getString("CLEAR_LOCATION").trim() + "\">" + rs.getString("CLEAR_LOCATION").trim() + "</option>");
}
buf1.append("</select>");
tLocB = buf1.toString();
buf1.delete(0, buf1.length());
rs = null;


and down in in the jsp replace the select box portion with ....
<%= tLocB%>
sarika Gupta
Greenhorn

Joined: Apr 27, 2005
Posts: 18
hi,
thanks a lot for ur kind help ,but i need some code in java so it is easy to understand .
actualy i want to populate value from database into combobox,
for ex-:
in student form there is a column qualification
so how can populate value from database
thank you!!!
sarika Gupta
Greenhorn

Joined: Apr 27, 2005
Posts: 18
hi,
thanks a lot for ur kind help ,but i need some code in java so it is easy to understand .
actualy i want to populate value from database into combobox,
for ex-:
in student form there is a column qualification
so how can populate value from database
thank you!!!
Raghuraman Muthuswamy
Ranch Hand

Joined: Mar 18, 2003
Posts: 73
Hi,
Do you want the java code which is embedded in JSP to populate a ComboBox.
sarika Gupta
Greenhorn

Joined: Apr 27, 2005
Posts: 18
hi,
yes i want the java code which is embedded in JSP to populate a ComboBox
please please it is so importent for me.
Raghuraman Muthuswamy
Ranch Hand

Joined: Mar 18, 2003
Posts: 73
hi sarika,
The code uses MS Access as database for JDBC. This is just a sample code. But the logic will be same for all databases.

<html>
<head>
<title>Combo Box Data from Database</title>
</head>
<%@ page import="java.sql.*"%>

<body>
<%
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc dbc SN_Name";
Connection con = DriverManager.getConnection(url);
Statement s = con.createStatement();

ResultSet re = s.executeQuery("select * from Table_Name");
%>
<h2>Username from Database Modified: </h2>
<select>
<%
while(re.next())
{
String un=re.getString(Field_Name);
%>
<option value="<%= un%>"><%= un%></option>
<%
}
%>
</select>

</body>
</html>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to populate value in combo box