开发者

Jdbc check type compatiblity

开发者 https://www.devze.com 2023-03-30 12:51 出处:网络
Is there some way to check beforehand if a java type is compatible with a sql.Types? I could type out all the stuff like:

Is there some way to check beforehand if a java type is compatible with a sql.Types?

I could type out all the stuff like:

if (BIGINT||TINYINT) try BigInteger.parse(myvalue) and if (TIME||DATE||TIMESTAMP) try new Date(Long.parse(myvalue))

This creates tonnes of code. Is开发者_如何学运维 their some generic way of prechecking?

EDIT: I'm talking about inserts, so I want to do some prechecking, to avoid sql exceptions while inserting


If you're using JDBC, just use ResultSet.getObject(). It will return what type is appropriate (let the JDBC driver do the heavy lifting).

Only use the typed getters, eg getInt(), when you know what you're expecting from the column.


Maybe I'm not understanding your question. Do you mean instanceof?

ie. if (myObject instanceof java.sql.Types) {
        ...
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消