• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

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

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you, mr.Rao
seema kamath
 
Onion rings are vegetable donuts. Taste this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic