A.Dumas
A.Dumas

Reputation: 3277

JDBI: Long/Interger exception when inserting data

I want to insert a row into a maraiDB database with JDBI/Dropwizard.

My table was generated with

CREATE TABLE parameter (
  job_id INT  references job(id),
  name VARCHAR(150) NOT NULL,
  content VARCHAR(150) NOT NULL
);

and use an interface to insert data in to the database

  @SqlUpdate("INSERT INTO parameter (job_id, name , content) VALUES " +
 "(:job_id, :name , :content)")
  long insert(
      @Bind("job_id") int job_id, 
      @Bind("name") String name, 
      @Bind("content") String content);

now when I call the method via

private final ParameterJDBI parameterJDBI;
public void insert() {

      parameterJDBI.insert(1, "name", "value");

  }

I get an error

ERROR [2018-06-14 15:39:46,083] io.dropwizard.jersey.errors.LoggingExceptionMapper: Error handling a request: 700d318fa5724df6
! java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long

I also changed the signature to long and changed the first argument to 1L but the error still persist. I dont understand where the long object comes from.

Upvotes: 2

Views: 692

Answers (1)

Spasa Mihajlovic
Spasa Mihajlovic

Reputation: 171

You are using @SqlUpdate which returns an int(the number of updated/inserted rows).So, your method declaration should return int or void.

If you want to return the key(s) generated from the SQL statement, then you should add @GetGeneratedKeys

Upvotes: 2

Related Questions