jsp中JAVA代码取select值,如何从数据库中获取下拉值并在jsp中显示
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 dis
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
解决方案
how to fetch the dropdown values from database and display in jsp:
Dynamically Fetch data from Mysql to (drop down) select option in Jsp. This post illustrates, to fetch the data from the mysql database and display in select option element in Jsp. You should know the following post before going through this post i.e :
How to Connect Mysql database to jsp.
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)
select option in Jsp’ :
id City
1 London
2 Bangalore
3 Mumbai
4 Paris
Following codes are used to insert the data in the MySql database. Database used is “City” and username = “root” and password is also set as “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');
Here is the code to Dynamically Fetch data from Mysql to (drop down) select option in Jsp:
Select element drop down boxtry{
//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") ;
%>
Drop down box or select element
//**Should I input the codes here?**
}
catch(Exception e)
{
out.println("wrong entry"+e);
}
%>
更多推荐
所有评论(0)