Question

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?

Was it helpful?

Solution

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top