java 数据库 空值_处理数据库中的null值问题

1 public classTest {2

3 public static voidnullPoint1() {4 String test = null;5 try{6 if (test.equals(null)) {7 System.out.println("test is null !");8 }9 } catch(NullPointerException e) {10 System.out.println("test.equals(null): " +e);11 }12 }13

14 public static voidnullPoint2() {15 String test = null;16 try{17 if (test.length() == 0) {18 System.out.println("test is null !");19 }20 } catch(NullPointerException e) {21 System.out.println("test.length() == 0: " +e);22 }23 }24

25 public static voidnullPoint3() {26 String test = null;27 try{28 if(test.isEmpty()) {29 System.out.println("test is null !");30 }31 } catch(NullPointerException e) {32 System.out.println("test.isEmpty(): " +e);33 }34 }35

36 public static voidnullPoint4() {37 String test = null;38 try{39 if (test.equals("")) {40 System.out.println("test is null !");41 }42 } catch(NullPointerException e) {43 System.out.println("test.equals(\"\"): " +e);44 }45 }46

47 public static voiderror() {48 String test = null;49 if ("".equals(test)) {50 System.out.println("test = null, \"\".equals(test): true");51 } else{52 System.out.println("test = null, \"\".equals(test): false");53 }54 }55

56 public static voiderror2() {57 String test = null;58 if (test == "") {59 System.out.println("test = null, test == \"\": true");60 } else{61 System.out.println("test = null, test == \"\": false");62 }63 }64

65 public static voidright() {66 String test = null;67 if (test == null) {68 System.out.println("test = null, test == null: true");69 }70 }71

72 public static voidright2() {73 String test = "not null";74 if (test.equals(null)) {75 System.out.println("test is null");76 }77 if (test.length() <= 0) {78 System.out.println("test is null");79 }80 if(test.isEmpty()) {81 System.out.println("test is null");82 }83 if (test == null) {84 System.out.println("test is null");85 }86 if (test != null && !test.isEmpty() && test.length() > 0 && !test.equals(null)) {87 System.out.println("test : " +test);88 }89 }90

91 public static voidright3() {92 String test = "";93 if (test.equals("") && test == "") {94 System.out.println("test = \"\"");95 }96 }97

98 public static voidmain(String[] args) {99 Test.nullPoint1();100 Test.nullPoint2();101 Test.nullPoint3();102 Test.nullPoint4();103 Test.error();104 Test.error2();105 Test.right();106 Test.right2();107 Test.right3();108 }109 }

运行结果为:

test.equals(null): java.lang.NullPointerException

test.length() == 0: java.lang.NullPointerException

test.isEmpty(): java.lang.NullPointerException

test.equals(“”): java.lang.NullPointerException

test = null, “”.equals(test): false

test = null, test == “”: false

test = null, test == null: true

test : not null

test = “”

可以看到当字符串为null时,用equals()、length()、isEmpty() 都会抛出异常;而用与 “” 进行比较时,会出现判断失误。

所以可以使用 “if (test == null)” 来判断,简单直接。

注意当字符串为 “” 时,与null不同,不是空指针,得与”“比较。字符串为空

这个方法也适用于处理数据库中的null值问题

---------------------

作者:sys_

来源:CSDN

原文:https://blog.csdn.net/qq_34732088/article/details/79571938

版权声明:本文为博主原创文章,转载请附上博文链接!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值