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

  • Home
  • SEARCH
  • 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 6657781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:48:46+00:00 2026-05-26T01:48:46+00:00

I am trying to program a database application with java & PostgreSQL. I have

  • 0

I am trying to program a database application with java & PostgreSQL. I have some rows with date data type. But i cant add any entries to the database with this code :

Date aDate = null;
aDate.setYear(1990);
aDate.setDate(01);
aDate.setMonth(05);

preparedStatement prep = connection.prepareStatement("insert 
into exampletable values (?,?);");
prep.setDate(1, (java.sql.Date) aDate);
prep.setDate(2, (java.sql.Date) aDate);

How can i add a date in a postgreSQL row with queries in java?

  • 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-26T01:48:46+00:00Added an answer on May 26, 2026 at 1:48 am

    It’s not clear whether or not this is your only problem, but this code is almost certainly not what you want:

    Date aDate = null;
    aDate.setYear(1990);
    aDate.setDate(01);
    aDate.setMonth(05);
    
    • It will throw a NullPointerException because you’re trying to dereference null
    • You’re then trying to set the year to 3890AD (java.util.Date is 1900-based for years)
    • You’re then setting the month to June. If you thought you were setting the month to May, think again – Date is 0-based for months
    • All the methods you’re using are deprecated – that should raise a big warning light for you
    • You’re then trying to cast aDate to java.sql.Date but there’s no sign that it is a java.sql.Date

    I would suggest:

    • Either use Joda Time as a far better date/time API, or java.util.Calendar
    • Make sure you actually create an instance before you set values
    • Probably create a new java.sql.Date later on.

    For example:

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.YEAR, 1990);
    calendar.set(Calendar.DAY_OF_MONTH, 1);
    calendar.set(Calendar.MONTH, 4); // Assuming you wanted May 1st
    
    java.sql.Date date = new java.sql.Date(calendar.getTime().getTime());
    
    // Ideally specify the columns here as well...
    PreparedStatement prep = connection.prepareStatement(
        "insert into exampletable values (?,?)");
    prep.setDate(1, date);
    prep.setDate(2, date);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Imagine we have a program trying to write to a particular file, but failing.
I'm trying to update an array element in Java. Some sort of a database
I am currently trying to program my first ajax interface using Rails. The application
I have a large tree of Java Objects in my Desktop Application and am
TL DR; Want some Java help with connecting to a truly local database (
I'm trying to test a developer's application against a SQL Server 2005 database (80)
I have created an application suite of three programs. The first program is a
I have created SQLiteOpenHelper class to help me open write the database. but i
I'm trying to implement the export functionality for my database application. I'm using C#
I'm trying to automate a program I made with a test suite via 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.