SQLException: No value specified for parameter 1

There is no such method as Connection() and getPreparedStatement() on java.sql.Connection.

conn.Connection();
stmt = conn.getPreparedStatement("select id_usuario, id_grupo from usuarios_grupos where id_grupo ='" + var + "'");

The conn is clearly a homegrown wrapper around JDBC code. Your particular problem is likely caused by the code behind the getPreparedStatement() method. It's apparently appending a ? to the SQL string before delegating through to the real connection.prepareStatement() method.

You probably don't want to hear this, but your JDBC approach is totally broken. This design indicates that the JDBC Connection is hold as a static or instance variable which is threadunsafe.

You need to totally rewrite it so that it boils down to the following proper usage and variable scoping:

public List<UsuariousGrupos> select(Integer idGrupo) throws SQLException {
    Connection connection = null;
    PreparedStatement statement = null;
    ResultSet resultSet = null;
    List<UsuariousGrupos> usuariousGrupos = new ArrayList<UsariousGrupos>();

    try {
        connection = database.getConnection();
        statement = connection.prepareStatement("select id_usuario, id_grupo from usuarios_grupos where id_grupo = ?");
        statement.setInt(1, idGrupo);
        resultSet = statement.executeQuery();

        while (resultSet.next()) {
            usuariousGrupos.add(mapUsuariousGrupos(resultSet));
        }
    } finally {
        if (resultSet != null) try { resultSet.close(); } catch (SQLException ignore) {}
        if (statement != null) try { statement.close(); } catch (SQLException ignore) {}
        if (connection != null) try { connection.close(); } catch (SQLException ignore) {}

    }

    return usuariousGrupos;
}

See also:

  • How to declare a global static class in Java?

Unrelated to the concrete question, you've another problem. The following exception

javax.el.ELException: /index.xhtml @61,99 value="#{usuariousGruposBean.listOfUserGroups}": Error reading 'listOfUserGroups' on type br.view.UsuariousGruposBean

indicates that you're doing the JDBC stuff inside a getter method instead of (post)constructor or (action)listener method. This is also a very bad idea because a getter can be called more than once during render response. Fix it accordingly.

See also:

  • Why JSF calls getters multiple times

Usually you get this kind of error when using prepared statements and forgot to set the parameter with index 1.

In this case, you are using prepared statements but there is nothing to prepare, you make the query string by hand.

Also, you may run into additional problems because you're concatenating an Integer between aposthrophes. Numeric values go without them.

So, it should be like this:

stmt = conn.getPreparedStatement("select id_usuario, id_grupo from usuarios_grupos where id_grupo = " + var + ";");

But actually you should use something like getStatement() or use the getPreparedStatement() correctly (place a ? at the position of var and setInteger() to place it in.

So the final solution would be:

stmt = conn.getPreparedStatement("select id_usuario, id_grupo from usuarios_grupos where id_grupo = ?;");
stmt.setInt(1, var);

Tags:

Java

Jdbc

Jsf