Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to populate Dropdownlist from database

 
heyanish Raj
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 238
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 120
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65218
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic