File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes Problem in inserting Date feild Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Problem in inserting Date feild" Watch "Problem in inserting Date feild" New topic

Problem in inserting Date feild

Kashif Iqbal Khan

Joined: Dec 22, 2001
Posts: 13
I am using MS SQL server database with jdbc odbc bridge driver. I have a table with the name of "student" have the feild of "DOB" having type of datetime and other feilds are of type of varchar etc. When i go for inserting the entries with DOB it give exception as follows:
"Microsoft][ODBC SQL Server Driver]Optional feature not implemented"
And when i go for inserting without DOB it goes perfectly.
I m using setDate(), setString() methods to parse the data according to sql type.
My code is as follows:
public void insertStudent(Student std){
try {
String query = new String("INSERT INTO student (RollNo, Name,DOB )VALUES(?,?,?)" );
PreparedStatement st= db.prepareStatement(query);
st.setDate(3,std.dob );
int rowsUpdated = st.executeUpdate();
Can any tell me the solution of this exception.

Kashif I. Khan
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17276

Not directly answering your question. But in my Visual Basic code to write to Oracle, I take a Date, and make it a String. Then I call the to_date function in Oracle to convert the string to a date in the insert statement.
So in your example the insert statement would look like this.
INSERT INTO student (RollNo, Name,DOB )VALUES ("RollNO", "Name", to_date("03/29/03","MM/DD/YY")
now I know SQL Server has a conversion function to make strings into dates and the other way around. In Foxpro it was CTOD() which stands for "Character To Date".
So you could go down that route.
Good Luck

Perfect World Programming, LLC - iOS Apps
How to Ask Questions the Smart Way FAQ
I agree. Here's the link:
subject: Problem in inserting Date feild
It's not a secret anymore!