2

I am getting a string value such as "2012-01-20" and converting it to sql.Date to insert it in the database.

code

java.util.Date DOB = new java.util.Date();
String datetext = txtDate.getText();

SimpleDateFormat sd = new SimpleDateFormat("yyyy-mm-dd");
DOB = sd.parse(datetext);
java.sql.Date sqlDate = new java.sql.Date(DOB.getTime());

String query = "ISERT INTO EMPLOYEE ('DOB')"
                   + " VALUES ( ? ) ";

con = db.createConnection();
 try{
     ps = con.prepareStatement(query);
     ps.setDate(1, sqlDate);
     ps.executeQuery();
    }catch(Exception ex){
       System.err.print(ex);
  }

When i run this code I am getting an exception ""[Microsoft][ODBC SQL Server Driver]Optional feature not implemented"

what am i doing wrong here ? pls help !

1 Answer 1

5

Remove single quote - ('DOB') and INSERT (misspell)

String query = "INSERT INTO EMPLOYEE (DOB) VALUES (?)";

You may use java.sql.Date.valueOf("2012-01-20") method to convert string to sql date.

Use executeUpdate() method instead of executeQuery().

String query = "INSERT INTO EMPLOYEE (DOB) VALUES (?)";

con = db.createConnection();
 try{
     ps = con.prepareStatement(query);
     ps.setDate(1, java.sql.Date.valueOf("2012-01-20"));
     //Or use
     //  ps.setTimestamp(1, new Timestamp(DOB.getTime())); 
     ps.executeUpdate();
    }catch(Exception ex){
       System.err.print(ex);
  }
Sign up to request clarification or add additional context in comments.

8 Comments

the "valueof" method is not found ? why is that
Thanks , I corrected that. But making all the modifications as You suggested , still does not solve the problem. It gives the same exception.
@ReezaMuaz - May be you missed something to set ODBC DSN. See I've edited post.
the DSN is already set and connection is made successfully. When I am debugging the executeUpdate() line is not even reached, after the ps.setDate() it throws the exception. :/
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.