简体   繁体   English

java插入null但不是原始默认值

[英]java insert null but not primitive default value

I want insert a null value into database instead of inserting default primitive value. 我想向数据库中插入一个空值,而不是插入默认的原始值。

I have a class 我有一堂课

class Test
(
    private int first;
    private int second;
    public Test() {}  
    public void setFirst(int val) {first = val;}
    public void setSecond(int val) {second = val;}
    public int getFirst() {return first;}
    public int getSecond() {return second;}
)

I have some code that initialize Test class: 我有一些代码初始化Test类:

Test my = new Test(); 测试my = new Test(); my.setFirst(100); my.setFirst(100);

so we have now: first = 100; 所以我们现在有:first = 100; second = 0; 秒= 0; -- since I don't initialize it and it default to zero "0" for int. -因为我没有初始化它,所以它对于int默认为零“ 0”。

Now I insert the data into database table Test ... 现在,我将数据插入数据库表Test中。

Create table Test
(
  first number,
  second number
)

Java call ... Java调用...

Test my = new Test();
my.setFirst(100);
String sql = "INSERT INTO TEST VALUES(?,?)";
PrepareStatement ps = connection.prepareStatement(sql);
ps.setInt(1, my.getFirst());    // will insert 100
ps.setInt(2, my.getSecond());   // will insert 0

I want insert NULL into "second" field since Test.second is not set in my code. 我想在“秒”字段中插入NULL,因为我的代码中未设置Test.second。 I use the following trick: 我使用以下技巧:

class Test
(
    private int first = -999;
    private int second = -999;
    public Test() {}  
    public void setFirst(int val) {first = val;}
    public void setSecond(int val) {second = val;}
    public int getFirst() {return first;}
    public int getSecond() {return second;}
)


Test my = new Test();
my.setFirst(100);
String sql = "INSERT INTO TEST VALUES(?,?)";
PrepareStatement ps = connection.prepareStatement(sql);

if (my.getFirst() == -999)
   ps.setNull(1, java.sql.Types.INTEGER) // will insert NULL
else
   ps.setInt(1, my.getFirst());    // will insert 100


if (my.getSecond() == -999)
   ps.setNull(2, java.sql.Types.INTEGER) // will insert NULL
else
   ps.setInt(2, my.getSecond());    // will insert 0

Does somebody has the best/nice solution to implement that ? 有人有最好/最好的解决方案来实现吗?

I get NullPointerException when use Integer, this is right since var is not initialized or I missed something here: 使用Integer时出现NullPointerException,这是正确的,因为未初始化var或我在这里错过了一些东西:

java.lang.NullPointerException
at UserQuiz.getFrom_flags_challenge_nid(UserQuiz:113)

UserQuiz. java
...
private Integer from_flags_challenge_nid;
public int getFrom_flags_challenge_nid() {
    return from_flags_challenge_nid;
}

public void setFrom_flags_challenge_nid(int from_flags_challenge_nid) {
    this.from_flags_challenge_nid = from_flags_challenge_nid;
}
... 

Change your int to Integer (everywhere - the field, parameter of setter and return type of getter) and then you can just try this: int更改为Integer (无处不在-字段,setter参数和getter的返回类型),然后可以尝试以下操作:

ps.setObject(1, my.getFirst()/*may return null*/, java.sql.Types.INTEGER);
ps.setObject(2, my.getSecond()/*may return null*/, java.sql.Types.INTEGER);

Note that code like this: 注意这样的代码:

private Integer from_flags_challenge_nid;
public int getFrom_flags_challenge_nid() {
   return from_flags_challenge_nid;
}

will throw a NullPointerException if from_flags_challenge_nid contains null . 如果from_flags_challenge_nid包含null则将抛出NullPointerException。

If you want to add this kind of behavior, why not extend PreparedStatement and provide your own implementation? 如果要添加这种行为,为什么不扩展PreparedStatement并提供自己的实现?

class MyPreparedStatement extends PreparedStatement {

    public void setNullableInt(int parameterIndex, int x)
        throws SQLException
        if (x == 0) {
            setNull(parameterIndex, java.sql.Types.INTEGER)
        }
        else {
            setInt(parameterIndex, x);
        }
    }
}

Now, the problem with this is that 0 is not logically equivalent to null , and really shouldn't be treated as such. 现在,这样做的问题是0在逻辑上不等于null ,实际上不应该这样对待。 A common method if you're dealing with positive integers in general is to use something like -1 to represent a 'non-value'. 如果通常要处理正整数,通常的方法是使用类似-1的值来表示“非值”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM