- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我有以下问题。我有一个通过 jdbc 连接到 Redshift-DB 的 Java 应用程序。执行一个简单的 SELECT 查询会产生一个不为空的结果集(我检查过)。
现在,当我尝试从此 ResultSet 中提取数据时,该方法有时会返回 null,即使之前的 ResultSet.wasnull() 返回 false 并且数据库中的手动检查也显示有问题的记录不包含该属性的空值。
示例代码:
private void test(){
ResultSet rs = null;
ResultSetMetaData rsmd = null;
String getTable = "SELECT * from myTable"; //
try(PreparedStatement prep = myConnection.prepareStatement(getTable
, ResultSet.TYPE_FORWARD_ONLY
)){
rs = prep.executeQuery();
rsmd = rs.getMetaData();
int columns = rsmd.getColumnCount();
while(rs.next()){ // loop over all rows
for(int col = 1; col <= columns; col++){
if(rs.wasNull())
System.out.println("NULL Value detected!");
System.out.println("Column Name: " + rsmd.getColumnName(col));
System.out.println("Column value: " + rs.getString(col));
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
这在大多数情况下都有效,除了一些记录,rs.getString(col) 只会导致空值(即使数据库中的数据类型是 CHAR 或 VARCHAR)。这不一定对所有列都发生(我已经看到有几列被正确读取和打印,直到最终出现 null,即使 wasnull() 返回 false。
到目前为止,我已经观察到 ResultSet.getString()、ResultSet.getDate() 和 ResultSet.getTimestamp() 的这种行为,但在提取任何类型的数字(getInt()...)时从未观察到。在所有这些情况下,我检查了数据库以确保 jdbc 方法与数据类型匹配。
我尝试使用不同的驱动程序版本,我尝试使用 rs.getBytes(col) 代替,但结果保持不变:我就是无法摆脱那些空值。
我还成功地进行了卸载操作,将麻烦的表卸载到 S3 并手动检查导出的 csv 文件。 UNLOAD 运行没有任何问题,CSV 对我来说看起来很好(所有值都符合预期)。
现在我当然可以通过跳过它们出现的记录来手动处理那些“不需要的 NULL”,但我宁愿避免在不了解其原因的情况下像这样掩盖这个错误。
所以我的问题是:
我的代码有没有根本性的错误?jdbc/Redshift 是否存在可能导致此问题的已知问题?有人可以向我指出一些资源或给我一些关于我还可以尝试什么的想法吗?
最佳答案
来自 ResultSet.wasNull()
的 javadoc :
Reports whether the last column read had a value of SQL
NULL
. Note that you must first call one of the getter methods on a column to try to read its value and then call the methodwasNull
to see if the value read was SQLNULL
.Returns:
true
if the last column value read was SQLNULL
and false otherwise
换句话说,您应该在阅读专栏后使用它。请注意,对于对象类型,调用 wasNull
是没有意义的。 ,因为对象 getter 已经返回 null
.
此方法旨在在使用原始 getter(如 getInt()
)读取列时使用,因为它们将返回 0
。对于 null
值。然后您可以使用 wasNull()
检查该值是否真的是 0
或者实际上 null
.
换句话说,将您的代码更改为:
while(rs.next()){ // loop over all rows
for(int col = 1; col <= columns; col++){
String value = rs.getString(col);
if(value == null)
System.out.println("NULL Value detected!");
System.out.println("Column Name: " + rsmd.getColumnName(col));
System.out.println("Column value: " + value);
}
}
或者,如果你真的想使用 wasNull()
(但实际上,你不应该这样做!):
while(rs.next()){ // loop over all rows
for(int col = 1; col <= columns; col++){
String value = rs.getString(col);
if(rs.wasNull())
System.out.println("NULL Value detected!");
System.out.println("Column Name: " + rsmd.getColumnName(col));
System.out.println("Column value: " + value);
}
}
关于java - jdbc ResultSet.get<DataType> 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51478061/
在我的应用程序中,我必须获取记录并将它们放入二维数组中。我必须首先触发两个查询以找出计数,以便我可以初始化数组,其次是获取数据。它会导致性能下降。我需要提高性能的解决方案。 谢谢。 最佳答案 I ha
在 Zend Framework 应用程序中,它的构建与 ZF2 Getting Started tutorial 中的相册应用程序非常相似,我使用 ResultSet 对象将数据从模型通过 Cont
我是 java 和 SQL 新手,我重复了一个我不知道如何避免的问题: 假设我想创建两个executeQuery,一个在另一个内部,在getRequestsFromDB方法中我创建第一个execute
这个问题已经有答案了: Why should I call close() on a ResultSet and Connection instance? (4 个回答) 已关闭 7 年前。 使用完结
public List searchCourse(String name) { List list = null; Course course = null;
我正在使用两个不同的查询从两个表中选择数据。我正在使用一个连接和一个结果集。两个表都已填充,但第二个查询的 resultset.next() 返回 false,尽管它必须为 true。 我还尝试使用两
试图了解这里发生了什么。 DatabaseMetaData 返回一个空结果集,而实际上相同的 SQL 查询则不会。这不是主要问题,因为我使用第二个代码示例作为解决方法。 DatabaseMetaDat
假设我有多个结果集(每个结果集将引用数据库中的 1 行)(它们属于同一个表。)。现在我想创建合并的结果集,它将实习生拥有所有其他结果集。所以我的主要目标是创建一个组合的 ResultSet,它将指向以
我正在尝试将 influxdb 与我的应用程序集成并处理输出。我正在导入 InfluxDBClient 包以连接到在我的本地计算机上运行的 influx 实例。使用以“influxdb.results
我正在尝试创建一个框架来显示数据库中的所有电影名称,然后他们选择框架中的一行来执行另一个查询。 我有一个 ResultSet (rs),它接收查询执行的结果以选择电影名称。之后,我使用 SQLTabl
我有一个 Servlet,它使用返回 ResultSet 的静态实用程序方法。 在我的 Servlet 代码中,我有 ResultSet rs = SimpleSearch.searchByName(
在从 Postgres 或 Oracle 检索时间戳时(我们称之为 startDate),使用 resultSet.getString("startDate") 调用或 resultSet.getTi
全部, 对于名为Analyze 的类及其构造函数Analyze,该构造函数将Java JDBC Resultset 对象作为参数。 private Resultset analysisSet publ
m 尝试遍历 3 个结果集并比较它们的值。 bt 它抛出这个异常......有人可以帮助我了解我正在经历的地方吗?这是一段代码: java.lang.Object[] reconciledPaymen
我已经尝试使用谷歌搜索并在 SO 上查看了这些线程: java jdbc accessing multiple resultsets Open two resultsets simultaneousl
我在 SQLite 的 JDBC 驱动程序中遇到问题。 我正在使用 SELECT 语句执行查询。 如果我得到一个空的 ResultSet(0 行),那么我会在调用 getString(1) 时看到抛出
我正在尝试根据特定条件搜索表格。在 SQL Developer 中执行时,SQL 查询返回正确的结果(1 行)。在 JDBC 中,resultset 不为 null,但 resultset.next(
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 4 年前。 Improve this ques
我正在尝试在 Java 应用程序中处理大量数据。数据存储在 MySQL 数据库中,我使用的是 jdbc 连接器 8.0.11。 我的问题是我需要多次访问每条记录并且再次执行查询需要太多时间。使用 Re
这个问题已经有答案了: Getting java.sql.SQLException: Operation not allowed after ResultSet closed (2 个回答) 已关闭
我是一名优秀的程序员,十分优秀!