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;
}
}
I recommend you read this topic: How Try-with-Resources works?
– user28595
The two lines before
return p;
are completely disposable in this code, andinsert
does not return given, so,ResultSet
is unnecessary for this type of query.– user28595