如何从数据库中获取下拉值并在jsp中显示:
动态从Mysql中获取数据到(下拉)选择Jsp中的选项.这篇文章说明了从mysql数据库中获取数据并在Jsp中的select选项元素中显示.在阅读这篇文章之前你应该知道以下帖子,即:
如何将Mysql数据库连接到jsp.
如何在MySql中创建数据库并将数据插入数据库.
使用以下数据库,说明’从Mysql动态获取数据到(下拉)
在Jsp’中选择选项:
id City
1 London
2 Bangalore
3 Mumbai
4 Paris
以下代码用于在MySql数据库中插入数据.使用的数据库是“City”,username =“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');
以下是在Jsp中从Mysql动态获取数据到(下拉)选择选项的代码:
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);
}
%>