如何从数据库中获取下拉值并在jsp中显示 [英] How to fetch the dropdown values from database and display in jsp
问题描述
我在jsp中有两个下拉菜单,必须从数据库中获取下拉列表并在jsp中显示它.我是第一次使用jsp.您能给我一个从数据库中获取下拉列表并在jsp dropdown元素中显示值的想法吗?谢谢!
I have two dropdowns in jsp and have to get dropdown list from database and show it in jsp. I am using jsp for the first time . Can you give me an idea to fetch the dropdown list from database and display the values in jsp dropdown element.Thanks in advance
推荐答案
如何从数据库中获取下拉值并显示在jsp中:
how to fetch the dropdown values from database and display in jsp:
从Mysql动态获取数据,以(下拉)Jsp中的select选项.这篇文章说明了如何从mysql数据库中获取数据并显示在Jsp的select选项元素中.您应该先阅读以下博文,然后再浏览本博文,即:
如何将Mysql数据库连接到jsp.
How to Connect Mysql database to jsp.
如何在MySql中创建数据库并将数据插入数据库. 使用以下数据库来说明从Mysql动态获取数据以(下拉)
How to create database in MySql and insert data into database. Following database is used, to illustrate ‘Dynamically Fetch data from Mysql to (drop down)
Jsp中的选择选项":
select option in Jsp’ :
id City
1 London
2 Bangalore
3 Mumbai
4 Paris
以下代码用于在MySql数据库中插入数据.使用的数据库是城市",用户名="root",密码也设置为"root".
Create Database city;
Use city;
Create table new(id int(4), city varchar(30));
insert into new values(1, 'LONDON');
insert into new values(2, 'MUMBAI');
insert into new values(3, 'PARIS');
insert into new values(4, 'BANGLORE');
这是从Mysql动态获取数据以(下拉)Jsp中的select选项的代码:
Here is the code to Dynamically Fetch data from Mysql to (drop down) select option in Jsp:
<%@ page import="java.sql.*" %>
<%ResultSet resultset =null;%>
<HTML>
<HEAD>
<TITLE>Select element drop down box</TITLE>
</HEAD>
<BODY BGCOLOR=##f89ggh>
<%
try{
//Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection connection =
DriverManager.getConnection
("jdbc:mysql://localhost/city?user=root&password=root");
Statement statement = connection.createStatement() ;
resultset =statement.executeQuery("select * from new") ;
%>
<center>
<h1> Drop down box or select element</h1>
<select>
<% while(resultset.next()){ %>
<option><%= resultset.getString(2)%></option>
<% } %>
</select>
</center>
<%
//**Should I input the codes here?**
}
catch(Exception e)
{
out.println("wrong entry"+e);
}
%>
</BODY>
</HTML>
这篇关于如何从数据库中获取下拉值并在jsp中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!