简体   繁体   中英

Load dropdown options from database in jsp

I'm having a little bit of a problem while loading a dropdown menu in a form in jsp from a database. This is the class I'm using:

package Carros;

import java.sql.*;
import java.util.List;
import java.util.Scanner;
import java.util.ArrayList;
/**
 *
 * @author luis.moran
 */
public class DatosVehiculos {
    Connection conn = null;
    private List<String> list;

    public DatosVehiculos(){
        CargarDriver();
    }

    public void CargarDriver(){
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            System.out.println("Conexion exitosa");
        } catch(Exception e){
            System.out.println("Conexion fallida");
            e.getStackTrace();
        }
    }

    public void conectarBaseDatos(){
        String url = ("jdbc:oracle:thin:@instanciadb.cl3tiuy2smr4.us-west-2.rds.amazonaws.com:1521:ORCLJAVA");
        String user = "proyectos";
        String pass = "portaldejava";

        try {
            conn = DriverManager.getConnection(url, user, pass);
            System.out.println("Conexion exitosa con base de datos");
        } catch(Exception e){
            System.out.println("Conexion fallida con base de datos");
            e.getStackTrace();
        }
    }

    public List<String> getList(Vehiculos datos){

        Statement stmt = null;
        ResultSet rs = null;
        conectarBaseDatos();
        list = new ArrayList<String>();

        try {
            String query = "";
            query = "select * from modelo";

            stmt = conn.createStatement();
            rs = stmt.executeQuery(query);

            while(rs.next()){
                list.add(rs.getString("marca"));
            }

        } catch(Exception e){
            System.out.println(e.getMessage());
        } finally {
            try {
                if(stmt != null){
                    stmt.close();
                }
                if(conn != null){
                    conn.close();
                }
            } catch(SQLException ex){
                ex.getStackTrace();
            }
        }

        return list;

    }

}

the usebean is:

<jsp:useBean id="buscarCombos" scope="page" class="Carros.DatosVehiculos" />

The dropdown is the following:

<select>

     <c:forEach var="item" items="${buscarCombos.getList(datos)}">
          <option>${item}</option>
     </c:forEach>

</select>

I am unable to compile the file and getting this error message:

error: package org.apache.taglibs.standard.tag.rt.core does not exist

what am I doing wrong???

There are two things that you can check:
1. Is your jstl jar file in classpath? You can put that in WEB-INF/lib if you are not using any dependency management system.
2. If jstl jar file is in the classpath, then check if you have included the jstl declaration like so on top of the jsp page:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

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