Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I insert a NULL value into a database column ?

Status
Not open for further replies.

Lars

Programmer
Aug 10, 2000
16
DE
Please help me!

I have to insert a NULL value into a column of a database.
I tried this:


String personId = "p0815";
String value1 = "test1";
String value2 = "test2";
String value3 = null;

String SQL_String = "'" + person + "'" + ",'" + value1 + "'" + ",'" + value2 + "'" + ",'" + value3 + "'";

Connection con = null;
con = DriverManager.getConnection(url, id, pw);
Statement stmt = con.createStatement();

stmt.executeUpdate(SQL_String);

I get an error that says: value cannot be a null value

Can this problem be solved by using a prepared statement and using the setNull method ? How does it work?

Thanks a lot!

 
it depends on the set up of your database, if that column has been specified as NOT NULL when the table was created then you will not be able to insert anything other than a valid non-null character.

You need to check your table definition.
 
hi pipk,

thanks for your reply.

i worked it out. the problem was that i always tried to insert an empty string whenever the string was really null.
access didn´t like that!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top