简体   繁体   English

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

[英]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. 从数据库以jsp的形式加载下拉菜单时,我遇到了一些问题。 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: usebean是:

<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? 1.您的jstl jar文件是否在classpath中? You can put that in WEB-INF/lib if you are not using any dependency management system. 如果不使用任何依赖项管理系统,则可以将其放在WEB-INF / lib中。
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: 2.如果jstl jar文件位于类路径中,请检查是否在jsp页面顶部包括了jstl声明:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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