Best practice for handling null rows from a database (in Java)

In my database application, I sometimes have to deal with rows nullin the database. In most cases, this is normal, but when it comes to displaying data in a form, Swing components - for example, with the help of JTextField- cannot process null strings. ( .setText(null)does not work)

( EDIT: I just noticed that it JTextFieldactually takes a string null, but the question remains for all other cases where unexpected values nullcan lead to problems.)

Zero values ​​have no special meaning; they can (should) be considered as empty strings.

What is the best practice to solve this problem? Sorry, I cannot modify the database.

  • Checking each value if it is nullbefore the call setText()?
  • Adding a try-catch handler for each call setText()?
  • Introducing a static method that filters all rows null?
  • Replace all values nullwith blank lines immediately after reading from the database?
  • ... [your suggestions]
+5
source share
7 answers

If you use some kind of ORM tool or somehow you map your database fields to a Java bean, you can always:

public void setFoo (String str) {
  this.foo = str! = null? str: "";
}
+6
source

From an SQL corner, try:

select ISNULL(column_name,'') from ...
+4
source

Beans API SWING. Beanins Binding .

+2

, , " ", . , , , - "" . , "" , . "String" , , .

, , . , , , , , .

+2

, - - .

0

JTextField setText(), NULL .

0

, JTextField, setText() NULL .

However, I would also rewrite the getText () method to overwrite the empty string with NULL, so that when you return to the database, you do not overwrite the null value there with the empty string.

0
source

All Articles