*
The moose likes JDBC and the fly likes Insert Default Columns if the String length is 0? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Insert Default Columns if the String length is 0?" Watch "Insert Default Columns if the String length is 0?" New topic
Author

Insert Default Columns if the String length is 0?

J. Insi
Ranch Hand

Joined: Aug 11, 2010
Posts: 90

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

Joined: Aug 17, 2008
Posts: 219

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


blog: http://tunatore.wordpress.com
SCJP 6.0 + SCWCD 1.5
J. Insi
Ranch Hand

Joined: Aug 11, 2010
Posts: 90
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

Joined: Aug 17, 2008
Posts: 219

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

Joined: Aug 11, 2010
Posts: 90
ok i got it!

OMG. i have a bunch of code like this in another apps....
 
 
subject: Insert Default Columns if the String length is 0?
 
Similar Threads
Insertion Of Data
Error in insert into
exception I got java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver]COUNT field incorrec
ResultSets Lose Second half of getString
entity with auto-generated primary key