How to get generated keys with regular dbutils?

I don't understand how to get automatically generated keys with commons-dbutils?

+6
source share
2 answers

You can use QueryRunner#insert() . The following is an example. For a table called users that has an auto-generated primary key column and a varchar column called username , you can do something like this:

 DataSource dataSource = ... // however your app normally gets a DataSource QueryRunner queryRunner = new QueryRunner(dataSource); String sql = "insert into users (username) values (?)"; long userId = queryRunner.insert(sql, new ScalarHandler<Long>(), "test"); 
+7
source

In fact, I think this is not possible with the current version of common-dbutils. A few months ago, when I worked for another company, I expanded QueryRunner with my own implementation.

The request was sent to the DbUtils project, and there you can even find a viable implementation that I think you could copy if you really need it.

https://issues.apache.org/jira/browse/DBUTILS-54

+3
source

Source: https://habr.com/ru/post/904941/


All Articles