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

后端 未结 4 1995
谎友^
谎友^ 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条回答
  •  清歌不尽
    2020-12-19 19:31

    1. Make the database connection and retrieve the query result.
    2. Traverse through the result and display the query results.

    The example code below demonstrates this in detail.

    <%@page import="java.sql.*, java.io.*,listresult"%> //import the required library
    
    <%
    
    String label = request.getParameter("label"); // retrieving a variable from a previous page
    
    Connection dbc = null; //Make connection to the database
    Class.forName("com.mysql.jdbc.Driver");
    dbc = DriverManager.getConnection("jdbc:mysql://localhost:3306/works", "root", "root");
    if (dbc != null) 
    {
        System.out.println("Connection successful");
    }
    
    ResultSet rs = listresult.dbresult.func(dbc, label); //This function is in the end. The function is defined in another package- listresult
    
    %>
    
    
    Label Name: ">
    //The function: public static ResultSet func(Connection dbc, String x) { ResultSet rs = null; String sql; PreparedStatement pst; try { sql = "select lname from demo where label like '" + x + "'"; pst = dbc.prepareStatement(sql); rs = pst.executeQuery(); } catch (Exception e) { e.printStackTrace(); String sqlMessage = e.getMessage(); } return rs; }

    I have tried to make this example as detailed as possible. Do ask if you have any queries.

提交回复
热议问题