PSQLException: No result returned by query

0

I can not identify the error that is returning when trying to insert data into the DB.

I believe the error is in the insert function.

The error returned is:

  

Exception in thread "main" org.postgresql.util.PSQLException: No result 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;
    }
}
    
asked by anonymous 15.02.2018 / 14:20

1 answer

3

Note this line of code

rs1.next();

The method next of ResultSet is 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 troubleshoot your code:

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

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

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

The method execute serves to execute any statement and returns true if the result is an object of 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 , so you can simply dismiss the first two lines.

Rearrange that block of code to only stay

st1.execute();
    
15.02.2018 / 14:28