How to display information from database using drop down list in JSP?

Hi all.

Like the tile above suggest, I'm having difficulty in obtaining the data as well as displaying them in a drop down list.

For example: If i were to have the following in my database:

SerialNoFood

1Bread

2Milk

3Butter

The drop down list should look like the following:

[Bread][\/]

[Milk]

[Butter]

How do i go around coding it in JSP?

Thanks in advance.

C.K

[448 byte] By [CKeera] at [2007-10-2 21:28:59]
# 1

Hai Please try the following code

You have to make some changes according to your system i am using

MSSqlserver Driver change the class name and the connection string

for your need

<HTML>

<HEAD>

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

<LINK href="../theme/Master.css" rel="stylesheet" type="text/css">

<TITLE>ListDrop.jsp</TITLE>

<%!Connection con;

%>

</HEAD>

<BODY>

<select>

<%

try {

Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");\\Here you specify the Class Nale

con = DriverManager

.getConnection(

"jdbc:microsoft:sqlserver://javaserver:1433;DatabaseName=karthi",

"sa", "sa");\\here Connection string

ResultSet rs;

Statement st = con.createStatement();

rs = st.executeQuery("select SerialNo,Food from Foods");

while (rs.next()) {

%>

<option value='<%= rs.getString("SerialNo") %>'><%= rs.getString("Food")%></option>

<%

}

} catch (Exception e) {

e.printStackTrace();

}

%>

</select>

</BODY>

</HTML>

Karthikeyan_Vaithilingama at 2007-7-14 0:42:03 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
grt answer..
dipak66a at 2007-7-14 0:42:03 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
Hi, It is not the best practice of having the java code on jsp.You can work on AJAX, that provides you the answer.ThanksBadri
pnatha at 2007-7-14 0:42:03 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
Yeah i agree with thisBut AJAX is browser dependant better you can use MVC design pattern
Karthikeyan_Vaithilingama at 2007-7-14 0:42:03 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
Thank you very much for ur answer. Really appreciate that.Thanks again.Cheers.C.K
CKeera at 2007-7-14 0:42:03 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...