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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:14:13+00:00 2026-05-20T10:14:13+00:00

my code is written in java, and I am really new to java, so

  • 0

my code is written in java, and I am really new to java, so i hope my explanations are correct:
i have a java written web service that works with a data base.
the data base types can be PostgreSQL and mysql.
so my webservice works with the JDBC connection for both data bases.
one of my data base tables is table urls,
for postgressql it is created like this:

CREATE TABLE urls ( 
id serial NOT NULL primary key, 
url text not null,   
type integer not null);

for mysql it is creates like this:

CREATE TABLE IF NOT EXISTS URLS ( 
id INTEGER primary key NOT NULL AUTO_INCREMENT, 
url varchar (1600) NOT NULL,   
type INTEGER NOT NULL );

when I try inserting data to this table I use an entity called urls:
this entity has:

    private BigDecimal id;
    private String url;
    private BigInteger type;

when I try to insert values to the urls table I assign values to the urls entity, while leaving the id as NULL since it is AUTO_INCREMENT for mysql and serial for postgres.
the query works for my sql, but fails for postgress.
in the postgres server log I can see the following error:

null value in column "id" violates not-null constraint

cause I sends NULL as the id value.
I found out that in order for the query to work I should use this query:

INSERT INTO URLS(ID, TYPE, URL) VALUES(DEFAULT, 1, 'DED'); or this one:
INSERT INTO URLS(TYPE, URL) VALUES(1, 'DED'); or this one:

instead of this one, that I use:

INSERT INTO URLS(ID, TYPE, URL) VALUES(NULL, 1, 'DED');

so my question is,

  1. how do I assign the DEFAULT value to a BigDecimal value in java ?
  2. is removing the id from my entity is the right way to go ?
  3. how can I make sure that any changes I do to my code wont harm the mysql or any other data base that I will use ?
  • 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-20T10:14:14+00:00Added an answer on May 20, 2026 at 10:14 am

    If you specify the column name in the insert query then postgres does not take the default value. So you should use your second insert query.

    INSERT INTO URLS(TYPE, URL) VALUES(1, 'DED');
    

    This syntax is correct for both postgres and MySQL.

    This should resolve your question (1) and (3). For (2) DO NOT delete the id field from your entity. This id is going to be your link to the database row for a specific object of the entity.

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

Sidebar

Related Questions

I have written code that automatically creates CSS sprites based on the IMG tags
I have a code snippet written in PHP that pulls a block of text
I have written a program that reads in a File object (really an XML
I am trying to use custom font in android. I have written java code
I have written code below.but it will print this exception and i really don't
I have java code written in eclipse and I want to show some basic
I am working on some code written by a co-worker who no longer works
What Visual Studio settings and .emacs macros improve the likelihood that code written on
I am working on a software development project that uses code written primarily in
I have just been re-working an old bit of compiler-like code written using bison.

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.