Frage

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?

War es hilfreich?

Lösung

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);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top