非空处理 Java非空判断 非空处理及mysql数据库字段的not null
1.mysql
## 去掉非空,如果非空又没有默认值,这样程序在添加数据的时候i,如果没有设置值就会报错。该操作很危险。
##ALTER TABLE `order_test` ADD COLUMN `test_card_name` NOT NULL VARCHAR(200) COMMENT '卡名称';
## 修改允许为空 ..
ALTER TABLE `order_test` MODIFY COLUMN `test_card_name` VARCHAR(200) COMMENT '卡名称';
2.java
package com.example.core.mydemo.java; /** * output: * null * null * 222 * 666 */ public class ObjectNullTest { public static void main(String[] args) { Person person = new Person(); System.out.println(person.getSex()); System.out.println(person.getAge()); if("aaa".equals(person.getSex())){ System.out.println("111"); }else{ System.out.println("222"); } //java.lang.NullPointerException // if(person.getSex().equals("aaa")){ // System.out.println("aaa"); // }else{ // System.out.println("bbb"); // } //java.lang.NullPointerException // if(100 == person.getAge()){ // System.out.println("ccc"); // }else{ // System.out.println("ddd"); // } //java.lang.NullPointerException // if(person.getAge() == 100){ // System.out.println("333"); // }else{ // System.out.println("444"); // } //解决方法:加上非空的判断 if(person.getAge() != null && person.getAge() == 100){ System.out.println("555"); }else{ System.out.println("666"); } } }
package com.example.core.mydemo.java; import java.io.Serializable; public class Person implements Serializable { private String firstName; private String lastName; //transient关键字表示有理的,被修饰的数据不能进行序列化 // private transient String sex; private String sex; private Integer age; public Person(){ } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Person(String fname, String lname){ this.firstName = fname; this.lastName = lname; } public void setFirstName(String firstName) { this.firstName = firstName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getFirstName(){ return firstName; } public String getLastName(){ return lastName; } public String getName(){ return firstName + " " + lastName; } }