Reputation: 3133
I'm now trying to call a sproc that returns scalar value which represents the inserted row's ID (which is varchar), how could this be achieved in JDBC? What I'm now doing is let the DB sproc return result like this
select SCOPE_IDENTITY()
and in my code I use ResultSet
ResultSet resultSet = statement.executeQuery();
resultSet.next();
String productId = resultSet.getString(1);
Am I doing it correctly?
Upvotes: 2
Views: 4746
Reputation: 136022
It's preferrable to retrieve generated IDs with JDBC as
PreparedStatement ps = conn.prepareStatement(insertStatement, Statement.RETURN_GENERATED_KEYS);
ps.executeUpdate();
ResultSet rs = ps.getGeneratedKeys();
rs.next();
String id = rs.getString(1);
Upvotes: 4