4

I want to update the string date into MySQL database using prepared statement. I have tried a lot and always got error java.util.Date cannot parse into java.sql.Date or vise versa. I didn't import anything here. Please import according to your answer.

public class Date1 
{ 
    public static void main(String args[]) 
    {
        String source="2008/4/5";              
        SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");                       
        java.sql.Date d=(Date) format.parse(source);             
        Class.forName("com.mysql.jdbc.Driver");        
        Connection con=(Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/employee", "root", "root");   
        PreparedStatement ps=con.prepareStatement("insert into ankur1 values(?)");          
        ps.setDate(1,(java.sql.Date) d);    
        ps.executUpdate();
    }
}
2
  • can you send details of ankur1 table Commented Oct 16, 2012 at 13:55
  • ` I didn't import anything here` -> Not even java.sql.Date?? Commented Oct 16, 2012 at 13:59

3 Answers 3

7

Write this

java.sql.Date d= new java.sql.Date(format.parse(source).getTime());

instead of this:

java.sql.Date d=(Date) format.parse(source);

Because you cannot just cast java.util.Date to its subtype java.sql.Date. You have to convert it. Do also note that your format string doesn't match your actual date format, as Bill the Lizard commented.

Sign up to request clarification or add additional context in comments.

7 Comments

This is correct. Also note that "2008/4/5" is not in the format "dd/MM/yyyy".
@BilltheLizard: Right, I didn't even notice
@Lukas Eder i have write your line you provided it is successfully reflected on a database like i have this String str = "2006-12-28"; SimpleDateFormat formatter = new SimpleDateFormat("yyyy-mm-dd"); java.sql.Date d= new java.sql.Date(formatter.parse(str).getTime()); ps.setDate(1,d); it is reflecting but whatever i have providing the data it is reflected as 01 in month place in each case in mysql 5.2.38
@ankurjadiya: I'm not sure what you're trying to say. Do you have any other issues apart from what you had already mentioned in the question?
@ankur jadiya: Your Formatstring in your comment is wrong. Use "yyyy-MM-dd". So the months in uppercase. in lowercase (mm) they are ment for minutes.
|
2
java.sql.Timestamp date = new java.sql.Timestamp(new java.util.Date().getTime());

pstmt.setTimestamp(1, date);

Try this it may work.. Thanks

Comments

1

here is another simpler way to solve this:

preparedStatement = connect.prepareStatement("INSERT INTO test.TABLENAME VALUES (default, STR_TO_DATE( ?, '%m/%d/%Y'), STR_TO_DATE(?, '%l:%i %p'),?,?,?,?,?)"); 

Or, you can replace the "?" with real data.

This is how I insert date and time to mySQL, just figured it out.

We can adjust the kinds of parameters to meet our data's format, it's easy and clean.

Comments

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.