The field in the database does not give a value or gives "", what is stored by default

Prepare

There is a stu table in the mysql database, the following is the structure and data of the table and the Student class

 

 Start test one (no value given)

 Scenario 1: Add a piece of data, but skip the math column

As you can see, the default value of the int type of the table is null.

Check the data with id=2 and use the student class to access it.

 The result math is 0, because the int element of the entity class will not be null, it will give 0 by default

Use hashMap<string,string> to pick up

 As a result, because the math field is null, the math field is not received directly.

 Case 2: Add a piece of data, but skip the sex column

  As you can see, the default value of varchar type is null.

Check the data with id=3 and use the student class to access it.

The result sex is null, because the string element of the entity class is also null by default.

 Use hashMap<string,string> to pick up

 As a result, because the sex field is null, the sex field is not received directly.

 Case 3: Add a piece of data, but skip the insertTime column

   As you can see, the default datetime type is null.

Check the data with id=3 and use the student class to access it.

 The result insertTime is null, because the Date element of the entity class is also null by default.

Use hashMap<string,string> to pick up

 As a result, because the insertTime field is null, the insertTime field is not received directly.

 Start test one (give empty string)

Since the assignment of an empty string to the int and datetime type fields of the table will cause an error, so I will only try the varchar type of the table.

Case 1: Add an empty string "" to the sex field

 From the figure below, you can see that the sex field in the added row of data displays a white box, and the mouse cursor is at the far left of the cell after the mouse is clicked in.

Check the data with id=5 and use the student class to access it.

  As you can see, the empty string "" is also printed.

 Check the data with id=5, use map to receive and print the result

 As you can see, the empty string "" is also printed.

Case 2: Add a string with several spaces to the sex field " "

From the picture below, you can see that the sex field in the added row of data also displays a white box. After the mouse is clicked in, the mouse cursor is some distance away from the left side of the cell.

  Check the data with id=6 and use the student class to access it.

   As you can see, what is printed is also a string with several spaces " "

 Check the data with id=6, use map to receive and print the result

  As you can see, what is printed is also a string with several spaces " "

Summarize:

1. In the table creation statement in the mysql database table, any field is followed by NULL DEFAULT NULL, which means that if no value is given, the default value is null. Therefore, the int, varchar and datetime type fields in the above table are not assigned values. time, defaults to null

2. When using the entity class to receive the select result, although the int type found in the table lookup is null, the entity class will automatically change the int to its own defined default value of 0.

3. When using map to receive select results, null fields in the table will not be received directly in map. Map only receives non-null fields.

4. Give the varchar type field of the table an empty string "" or a string with spaces " ", and the table will really store them exactly the same. And when you use entity classes or maps to receive results, you can receive them. Empty string "" or string with spaces " ".

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

Time is limited. For the time being, we will test the three field types of the table: int, varchar, and datetime. We will summarize them later when we have time or encounter other field types.

Guess you like

Origin blog.csdn.net/weixin_70280523/article/details/132418376