Insert into multiple tables with jdbc

I am trying to use JDBC to insert into multiple tables. Since it should be fast, I want to use the PreparedStatementand method executeBatch. Tables are combined with a foreign key relationship.

  • The first idea was to use getGeneratedKeys(), but this failed with some JDBC drivers. For instance. PostgreSQL

  • The second idea was to use the SQL-currval (...) function. But you need to call the execute command for one statement, and for another it makes all the keys the same. Thus, this method also fails.

  • JDBC does not accept comma-inserted inserts.

How can i achieve this?

+3
source share
3 answers

PostgreSQL. , PostgreSQL JDBC 8.4-701 PreparedStatement#getGeneratedKeys() .

, :

statement = connection.prepareStatement(SQL, Statement.RETURN_GENERATED_KEYS);

, . JDBC.


- JDBC "" "Hibernate" JPA. - Java .. DB.

+1

, /, , .

.

0

, .

, getGeneratedKeys(), .

, .

Postgres 9.0:

  • "SELECT nextval('public.\"table_column_seq\"') FROM generate_series(1,"+pollsize+")");
  • .

, , , - . JDBC , - YouVeGotaF ** ckedUpDriverException.

0
source

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


All Articles