File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and the fly likes Inserting nulls 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 "Inserting nulls" Watch "Inserting nulls" New topic

Inserting nulls

Gayathri Neti

Joined: Mar 08, 2002
Posts: 25
Hai Java Gurus,
The question may soud dumb but ...let me ask. I have a varchar column and in my program I have a string variable that take sdata from thr textbox in the gui and inserts it into database. My problem is if th evalue in text box is empty then it is entering empty string into database. I want it to enter nothing.....I canno tchange any database setting and database I am using is Sql server.
Avi Abrami
Ranch Hand

Joined: Oct 11, 2000
Posts: 1121

Hi Gayathri,
If you are using a "java.sql.PreparedStatement" to insert the data into your database, then you can use the "setNull()" method to set a column value to null -- assuming that you want to set the column to null if nothing has been entered into the "textbox".
I suggest you read the javadoc for the "setNull()" method.
Hope this helps.
Good Luck,
Suresh Selvaraj
Ranch Hand

Joined: Nov 14, 2000
Posts: 104
If you are using PreparedStatement then you may try this.
PreparedStatement ps=null;
if(textString == null)
ps.setNull(1, java.sql.Types.VARCHAR);
Suresh Selvaraj

Suresh Selvaraj, (author of JQuiz)<br />SCJP2<br /><a href="" target="_blank" rel="nofollow"></a>
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: Inserting nulls
Similar Threads
Creating empty tags using JAXB.
Getting value of Disabled field
Creating a Unique ID from a String.
Handle Null Values
How to have checkbox value blank instead of "null"