Soheil
Soheil

Reputation: 627

Insert using PreparedStatement and return values of one column for inserted rows

I'm using PreparedStatement to batch insert into a MySQL database. Then I need the query to return the values of one specific column (name) for the successfully inserted rows. I have already tried:

preparedStatement = connection.prepareStatement("INSERT INTO x (id,name) VALUES (?,?)", new String[]{"name"});

However, I have not found any method to get the returned values from preparedStatement after it is run. I have tried preparedStatemet.getGeneratedKeys(); but it only returns the primary key values.

Upvotes: 0

Views: 884

Answers (1)

Basil Bourque
Basil Bourque

Reputation: 340118

Use the generated keys to find the inserted rows. In that query return any column you desire.

Stay within a single transaction to get unchanging results.

Upvotes: 1

Related Questions