Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

URGENT,PLEASE HELP!!!!!!!!!!!!!

 
seema kamath
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello friends,
This requires some imeediate help.
Im working with inserting null values thru PreparedStatements. I want to set a Date field to null.The field is designed to accept null values.Any manual inserts doesnt give me problems but then when i try to insert null for this field through a PreparedStatement it gives me a NullPointerException or a Data Out of range
I have tried using setObject() method as well as setNull method.I waork with sqlserver7.0. i have tried this on ms acess also.
given below is the code
//******************************************************************

import java.sql.*;
public class Ins
{
public static void main(String a[])
{Connection con = null,con2=null;
PreparedStatement st= null;
int i=0;
String cust=" ";
Date d=null;
try
{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc dbc:NWind");
st=con.prepareStatement("Insert into Employees(LastName,FirstName,BirthDate) values(?,?,?)");
st.setString(1,"gaurav");
st.setString(2,"srivastava");
st.setObject(3,null,Types.DATE);
st.setNull(3,Types.DATE);
i=st.executeUpdate();
con.close();
System.out.println("Insert Success"+i+ " ");
}

catch(Exception e)
{
System.out.println(e);
}
}
}

//******************************************************************
PLEASE HELP ME AS THE SOLUTION IS REQUIRED AS SOON AS POSSIBLE.
THANKS A LOT FOR THE HELP
 
NageswaraRao Karra
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi!
the following will work
String myDate="";
if(myDate.equals(""))
{
st.setString(3,myDate);
}
else
{
Date d=Date.valueOf(myDate);
st.setDate(3,d);
}
 
seema kamath
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you, mr.Rao
seema kamath
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic