我正在格式化 ResultSet
以输出到 CSV 文件。因此,我真的不关心结果集的 Java 类型,除了知道它是文本还是数字之外。
JDBC 是否保证 getString 始终给出值的字符串表示形式,至少对于单个值(我不需要担心 java.sql.Types.ARRAY
,java .sql.Types.JAVA_OBJECT
和其他一些)。
例如给定的 resultSetMetaData.getColumnType(i)
是 Types.FLOAT
或 Types.BIGDECIMAL
。 rs.GetString(i)
总是会产生一些 String 吗?
即当 getXXX 给我值时,是否存在 getString 会抛出 SQLException 或返回 null 的情况?
最佳答案
是的,检查一下:http://java.sun.com/docs/books/tutorial/jdbc/basics/retrieving.html
JDBC allows a lot of latitude as far as which getXXX methods you can use to retrieve the different SQL types. For example, the method getInt can be used to retrieve any of the numeric or character types. The data it retrieves will be converted to an int; that is, if the SQL type is VARCHAR , JDBC will attempt to parse an integer out of the VARCHAR. The method getInt is recommended for retrieving only SQL INTEGER types, however, and it cannot be used for the SQL types BINARY, VARBINARY, LONGVARBINARY, DATE , TIME, or TIMESTAMP.
但要小心,不同的 JDBC 驱动程序可能会产生不同的结果。
关于jdbc - JDBC ResultSet getString 是否始终返回字符串表示形式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2071845/