0

I have the following table in MYSQL:

  CREATE TABLE 't001_prospeccao' (
  'pro_id' bigint(20) NOT NULL AUTO_INCREMENT,
  'pro_cliente' bigint(20) NOT NULL,
  'pro_produto' bigint(20) NOT NULL,
  'pro_status' int(11) DEFAULT NULL,
  'pro_vlr_entabulado' double DEFAULT NULL,
  'pro_vlr_contratado' double DEFAULT NULL,
  'pro_vlr_utilizado' double DEFAULT NULL,
  'pro_obs' varchar(255) DEFAULT NULL,
  'pro_dt_visita' datetime DEFAULT NULL,
  'pro_dt_originacao' datetime DEFAULT NULL,
  'pro_protocolo' double DEFAULT NULL,
  'pro_correio' double DEFAULT NULL,
  'pro_dt_status' datetime DEFAULT NULL,
  'pro_funci' bigint(20) DEFAULT NULL,
  PRIMARY KEY ('pro_id'),
  UNIQUE KEY 'id_UNIQUE' ('pro_id')
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=latin1

And the following INSERT statement, which executes normally in MySQL console:

INSERT INTO t001_prospeccao (pro_cliente,pro_produto) VALUES (123456789,111)

But, on running this code in Eclipse, an error occours:

 String sql = "INSERT INTO t001_prospeccao (pro_cliente,pro_produto) VALUES (?,?);";

            PreparedStatement stmt = connection.prepareStatement(sql);

            try {
                stmt.setLong(1, p.getCliente().getMci());
                stmt.setLong(2, p.getProduto().getCod());

                stmt.execute(sql);

            } catch (SQLException e) {
                throw new RuntimeException(e);
            } finally {
                stmt.close();
            }

Error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?,?)' at line 1

Data from stmt object:

INSERT INTO t001_prospeccao (pro_cliente,pro_produto) VALUES (123456789,111)

What should I doing wrong? Thanks in advance.

1
  • This is not exactly "running in Eclipse". Eclipse has database tools that communicate with SQL databases, that would be "running in Eclipse". This is just Java code (unless there would be different behaviour when executing the code from the IDE and the command line). Commented Feb 6, 2012 at 13:43

1 Answer 1

4

You are using Statement.execute(String) where you should use the PreparedStatement.execute() method:

stmt.execute(sql);

That line should be:

stmt.execute();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.