You are on page 1of 3

27/12/2017 How to get Data into DropDown List from Database in JSP %

JavaTechCodes
Java Programming Examples on Servlet, JSP, Spring, Hibernate, JavaScript & much more

How to get Data into DropDown List from Database in


JSP
By Anshul Gupta | June 11, 2017 0 Comment

How to get Data into DropDown List from Database in JSP

We will learn how to get Data into DropDown List from Database table in JSP.we will create a drop-down list in
which we will not set the drop down values.we will get all the drop-down values from the database table
data. rst, we will create a connection between JSP page and Database and then Fetch the column data that we
will use in the drop-down as a value.All the Database column data will show as a dropdown value.all the column
data will be in DropDown List.Let’s see the Example and understand the logic.

Database Query–

1 CREATE TABLE employee_table(


2    emp_id NUMERIC(10) NOT NULL,
3    first_name varchar(40) NOT NULL,
4    last_name varchar(40) NOT NULL,
5    gender varchar(40) NOT NULL,
6    email_id varchar(40) NOT NULL,
7    mobileno varchar(40) NOT NULL,
8   CONSTRAINT emp_id_pk PRIMARY KEY (emp_id)

http://www.javatechcodes.com/jsp/get-data-dropdown-list-database-jsp/ 1/3
27/12/2017 How to get Data into DropDown List from Database in JSP %

9 );

Here we are retrieving all the employees’id from employee table and showing it to the DropDown List of JSP le.

Source Code of JSP le- index.jsp

1 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-


2 <%@ page import ="java.sql.*" %>
3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/l
4 <html>
5 <head>
6 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
7 <title>How to get Data into DropDown List from Database in JSP</title>
8 </head>
9 <body>
10 <center>
11 <h1>How to get Data into DropDown List from Database in JSP</h1>
12 <%
13 Class.forName("com.mysql.jdbc.Driver");  
14 Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3307/projectname","userna
15 PreparedStatement ps= con.prepareStatement("select * from employee_table");
16 ResultSet rs=ps.executeQuery();  
17 %>
18 <h3>Employee ID:-
19 <select>
20 <%
21 while(rs.next()) {
22 String empid=rs.getString("emp_id");
23 %>
24 <option value="<%=empid %>"><%=empid %></option>
25 <%
26 }
27 %>
28 </select>
29 </h3>
30 </table>
31 </center>
32 </body>
33 </html>

Output-Getting all the column data into a Dropdown list.

You might also like


http://www.javatechcodes.com/jsp/get-data-dropdown-list-database-jsp/ 2/3
27/12/2017 How to get Data into DropDown List from Database in JSP %

How to retrieve Data From How to upload image in


Database and Display in ... folder and path in
Database...

Category: JSP Tags: connection , Database , DropDown , Query

http://www.javatechcodes.com/jsp/get-data-dropdown-list-database-jsp/ 3/3

You might also like