0

Im trying to execute a query through a java program but it doesn't execute. here's the method

 public List<Usuario> darProveedores() throws Exception{
    PreparedStatement st=null;
    ArrayList<Usuario> respuesta = new ArrayList<Usuario>();
    String consulta ="SELECT u.direccion_electronica AS dirE, "
                    + "u.login AS login, "
                    + "u.palabra_clave AS clave, "
                    + "u.rol AS rol, "
                    + "u.tipo_persona AS tipoPer, "
                    + "u.documento_identificacion AS docID, "
                    + "u.nombre AS nombre, "
                    + "u.nacionalidad AS naci, "
                    + "u.direccion_fisica AS dirF, "
                    + "u.telefono AS tel, "
                    + "u.ciudad AS ciudad, "
                    + "u.departamento AS depto, "
                    + "u.codigo_postal AS codPostal "
                    + " FROM usuarios u "
                    + " WHERE u.rol='Proveedor' ";
    try{

        iniTemp();
        establecerConexion(cadenaConexion, usuario, clave);
        st = conexion.prepareStatement(consulta);
        ResultSet r= st.executeQuery(consulta);

        while(r.next()){

            String dirE= r.getString("dirE");
            String login = r.getString("login");
            String clave = r.getString("clave");
            String rol = r.getString("rol");
            String tipoPer = r.getString("tipoPer");
            String docID = r.getString("docID");
            String nombre = r.getString("nombre");
            String naci = r.getString("naci");
            String dirF = r.getString("dirF");
            String tel= r.getString("tel");
            String ciudad = r.getString("ciudad");
            String depto = r.getString("depto");
            String codPostal = r.getString("codPostal");

            Usuario u = new Usuario(login, dirE, clave, rol, tipoPer, Integer.parseInt(docID), nombre, naci, dirF, Integer.parseInt(tel), ciudad, depto, Integer.parseInt(codPostal));

            respuesta.add(u);
        }

    }
    catch(Exception e)
    {
        e.printStackTrace();

    }

    finally{
        if (st != null) 
        {
            try {
                st.close();
            } catch (SQLException exception) {

                throw new Exception("ERROR: ConsultaDAO: loadRow() =  cerrando una conexion.");
            }
        }
        closeConnection(conexion);
    }

    return respuesta;
}

I have executed the query on SQL Developer and it returns a table with values, but when i do it through here the while(r.next()) instruction says there are no rows in the answer

2
  • 1
    Probably you're connecting to the wrong database? Commented Apr 7, 2014 at 0:55
  • No, the "establecerConexion" method does that job and it works fine, i have used it in other queries that work fine Commented Apr 7, 2014 at 1:00

1 Answer 1

1

You don't need to use a PreparedStatement when there are no parameters. Just use Statement in place of PreparedStatement, and st = conexion.createStatement() to create it.

Sign up to request clarification or add additional context in comments.

1 Comment

I have done that, but still it doesn't enter the while()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.