Error: make a query in dao that is an alias

0

This query normally works in postgreSQL:

select nome as "pessoas?" from pessoa

But in java I made a dao that has a method called findAlias:

sql = "select nome as 'teste?' from pessoa where id=?";

Give the message:

  

ERROR: syntax error at or near "'test?'"

PS: I'm using PreparedStatement.

@Override
    public Pessoa enncontrarAlias(int id) {
    Pessoa pessoa = null;

    sql = "select nome as \"teste?\" from pessoa where id=?";

    try {
        pst = conexao.prepareStatement(sql);
        pst.setInt(1, id);
        rs = pst.executeQuery();

        while(rs.next()){
            pessoa = new Pessoa();
            pessoa.setNome(rs.getString("nome"));
        }

        rs.close();

        return pessoa;

    } catch (SQLException e) {

         throw new RuntimeException(e);     }   
}
    
asked by anonymous 12.04.2016 / 17:16

1 answer

0

Do not use single quotation marks as a escape of characters in postgres, but double quotation marks, in that case add a slash to escape in java.

sql = "select nome as \"teste?\" from pessoa where id=?";

To retrieve the database information use the alias:

rs.getString("teste?")
    
12.04.2016 / 17:18