Error trying to insert data to Postgresql

Asked

Viewed 352 times

1

I’m having trouble entering information to the BD. I believe the error is in the DAO layer more specifically in the insert function. You could help me find this mistake?

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;

    }
}

Personal 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\8.2\executor-snippets\run.xml:53:
Java returned: 1
FALHA NA CONSTRUÇÃO (tempo total: 0 segundos)
  • And what’s the bug? Where’s the error stack? Add that to the question.

  • 1

    I believe you don’t have that set in insert into pessoa set.

  • I removed the set and generated the error:Exception in thread "main" org.postgresql.util.Psqlexception: No results were returned by the query.

  • Ai is already a completely different error from the question asked, the error of the question has been answered.

  • @elbenevides In this case you should open a new question with the new problem. Btw, you can always mark an answer as correct in your questions, just use the V on the left side of the response.

1 answer

2


The syntax of insert is incorrect. Error tells exactly which part is wrong.

ERROR: syntax error at or near "set"

It’s like this

st1 = conn.prepareStatement("insert into pessoa set (...) values (...)");

and should be without the word SET

st1 = conn.prepareStatement("insert into pessoa (...) values (...)");

Browser other questions tagged

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