java - 如何在不知道列类型的情况下获取列的值

标签 java mysql database servlets

我正在根据用户选择要在结果中显示的字段动态创建查询。问题是,当要从数据库中检索数据时,我通常使用 getXXX() 方法来检索适当的类型。但在这种情况下,我不知道合适的类型是什么,因为列是随机选择的。

有什么方法可以在不指定数据类型 XXX 的情况下获取字符串格式的数据?我正在使用 MySQL 和 Servlet。

最佳答案

您可以使用通用的 getObject方法:

Gets the value of the designated column in the current row of this ResultSet object as an Object in the Java programming language.

This method will return the value of the given column as a Java object. The type of the Java object will be the default Java object type corresponding to the column's SQL type, following the mapping for built-in types specified in the JDBC specification. If the value is an SQL NULL, the driver returns a Java null.

关于java - 如何在不知道列类型的情况下获取列的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15310549/

相关文章:

database - 使用 Perl,如何使用 dbh csv_tables 从标准输入读取 CSV 字符串?

java - 如何为 SpringJUnit4ClassRunner.class 以外的类加载 @ContextConfiguration?

java - 使用 thrift 在 java 生成的文件中额外的 boolean 集字段

java - 为不同项目创建聚合 POM

sql - 摆脱 STI - SQL 将单个表分解为新的多表结构

MySQL:如何在存储过程中使用 select 设置局部变量?

java - 我需要 JDK 还是只需要 JRE?

mysql - Bash 脚本,其变量来自数组中的不同文件

database - 动态Word文档

python - Pandas "read_sql"UnicodeDecodeError