有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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);

共 (2) 个答案

  1. # 1 楼答案

    请记住,我从未使用过Netezza,我也无法尝试任何东西。这是IBM Netezza官方文档中的信息集合

    如果我读的是这个权利,有一个文本值“null”的显式转换来识别一个值的缺失。。。对于external table。原因很简单,外部表是一个平面文件,因此它可能以文本形式存储所有内容,不使用分隔符,但使用分隔大小的文件

    Handle the absence of a value

    In SQL, a record must include a value if a column is declared as not null. When a record contains no value for a column, the column is considered to be null. The system provides an explicit and implicit method for conveying nullness.
    - The explicit method includes a specific token in the field instead of a value. By default, this token is the word “null” (not case-sensitive). You can use the nullValue option to change this token to any other 1 - 4 character alphabetic token. You can precede or follow an occurrence of the explicit null token in a non-string field with adjacent spaces. For the system to recognize an explicit null token in a string field, the token cannot have preceding or trailing adjacent spaces. The explicit null token method makes it impossible to express a string that consists of exactly the text of the null token.

    然后,我们可以在The NullValue option中看到相同的想法

    Specifies the string to use for the null value, with a maximum 4-byte UTF-8 string. The default is ‘NULL’.

    您可以用任何其他String替换此表的此选项,但我相信这会解决问题

    因此,我的解决方案是,将表的NullValue设置为" "(一个空格),在项目中修剪每个值,您将永远无法拥有" ",因为它将被修剪为""

  2. # 2 楼答案

    鉴于您的问题,我认为您正在尝试插入字符串null,除非另有规定。 这个问题最简单的解决方案是将列的默认值设置为stringnull,这样您就不必担心在代码中指定它可能会或可能不会将其解释为空。 i、 在列定义NOT NULL DEFAULT 'null'上添加约束