1

I am struggling to insert this date into the moviesTbl. I am expected to do this without a preparedstatement. When I tried the below I received the error:

Exception in thread "main" net.ucanaccess.jdbc.UcanaccessSQLException: user lacks privilege or object not found: RELEASED

Here is the code:

String date = "2012-12-28";
java.sql.Date released = java.sql.Date.valueOf(date);
qtd.update("insert into moviesTbl (Title, ReleaseDate, Genre) VALUES ('Harry Potter', released, 'drama')");

How do I insert a date without a prepared statement?

jww
  • 97,681
  • 90
  • 411
  • 885
  • 1
    I am not familiar with "net.ucanaccess", but most databases have a defined format for representing date or TIMESTAMP data in a String. – Brett Okken Aug 01 '14 at 01:57

1 Answers1

1

You have the variable released inside the quotes. So the SQL errror is saying something along the lines of "I cant find the variable released". You need to put your variable in the java so the value can be placed into the SQL otherwise the SQL server is looking for a variable called released but of course it isnt there, its in your Java.

String date = "2012-12-28";
qtd.update("insert into moviesTbl (Title, ReleaseDate, Genre) VALUES ('Harry Potter', '"+date+"', 'drama')");

As @user2067753 stated different databases have different formats for getting and setting datetime formats. Assuming you are using a MS SQL server then following this post you can do something like this

java.util.Date date = new SimpleDateFormat("yyyy-MM-dd").parse("2012-12-28");
java.text.SimpleDateFormat msSqlDateFormat = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
qtd.update("insert into moviesTbl (Title, ReleaseDate, Genre) VALUES ('Harry Potter', '"+msSqlDateFormat.format(date)+"', 'drama')");
Community
  • 1
  • 1
ug_
  • 11,267
  • 2
  • 35
  • 52
  • Thank you, although now I get an error saying that my datetime format is invalid? – user3893801 Aug 01 '14 at 02:01
  • 2
    every dbms vendor has different ways of handling dates. the term `datetime` indicates you are using MS SQL Server (but I can't be 100% sure). For MS SQL Server the safest date string for this purpose is YYYYMMDD `String date = "20121228";` but you MUST also send the single quotes as well because it is a string you are sending (but it looks like a date) – Paul Maxwell Aug 01 '14 at 02:53
  • @ug_ : ye, I figured, lol. Thanks once again, but when I tried the above I first got a 'unreported parse exception' problem so I added in a throws for the parse exception. But then, I got an "integrity constraint violation: unique constraint or index violation" error. – user3893801 Aug 02 '14 at 20:30
  • @user3893801 Copy that error message then put it into Google. Its a common problem to have but the error is unrelated to the current question and I dont have enough information to tell you EXACTLY what is causing the error. There error is very likely to be caused by a duplicate column attempting to be inserted into your Database. If you are still having trouble and cant find the solution post a new Stack Overflow question with the details of your SQL Table and the error you are receiving. – ug_ Aug 02 '14 at 20:35
  • Oh, and i'm using ms access – user3893801 Aug 02 '14 at 20:36