Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Insert Default Columns if the String length is 0?

 
J. Insi
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hello guys,
I'm actually having an JSP Form in a single page.
Well, we can say it is a static one. But
my case here is that, if there's nobody enter any value into the form...
Then the textfields there would be a STRING that has a length of 0.

Thus, I wanted to use that NOT To be stored into the (Database) db that I used.
Instead I want to use its default value.

Here I use to create the insertion occured in mysql utility that I made;



But, if I put that way... then the Order1 variable is not getting the default value for them,
Then if I set my beans into a "DEFAULT" ---> String. also it will not making the Column insert its default value.

Is there any solve for this case?
 
Tuna Töre
Ranch Hand
Posts: 220
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you configure your database's column values to set DEFAULT for the table mentioned?

You can configure columns' default value in the Database.

After you INSERT without any values inside INSERT statement, then those values are set to database defaults automatically.

Tuna TÖRE
 
J. Insi
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well, ya ... I mean, the table of that mysql is already assigned using DEFAULT value.

Such as its values from :


I already set them with : "-" as the default value.

But, here the case is... what String value that I need to pass upon
the values of :



Because, If I pass null or empty string (" ") into the setString as below ;


it won't pass the default value... even If, let say I one of them become NULL, and it's set...
still, the query passing over the values not the DEFAULT.

~ correct me if i'm wrong.
 
Tuna Töre
Ranch Hand
Posts: 220
Eclipse IDE Java Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
J. Insi wrote:well, ya ... I mean, the table of that mysql is already assigned using DEFAULT value.

Such as its values from :


I already set them with : "-" as the default value.

But, here the case is... what String value that I need to pass upon
the values of :



Because, If I pass null or empty string (" ") into the setString as below ;


it won't pass the default value... even If, let say I one of them become NULL, and it's set...
still, the query passing over the values not the DEFAULT.

~ correct me if i'm wrong.


It is normal for Java J2EE applications, if you set them with empty strings then those columns will store empty string value inside the columns defined.

Firstly, you should check the value in your Java code and validate the getString method for NULL or empty strings. Validation inside Java Code is required if you don't want to set (empty or NULL).
You should decide by using simple if/else logic and checking NULL or empty string. After checking values you can use setString method for those columns with DEFAULT like below;

INSERT table VALUES (DEFAULT, NULL, 'WA', DEFAULT,.....) I mean use setString method like this setString(1,'DEFAULT')

If you use setString method, naturally it will change(override) the default value.

Otherwise how can you set NULL or empty string for those colums??? That's case. ? got it ?

Tuna TÖRE
 
J. Insi
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok i got it!

OMG. i have a bunch of code like this in another apps....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic