简体   繁体   中英

Query in java does not work

I try to run a query from java, i run the query in the postgresql editor and this work, but when running the application does not work, to run the query returns no data

this is the code:

private synchronized Perfil consultarPerfil(String nombre) {
    Perfil perfil = null;
    String sql = "SELECT * FROM PERFIL WHERE NOMBRE = ?";
    Connection connection = conexion.getConnection();
    try {
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1, nombre);
        ResultSet rs = statement.executeQuery();
        while (rs.next()) {
            perfil = new Perfil(rs.getString("nombre"), rs.getString("descripcion"), rs.getBoolean("estado"));
        }
    } catch (SQLException ex) {
        Logger.getLogger(ColeccionPerfiles.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        conexion.close(connection);
    }
    return perfil;
}

add this : after = like this

String sql = "SELECT * FROM PERFIL WHERE NOMBRE =: ?";

Note: the parameter must have a same data type as the field "nombre" has

If column NOMBRE in table PERFIL is a NUMBER, then when you try to set your parameter as Int like this:

 statement.setInt(1, nombre);

Instead of

statement.setString(1, nombre);

Regards,

I try adding : after = but show exception, NOMBRE is string type and I have a database only. The connection works correctly.

I could run the query sometimes from java, but then stopped working and the debugger does not display errors. Restart the computer and clean the cache does not work netbeans.

I are spending a lot of mistakes of this kind lately with java.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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