在jsp中从数据库加载下拉选项

时间:2016-05-15 01:17:17

标签: java jsp

我在从数据库中加载jsp表单中的下拉菜单时遇到了一些问题。这是我使用的课程:

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;

    }

}

usebean是:

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

下拉列表如下:

<select>

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

</select>

我无法编译该文件并收到此错误消息:

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

我做错了什么???

1 个答案:

答案 0 :(得分:0)

您可以检查两件事:
1.你的jstl jar文件是否在classpath中?如果您没有使用任何依赖管理系统,可以将它放在WEB-INF / lib中 2.如果jstl jar文件在类路径中,那么检查你是否在jsp页面顶部包含了jstl声明:

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