Solved

NULL value problem (URGENT please!)

Posted on 2003-12-04
6
220 Views
Last Modified: 2010-04-01
From my JSP program I need to execute an SQL insert statement:

***JSP file**
<%
String st1;
...
...//some work done on st1
..

count = stmt.executeUpdate("insert into mytab (f1, f2,..) values ('st1', ...,..)");

Here when I execute above statement, I do not know if string "st1" has some value or if it is null. If "st1" has some value, no problem.But if "st1" has no value, the above statement is entering the string "nu;;" in the above table:
SQL>select f1 from mytab:
f1
==
null

How can I change my JSP to avoid entering "null" when st1 does not have any content?

please help!
0
Comment
Question by:tooki
  • 2
6 Comments
 
LVL 4

Expert Comment

by:vk33
ID: 9880461
What value do you want to be inserted if st1 is null or empty? If you need the null-value (not the 'null' string) you should exclude the f1 parameter from the list:

if (st1 == null)
   count = stmt.executeUpdate("INSERT INTO mytab (f2,...) VALUES (...)");
else
   count = stmt.executeUpdate("INSERT INTO mytab (f1,f2,...) VALUES ('" + st1 + "',...");

And I would suggest using PreparedStatement instead:
PreparedStatement st = conn.prepareStatement("INSERT INTO mytab (f1,f2,...) VALUES (?,?,...)";
st.setString(1,st1);
st.setString(2,...);
count = st.executeUpdate();

Regards!
0
 

Author Comment

by:tooki
ID: 9880724
Thanks!
But is was ok if I was ambigupus about only one field (if it is null) in that satement. but if in the insert sql statement has say 5 fields and I know that any of them can be null, then it becomes difficult.

And when a string is null, I would like to enter no value (In oracle it is NULL) for that field insead of the string "null".

Isn't there any better way of this null check?just wonder..

-tooki
0
 
LVL 4

Accepted Solution

by:
vk33 earned 25 total points
ID: 9880785
yes, sure! Use PreparedStatement, it will solve the problem:

PreparedStatement st = conn.prepareStatement("INSERT INTO mytab (f1,f2,f3) VALUES (?,?,?)";
st.setString(1,st1);
st.setString(2,st2);
st.setString(3,st3);
count = st.executeUpdate();

If any of these strings are null, you'll have a NULL value in the database.

Regards!
0
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 25 total points
ID: 9926311
Or maybet that you can write a utility function like:

public String getValidString ( String sCheck )
{
  return ( sCheck == null ) ? "" : sCheck.trim () ; // if not null, then it also trims it - optional

} // end of getValidString ()

Now you can use this method everywhere in your code, like:

count = stmt.executeUpdate("insert into mytab (f1, f2,..) values ('" + getValidString (st1) + "','" + getValidString (st2) + "',  ...,..)");

Mayank.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Problem with Pie chart in Jsp 20 222
TreeSet comparator example 7 97
How to find missing packages when using Netbeans IDE 8.1 ? 19 59
Suggestion on WebSite Template Sites 6 108
This article describes my battle tested process for setting up delegation. I use this process anywhere that I need to setup delegation. In the article I will show how it applies to Active Directory
This article describes how to reset your Windows 10 password when you've forgotten it.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question