Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7631105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:16:06+00:00 2026-05-31T06:16:06+00:00

Relatively new to using database and for some reason I can’t get this ‘execute’

  • 0

Relatively new to using database and for some reason I can’t get this ‘execute’ to work.

statment2.execute("insert into table Value (" + int + "," + date + "," + int + ",'" + string + "')");

The error I get is “missing a comma”. The date is designated as dates only in that particular field.

I set it up as follows

Date date = new Date();
date.setMonth(month);
date.setYear(year);
date.setDate(weekStart); //weekStart is always monday

Do I need to use just plain old date or date.toString? I was going to use Calendar but I don’t know how to set a DB date using the Calendar object. I didn’t see a “gety/m/d” method.

So, is the problem my query or am I improperly using the Date object to set the date in the database?

Edit:

Tried the response, got incorrect format – Expected Date got number.
Tried

sqlDate.valueOf(dateString I created)
sqlDate.toString()
sqlDate

Using a preparedStatement wouldn’t fix this would it? I realize it’s supposed to be better for security reasons.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T06:16:07+00:00Added an answer on May 31, 2026 at 6:16 am

    First, you should use a PreparedStatement to insert values in your query. This has many advantages including avoiding SQL Injection issues. If you use PreparedStatement, you will be avoid the errors that you are seeing now. Your code using PreparedStatement would something like this:

        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
          conn = getConnection();
          String query = "insert into table (column1,column2,column3,column4) values(?, ?, ?,?)";
    
          pstmt = conn.prepareStatement(query); 
          pstmt.setInt(1, 1); 
          pstmt.setDate(2, sqlDate); 
          pstmt.setInt(3, 3); 
          pstmt.setString(3, "test"); 
          pstmt.executeUpdate();
        } catch (Exception e) {
          //log the error messages log.error(e,e);
          //throw the actual exception upstream
        } finally {
          pstmt.close();
          conn.close();
        }
    

    I am not sure what you meant by “DB” date. If you are after the sql date object you can convert a java.util.Date object to a java.sql.Date object this way:

    java.util.Date date = new java.util.Date();
    java.sql.Date sqlDate = new java.sql.Date(date.getTime());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am relatively new to multi-threading and want to execute a background task using
I am relatively new to Perl.I have been trying to insert data to database
I'm relatively new to using jsoup, and I can't seem to find the correct
I'm relatively new in using Access. I'm creating a database for questions and answers.
I am relatively new to using jQuery and would like to use the load
I am relatively new to using MSpec and as I write more and more
I'm relatively new to NHibernate, but have been using it for the last few
Being relatively new to functional programming, I expend lots of energy wondering is this
I am relatively new to .Net and the database side of it in particular.
I'm relatively new to .NET and have being using Linq2Sql for a almost a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.