Help Netbeans java error in cycle for [closed]

0

Hi, I have the following problem, in my university I opened the same project and it worked correctly, but in my house, I released this error.

JSP:

<%@page import="controlador.AccesoDatos"%>

<%@page import="modelo.*"%>

<%@page import="java.util.ArrayList"%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>



<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>LISTADO DE LIBROS</title>
    </head>
    <body>
        <% 
        AccesoDatos a = new AccesoDatos();
        %>
        <h1 align="center">Libros Almacenados</h1>

        <table align="center"   border="1">
            <tr>
                <th>CODIGO</th>
                <th>AUTOR</th>
                <th>NOMBRE</th>
                <th>ESTADO</th>
            </tr>
            <%
            ArrayList<Libro> listL = new ArrayList();
            listL = a.listarLibros();
            for(Libro lib : listL){
                out.println("<tr>");
                   out.println("<td>"+lib.getCodigo()+"</td>");
                   out.println("<td>"+lib.getAutor()+"</td>");
                   out.println("<td>"+lib.getNombre()+"</td>");
                   out.println("<td>"+lib.getEstado()+"</td>");
                out.println("</tr>");
            }
            %>
        </table>



    </body>
</html>

The code of AccesoDatos :

public class AccesoDatos {
private  ResultSet rs = null;
private Connection con = null;
private Statement sentencia = null;

private ArrayList<Comuna> listC;
private ArrayList<Libro> listL;
private ArrayList<Pelicula> listP;
private ArrayList<Vendedor> listV;

private Comuna com;
private Libro lib;
private Pelicula peli;
private Vendedor vend;


private void conectar (){
    try{
        String parametros = "jdbc:mysql://localhost:3306/libros?zeroDateTimeBehavior=convertToNull";
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection(parametros,"root","");

    }catch(SQLException e){
        System.out.print("No se ha podido conectar");
    }catch(Exception e){
        System.out.print("No se ha podido conectar");
    }
}

private void desconectar (){
    try{
       con.close();
    }catch(SQLException e){
        System.out.print("No se ha podido desconectar");
    }catch(Exception e){
        System.out.print("No se ha podido desconectar");
    }
}

public ArrayList<Libro> listarLibros(){
    try{
        conectar();
        sentencia = con.createStatement();
        String sql = "select * from libro";
        sentencia.executeQuery(sql);
        listL = new ArrayList();
        while(rs.next()){
            int codigoL = rs.getInt("cod_lib");
            String autorL = rs.getString("aut_lib");
            String nombreL = rs.getString("nom_lib");
            String estadiL = rs.getString("est_lib");
        lib = new Libro(codigoL,autorL,nombreL,estadiL);    
        listL.add(lib);
        }
        sentencia.close();
        desconectar();
        return listL;

    }catch(SQLException e){
        return null;
    }catch(Exception e){
        return null;
    }
}
    
asked by Mick Foley 24.06.2016 в 21:15
source

2 answers

3

It seems that you are entering the catch of listing Books test by adding these lines to list Data Access Books.

catch(SQLException e){
    e.printStackTrace();
    System.out.println(e);
    return null;
}catch(Exception e){
    e.printStackTrace();
    System.out.println(e);
    return null;
}

and check the messages that appear in the console, the problem may be related when connected to the base. Greetings

    
answered by 24.06.2016 в 21:40
-1

I managed to find the solution, I made the Data Access javaclass from 0 and the error disappeared. Thanks for the help, I'm like this.

public class AccesoDatos {
private  ResultSet rs = null;
private Connection con = null;
private Statement sentencia = null;

private ArrayList<Comuna> listC;
private ArrayList<Libro> listL;
private ArrayList<Pelicula> listP;
private ArrayList<Vendedor> listV;

private Comuna com;
private Libro lib;
private Pelicula peli;
private Vendedor vend;


private void conectar (){
    try{
        String parametros = "jdbc:mysql://localhost:3306/libros?zeroDateTimeBehavior=convertToNull";
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection(parametros,"root","");

    }catch(SQLException e){
        System.err.println("ERROR DE CONEXION A LA BD");
    }catch(Exception e){
        System.err.println("ERROR DE CONEXION ");
    }
}

private void desconectar (){
    try{
       con.close();
    }catch(SQLException e){
        System.err.println("ERROR DE CONEXION A LA BD");
    }catch(Exception e){
        System.err.println("ERROR DE CONEXION ");
    }
}


public ArrayList<Libro> listarLibro(){
    try{
        conectar();
        String sql = "select * from libro";
        sentencia = con.createStatement();
        rs = sentencia.executeQuery(sql);
        listL = new ArrayList();
        while(rs.next()){
       int codigoL = rs.getInt("cod_lib");
            String autorL = rs.getString("aut_lib");
            String nombreL = rs.getString("nom_lib");
            String estadiL = rs.getString("est_lib");

            lib = new Libro(codigoL,autorL,nombreL,estadiL);
            listL.add(lib);

        }
        sentencia.close();
        desconectar();
        return listL;
    }catch(SQLException e){
        return null;
    }catch(Exception e){
        return null;
    }
}    
    
answered by 24.06.2016 в 23:07