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?

有帮助吗?

解决方案

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);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top