java preparedStatement。setString(1,“null”)被解释为null而不是字符串(在preparedStatement.addBatch()之后)
PreparedStatementSetString接受一个“null”(好像String a = "null"
)和后面的值。addBatch将其转换为正常的null(好像String a = null
)
我不知道如何才能绕过这种误解,因为很多行在全部添加为批处理后立即执行。。。(sql语句是一个INSERT INTO
…)(VarChar不能为null,但如果表在未批处理的情况下发送String a = "null"
,则表可以接受该VarChar…由于服务器发出错误消息,整个程序停止)
错误消息: 将数据写入数据库时出错。。。组织。内特扎。错误NzSQLException:错误:第17列:字段不能包含空值
代码:
preparedStatement.setString(17, x); //currently x was recieved as "null"
preparedStatement.addBatch() //after other parameters were filled then this statement
preparedStatement.executeBatch(); //after the logs this statement is executed.
希望有一个快速修复方法
我用了这个旁路,但我想会有一个更好的。。。(因为我无法更改数据库默认值或操作数据…)
if(helper.toLowerCase().equals("null"))
helper = (helper.equals("null") ? "null ":"NULL ");
preparedStatement.setString(17, helper);
# 1 楼答案
请记住,我从未使用过Netezza,我也无法尝试任何东西。这是IBM Netezza官方文档中的信息集合
如果我读的是这个权利,有一个文本值“null”的显式转换来识别一个值的缺失。。。对于external table。原因很简单,外部表是一个平面文件,因此它可能以文本形式存储所有内容,不使用分隔符,但使用分隔大小的文件
Handle the absence of a value
然后,我们可以在The NullValue option中看到相同的想法
您可以用任何其他
String
替换此表的此选项,但我相信这会解决问题因此,我的解决方案是,将表的
NullValue
设置为" "
(一个空格),在项目中修剪每个值,您将永远无法拥有" "
,因为它将被修剪为""
# 2 楼答案
鉴于您的问题,我认为您正在尝试插入字符串
null
,除非另有规定。 这个问题最简单的解决方案是将列的默认值设置为stringnull
,这样您就不必担心在代码中指定它可能会或可能不会将其解释为空。 i、 在列定义NOT NULL DEFAULT 'null'
上添加约束