I'm having trouble entering information into DB. I believe the error is in the DAO layer more specifically in the insert function. Could you help me find this error?
CONNECTIONFACTORY
package DAO;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConnectionFactory {
public static Connection getConnectionFactory() throws SQLException, ClassNotFoundException{
String url = "jdbc:postgresql://localhost:5432/topografiaJava";
String usuario = "postgres";
String senha = "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;
}
}
PersonaDAO class
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 set (id, nome, matricula, setor, login, senha, email) values(?,?,?,?,?,?,?)");
st1.setInt(1, 2);
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;
}
Error:
Exception in thread "main" org.postgresql.util.PSQLException: ERROR: syntax error at or near "set" Posição: 20 at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2422) at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2167) at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:306) at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:441) at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:365) at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:155) at org.postgresql.jdbc.PgPreparedStatement.executeQuery(PgPreparedStatement.java:118) at DAO.PessoaDAO.inserir(PessoaDAO.java:55) at DAO.TestaDAO.main(TestaDAO.java:28) C:\Users\eduar\AppData\Local\NetBeans\Cache.2\executor-snippets\run.xml:53: Java returned: 1 FALHA NA CONSTRUÇÃO (tempo total: 0 segundos)