简体   繁体   中英

How can I retrieve data to a drop down list box in my jsp file from a function

How can I retrieve data from SQL database (specifically from a column) to drop down list box in my JSP file? but I want to retrieve this data from a function in a .java file and the to the JSP list box so the order will be:

A function in my java file that retrieve the data from sql DB (specifically from a column) and the show it in my jps list box?

this will the function in java file

here is my code:

public String getc_Condicion(){
    String query = "";

    String c_condicion = new String();
        query = "select C_Condicion_Beca from Bca_Condicion_Beca";
        try{
            st = con.createStatement();
            rs = st.executeQuery(query);

            while (rs.next()){
                c_condicion = rs.getString("C_Condicion_Beca");
               }
        }
        catch(SQLException e){
        System.out.println("Error al obtener los datos: "+e.getMessage());

        }

    return c_condicion;
}

Are you looking for this? This may not be Best design wise though.

public String[] getcDataCondicion(){
    String query = "";

    String[] cDataCondicion= new String[100];
        query = "select C_Condicion_Beca from Bca_Condicion_Beca";
        try{
            st = con.createStatement();
            rs = st.executeQuery(query);
            int i=0;
            while (rs.next()){
                cDataCondicion[i] = rs.getString("C_Condicion_Beca");
                 i++;
               }
        }
        catch(SQLException e){
        System.out.println("Error al obtener los datos: "+e.getMessage());

        }

    return cDataCondicion;
}

<%  

  YourClass c= new YourClass();
String[] result=c.getcDataCondicion();
%>

<Select>
<%
  for(int i=0;i<result.length;i++){
%>
 <Option>result[i]</ Option>
<% } %>
</select>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM