Solved

NULL value problem (URGENT please!)

Posted on 2003-12-04
6
224 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Popular third-party chat platforms like Slack, Discord, and Telegram are just a few of the many new productivity applications that are being hijacked by cybercriminals to create command-and-control (C&C) communications infrastructures for their malw…
This article provides a convenient collection of links to Microsoft provided Security Patches for operating systems that have reached their End of Life support cycle. Included operating systems covered by this article are Windows XP,  Windows Server…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

707 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