zoukankan      html  css  js  c++  java
  • 处理数据库中的null值问题

      1 public class Test {
      2 
      3     public static void nullPoint1() {
      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 void nullPoint2() {
     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 void nullPoint3() {
     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 void nullPoint4() {
     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 void error() {
     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 void error2() {
     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 void right() {
     66         String test = null;
     67         if (test == null) {
     68             System.out.println("test = null, test == null: true");  
     69         }
     70     }
     71 
     72     public static void right2() {
     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 void right3() {
     92         String test = "";
     93         if (test.equals("") && test == "") {
     94             System.out.println("test = """);
     95         }
     96     }
     97 
     98     public static void main(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
    版权声明:本文为博主原创文章,转载请附上博文链接!

  • 相关阅读:
    mysql的常用查询创建命令
    maven的简介
    google guava
    分库分表的情况下生成全局唯一的ID
    书单
    MD5Util
    UUID生成工具
    nodejs学习笔记三——nodejs使用富文本插件ueditor
    nodejs学习笔记二——链接mongodb
    mongodb 安装
  • 原文地址:https://www.cnblogs.com/minixiong/p/11131238.html
Copyright © 2011-2022 走看看