Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to populate value in combo box

 
sarika Gupta
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just search it out with "populate combo" you'll get lots of material on this forum...
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Karthik Rallapalli
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Do you want the java code which is embedded in JSP to populate a ComboBox.
 
sarika Gupta
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic