aspose file tools*
The moose likes JSP and the fly likes How to populate Dropdownlist from database 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 Dropdownlist from database" Watch "How to populate Dropdownlist from database" New topic
Author

How to populate Dropdownlist from database

heyanish Raj
Greenhorn

Joined: Aug 19, 2009
Posts: 2
All,

I am new to this world.
I wrote an application that has a dropdown list and I need data from database to be populated in that dropdown before submitting and adding the data to another table.

Please help me in getting this done through beans or JSP.

Thank you all..
Anish
sandeep kethe
Greenhorn

Joined: Jan 11, 2009
Posts: 14
Hi
Store the data in hashmap or in an array list and iterate it for example use as in this way

for(int i=0;i<arraylist.size;i++)
{
Bean instance = (Instance of bean)arraylist.get(i);
//Store all the values in the bean and now close the scriptlet
//Put the code for select box like
<option value= <%=bean instance.get id %><%=bean intance.get required value&>
}

Hope you have understood

Regards
K. Sandeep
heyanish Raj
Greenhorn

Joined: Aug 19, 2009
Posts: 2
hi Sandeep,

i am very new to this, so to be honest I dont understand,.
Let me put what i have done so far..
In my Class file




package PPickDomain;

import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;

public class PickDomain {

String DDomain;
private String user;
private String pwd;
private String Domain;

//private Connection connection=null;
// private ResultSet rs = null;
// private Statement st = null;
Connection connection = null;
Statement st = null;
ResultSet rs = null;
ResultSet rs1 = null;
public PickDomain()
{
try {

DriverManager.registerDriver(
new oracle.jdbc.driver.OracleDriver());
String serverName = "localhost";
int port = 1521;
String user1 = "scott";
String password = "tiger";
String SID = "ORACLE";
String URL = "jdbcracle:thin:@" + serverName + ":" + port + ":" + SID;
connection = DriverManager.getConnection(URL, user1, password);
}
catch(Exception e)
{
System.out.println("Exception is ;"+e);
e.printStackTrace();
}
}


public void setDomain(String Domain)
{
this.Domain = Domain;
}
public String getDomian ()
{
return (this.Domain);
}

public String selectD()
{
try
{

Statement s = connection.createStatement();
rs=s.executeQuery("Select T_A_DOMAIN from BILL_DOMAIN");


//Not sure what to do next //


}

catch(Exception e)
{
System.out.println("Exception is ;"+e);
e.printStackTrace();
}
return DDomain;
}

}


in my JSP file..

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd" >

<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %> 

<HTML>
<HEAD>
<TITLE>Adding ticket to Database Table </TITLE>
</HEAD>

<BODY bgcolor="#ffffcc">
<font size="+3" color="green"><br> Add the Ticket Details here</font>
<FORM action="addticket2.jsp" method="get">
<TABLE style="background-color: #ECE5B6;" WIDTH="30%" >


<TR>
<TH width="50%">Domain</TH>
<TD width="50%"><select name="Domain" size="1">
<option selected="selected">Select a Domain</option>

</select>

</FORM>
</body>
</html>


please help with this code.
thanks buddy...
Mathew Lee
Ranch Hand

Joined: Jun 08, 2009
Posts: 238
try something like this....but you need to modify ...just putting quickly

<HTML>
<HEAD>
<TITLE>Adding ticket to Database Table </TITLE>
</HEAD>

<BODY bgcolor="#ffffcc">


<%
for(int i=0;i<arraylist.size;i++)
{
Bean instance = (Instance of bean)arraylist.get(i);

%>

<option value= <%=bean instance.get id %><%=bean intance.get required value&>


<font size="+3" color="green"><br> Add the Ticket Details here</font>

}
<FORM action="addticket2.jsp" method="get">
<TABLE style="background-color: #ECE5B6;" WIDTH="30%" >


<TR>
<TH width="50%">Domain</TH>
<TD width="50%"><select name="Domain" size="1">
<option selected="selected">Select a Domain</option>

</select>

</FORM>
</body>
</html>
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Gudim, Just above your post Bear mentioned that don't use Java code(Scriptlets) in JSP. JSTL and EL better alternatives.
Max Rahder
Ranch Hand

Joined: Nov 06, 2000
Posts: 177
Vishal Pandya wrote:Gudim, Just above your post Bear mentioned that don't use Java code(Scriptlets) in JSP. JSTL and EL better alternatives.

I agree. Your JSP would be structured something like this:

This code assumes that some previous code, such as your MVC controller, placed a java.util.List in scope -- e.g., via request.setAttribute("mylist",aList); -- and also assumes that each list item has a "getProperty()" method.

To learn more, Google "JSP expression language" and "jstl".
Sunder Ganapathy
Ranch Hand

Joined: Apr 01, 2003
Posts: 120
The Java Bean ( DTO ) is as follows:


The Java Bean ( DAO ) is as follows.


JSP accessing the DAO class is as follows.



The database ( test ) is updated with state names in a table ( states ) .
Preet Dhillon
Greenhorn

Joined: May 20, 2008
Posts: 24
In the code sample provided by Sunder Ganapathy for the dropdown, is there a posibility to pass locale from jsp to the DAO?

My database table is setup for different locales and I am currently working with something like this: ("select * FROM tablename WHERE tablename.locale='en_CA'")

I need the locale to change when user switches language. I am thinking of keeping locale in a session variable but not sure if it would be a good practice to specify session variable in the WHERE clause. Are there other better options?

Thank you in advance.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61656
    
  67

It's fine to pass the value of any scoped variable to the lower layers of the application, as long as you aren't passing the session itself (ar anything else from the servlet packages).

Another way of looking at it is if you need to import javax.servlet in model classes, you're doing it wrong!


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
 
subject: How to populate Dropdownlist from database