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 8717201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:15:19+00:00 2026-06-13T06:15:19+00:00

I want to insert a record into PostgreSQL database. Most of it’s fields are

  • 0

I want to insert a record into PostgreSQL database. Most of it’s fields are String, but there is also java.sql.date, and BIGINT. How can i do that? My code doesn’t seem to work.

    public Record(RecordFields data)
    {
        CreateNewRecord(data);
    }

        private void CreateNewRecord(RecordFields recordFields)
        {
            try
            {    

                    String addRecord = "INSERT INTO " + GlobalFields.TABLE + " VALUES (? , ? , ? , ? , ? , ? , ? , ? , ?);";

                    Connection conn = DataBase.Connect();

                    PreparedStatement query = conn.prepareStatement(addRecord);
                    query.setLong(1, recordFieldsID);
                    query.setString(2, recordFields.surname);
                    query.setString(3, recordFields.name);
                    query.setString(4, recordFields.sex);

                    DateTime date = recordFields.bornDate.toDateTime();
                    query.setTimestamp(5, new Timestamp(date.getMillis()));

                    query.setString(6, recordFields.adress);
                    query.setString(7, recordFields.contact);
                    query.setString(8, recordFields.insurance);
                    query.setString(9, recordFields.commentary);

                    query.executeUpdate(addRecord);
        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }


    }    

table generator:

public static void CreateTable()
    {
        try
        {
            Connection conn = DataBase.Connect();

            try
            {
                Statement stat = conn.createStatement();

                String createTable = "CREATE TABLE " + GlobalFields.TABLE 
                    + "(id BIGINT PRIMARY KEY,"
                    + "surname TEXT,"
                    + "name TEXT,"
                    + "sex CHAR(1),"                 
                    + "bornDate DATE,"
                    + "adress TEXT,"
                    + "contact TEXT,"
                    + "insurance TEXT,"
                    + "commentary TEXT);";

                stat.executeUpdate(createTable);
            }
            finally
            {
                conn.close();
            }
        }
        catch(SQLException e)
        {
            e.printStackTrace();
        } 
    } 

public class RecordFields
{
public RecordFields()
{
}

public long id;
public String surname;
public String name;
public String sex;
public MutableDateTime bornDate;
public String adress;
public String contact;
public String insurance;
public String commentary;

}

  • 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-06-13T06:15:20+00:00Added an answer on June 13, 2026 at 6:15 am
    stat.executeUpdate(createTable);
    

    is the culprit. On PreparedStatements executeUpdate takes no parameters.

    So, modify that line to – just as expained by Tom Anderson

    stat.executeUpdate();
    

    and you’re done

    Also,

    INSERT table VALUES (values)
    

    makes your code unnecessarily brittle. Use

    INSERT INTO table (fieldlist) VALUES (valuelist)
    

    instead.

    And also 2:

    DateTime date = recordFields.bornDate.toDateTime();
    query.setTimestamp(5, new Timestamp(data.getMillis()));
    

    is a rather convoluted version of saying

    query.setTimestamp(5,new Timestamp(recordFields.bornDate.getMillis()));
    

    EDIT: regarding Date in postgressql

    What Tom says is that to fill a PostgreSQL DATE type column you can either use a java.sql.date type variable (as opposed to the “common” java.util.Date), or a javax.sql.TimeStamp. But that’s totally unrelated to the present problem ( which is caused by the parameter in the call to executeUpdate ) and what you do here (set it using a Timestamp type variable) is OK.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to insert a record into a MySQL database table from a formulaire.
Am having 6 fields in the record set I want to insert into the
I made iPad application in which, I want to insert record into database table,
When I insert a record into my database I want to put the date
Possible Duplicate: insert contacts into database but does not want to duplicate already existing
I want to insert a date record to an Access database. Here is my
i want to insert all processes that running into listbox, and also how to
I want to insert multiple values into a row, but I want to make
I want to insert a record into MySQL that has a non-ASCII Unicode character,
I want to have an 'updateinfo' table in order to record every update/insert/delete operations

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.