Meaningless Drivel is fun!*
The moose likes JDBC and the fly likes URGENT,PLEASE HELP!!!!!!!!!!!!! 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 "URGENT,PLEASE HELP!!!!!!!!!!!!!" Watch "URGENT,PLEASE HELP!!!!!!!!!!!!!" New topic
Author

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

seema kamath
Greenhorn

Joined: Nov 27, 2000
Posts: 14
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

Joined: Nov 12, 2000
Posts: 34
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

Joined: Nov 27, 2000
Posts: 14
thank you, mr.Rao
seema kamath
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: URGENT,PLEASE HELP!!!!!!!!!!!!!
 
Similar Threads
Getting sql error [Microsoft ][odbc microsoft access driver]numeric index out of range
getting NumberFormatException
connecting java application to JAVADB database
storing date in mysql
Doubt in the following code