Java – is there any way to extract the primary key (or ROWID) using namedparameterjdbctemplate and generatedkeyholder?

I tried to extract ROWID or primary key using spring's namedparameterjdbctemplate and generatedkeyholder

I want to do such a thing

MapsqlParameterSource parameters = new MapsqlParameterSource()
                .addValue("param1",value1)
                .addValue("param2",value2);
KeyHolder keyHolder = new GeneratedKeyHolder();
namedParameterJdbcTemplate.update("INSERT INTO TABLE(ID,col1,col2)"
                + "VALUES(TABLE.TABLE_SEQ.NEXTVAL,:param1,:param2)",parameters,keyHolder);

After executing the above query, when I try to execute keyholder getKey(). When longvalue(), it throws an exception

HTTP Status 500 - Request processing Failed; nested exception is org.springframework.dao.DataRetrievalFailureException: The generated key is not of a supported numeric type. Unable to cast [oracle.sql.ROWID] to [java.lang.Number]

When I pass this http://docs.oracle.com/cd/B28359_01/java.111/b31224/datacc.htm I understand (I hope I do) that ojdbc does not map Oracle ROWID to Java ROWID

Anyone can suggest any way to extract the key? Yes, it can be done using Preparedstatement, but it makes my code a little ugly to read and operate under certain conditions Thank you very much for your suggestion

Solution

You must use it

namedParameterJdbcTemplate.update("INSERT INTO TABLE(ID,col2)"
            + "VALUES(TABLE.TABLE_SEQ.NEXTVAL,keyHolder,new String[]{"ID"});
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>