Psqlexception: No results returned by query

Asked

Viewed 568 times

0

I cannot identify the error that is returning when trying to insert data to the BD.

I believe the error is in the insert function.

The error returned is:

Exception in thread "main" org.postgresql.util.Psqlexception: No results returned by query.

public class ConnectionFactory {

    public static Connection  getConnectionFactory() throws SQLException, ClassNotFoundException{

           String url = "jdbc:postgresql://localhost:5432/topografiaJava";  
           String usuario = "postgres";  
           String senha = "Maker@1";  
           Class.forName("org.postgresql.Driver");  
           Connection con;  
           con = DriverManager.getConnection(url, usuario, senha);  
           System.out.println("Conexão realizada com sucesso.");  
           Statement st1;  
           st1 = con.createStatement();
           return con;

    }
}
public class PessoaDAO {

private Connection conn;

private Pessoa resultSet2Model(ResultSet rs) throws SQLException, ClassNotFoundException {

     Pessoa p;
     p = new Pessoa(rs.getInt("id"),
             rs.getString("nome"),
             rs.getString("matricula"),
             rs.getString("setor"),
             rs.getString("login"),
             rs.getString("senha"),
             rs.getString("email"));
        return p; 
    }

    public Pessoa inserir(Pessoa p) throws SQLException, ClassNotFoundException {
        PreparedStatement st1=null;
        this.conn = new ConnectionFactory().getConnectionFactory();
        st1 = conn.prepareStatement("insert into pessoa (id, nome, matricula, setor, login, senha, email) values(?,?,?,?,?,?,?)");
         st1.setInt(1,p.getID);
         st1.setString(2, p.getNome());
         st1.setString (3, p.getMatricula());
         st1.setString(4, p.getSetor());
         st1.setString(5, p.getLogin());
         st1.setString(6,p.getSenha());
         st1.setString(7, p.getEmail());
         ResultSet rs1 = st1.executeQuery();
         rs1.next();
         st1.execute();
         return p;
    }
}
  • 3

    I recommend you read this topic: How Try-with-Resources works?

  • 2

    The two lines before return p; are completely disposable in this code, and insert does not return given, so, ResultSet is unnecessary for this type of query.

1 answer

3


Note this line of code

rs1.next();

The method next of ResultSet serves to move the "cursor" to the next line returned by the bank. As a insert does not return anything, it is popping this error.

Let’s get to the problems of your code:

In the block below you try to run a query twice in a row

ResultSet rs1 = st1.executeQuery(); // Aqui
rs1.next();
st1.execute(); // Aqui

The method executeQuery serves to execute a query in the database and tries to return an object of the type ResultSet.

Already the method execute serves to execute any statement and returns true if the result is an object of the type ResultSet or false if the first result is a count of update (how many lines were changed) or if there was no result.

Again. A insert does not return something, then you can just discard the first two lines.

Reorganize that code block to stay only

st1.execute();
  • The method execute() returns true if the result of the instruction is a Resultset; and returns false if the result of the instruction is a number of altered/inserted/deleted lines or if it has no result.

  • @mutlei is, I just saw in the V9 documentation

Browser other questions tagged

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