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
asked Apr 7, 2014 at 0:53
JsMartinez
3411 gold badge5 silver badges17 bronze badges
-
1Probably you're connecting to the wrong database?gerrytan– gerrytan2014年04月07日 00:55:30 +00:00Commented 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 fineJsMartinez– JsMartinez2014年04月07日 01:00:14 +00:00Commented Apr 7, 2014 at 1:00
1 Answer 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.
answered Apr 7, 2014 at 0:55
Dawood ibn Kareem
80.2k15 gold badges103 silver badges112 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
JsMartinez
I have done that, but still it doesn't enter the while()
default