2

Hi all i have a problem in a insert query.

I get datetime from java with:

SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date now = new Date();
String strDate = sdfDate.format(now);

p.s: I have try a sysout on strDate and it is correct.

I read a question, like that, where to enter in MySQL datetime must make a query like this:

INSERT INTO tracks (in) VALUES (STR_TO_DATE( ? ,'%Y-%m-%d %r'))

Where '?' is the string containing the date, but it don' t work.

Can anyone help me?

UPDATE: MySQL Server 5.7

4
  • 1
    You should provide some information on the mysql version that you are using. Also: to allow for quick and easy testing you could connect to your mysql server for example with the commandline client and then enter a test like this at the prompt: "mysql> SELECT STR_TO_DATE('May 18, 2009','%M %d,%Y');" Commented Oct 24, 2016 at 11:47
  • Yes, it work... but how i can use it for my problem?! Commented Oct 24, 2016 at 11:54
  • My assumption was, that when you have it working on the cli you should be able to transfer it into java... (hope it helps) or on the command line you get hints on why your usecase in java is not working. Commented Oct 24, 2016 at 11:57
  • Thx you for the help. I have found the problem, the name "in" for the field get me a problem in the execution of the query. Commented Oct 24, 2016 at 12:03

2 Answers 2

2

You have use current time. So you can do it simply with NOW().

INSERT INTO tracks (`in`) VALUES (NOW())

OR

You can do this way

java.sql.Timestamp now = new java.sql.Timestamp(new java.util.Date().getTime());
PrepStmt.setTimestamp(1, now);
Sign up to request clarification or add additional context in comments.

1 Comment

Thx for the hint to use NOW(), is much simpler!
1

I have found the problem.

The name "in" for the DateTime field is not accepted.

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'in, session) VALUES (1, NOW(), "abc")' at line 1.

'in, session)

I have try to rename it in "indate" and the query:

INSERT INTO tracks (fk_utenza, indate, session) VALUES (1, NOW(), "abc");

works well!

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.