How to fetch the dropdown values from database and display in jsp

后端 未结 4 1991
谎友^
谎友^ 2020-12-19 18:43

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

4条回答
  •  Happy的楠姐
    2020-12-19 19:40

    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:

    <%@ page import="java.sql.*" %>
    <%ResultSet resultset =null;%>
    
    
    
        Select element drop down box
    
    
    
    
    <%
        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") ;
    %>
    
    

    Drop down box or select element

    <% //**Should I input the codes here?** } catch(Exception e) { out.println("wrong entry"+e); } %>

    enter image description here

提交回复
热议问题