Error: make a query(query) in dao which is an alias

Asked

Viewed 79 times

0

This query normally functions in postgresql:

select nome as "pessoas?" from pessoa

But in java I did a dao which has a method called find:

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

Gives the message:

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

PS.: I am 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);     }   
}
  • What is "test?"? What do you want with this question? Preparedstatement is understanding this as if it were a parameter.

  • 'test? ' is the name of the alias, I don’t know could be 'names? '. But you need to have the question I’m doing a test here...

  • You can add how you are declaring and executing the query by Preparedstatement?

  • @Diegof is there, I edited it. Look.

  • Still making a mistake?

  • When you remove the ? works?

  • Yes. even if it makes a mistake

  • Which version of postgres is using?

Show 3 more comments

1 answer

0


Does not use single quotes as escape of caractares in postgres but double quotes, in this case add a bar to escape in java.

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

To recover the database information use alias:

rs.getString("teste?")
  • I’ll test it here buddy. D

  • rray think will give problem even so, preparedStatement recognizes a ? as a parameter to be passed.

  • 1

    @Diegof is a good question hehe not sure but he should ignore this question, at least based on the error of the question.

  • : org.postgresql.util.Psqlexception: The name column name was not found in this Resultset. at dao.PessoaDAO.enncontrarAlias(Personal.java:161)

  • It looks like he’s been waiting...

  • 1

    @gonz at the time of taking the value of the bank you did so: pessoa.setNome(rs.getString("teste?")); ?

  • 1

    @gonz worked? just put the rs.getString("teste?") ?

  • 1

    before you write it I tested it here. it worked;

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.