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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:36:55+00:00 2026-06-11T09:36:55+00:00

To give a simple example of my problem: //Create database table allowing nulls. CREATE

  • 0

To give a simple example of my problem:

//Create database table allowing nulls.
CREATE TABLE test(one int not null, two int null, three int not null);

Now, in Java I have an array of objects matching the database content. I need to create an insert/update/delete statement for this database table using the array values.

Object[] arr = { 4, null, 1 };
String sql = GenereateInsert(arr);

This gets difficult though because I need to leave out null fields from the insert in advance. E.g. if two above is not null:

 INSERT INTO test(4, 2, 1);

everything is easy. But if it is null, I need the statement to be:

 INSERT INTO test(one, three) values(4, 1);

The tables I’m dealing with are 50 columns and up, so I don’t want to do this manually, it will be unmanageable. So, how do people address this problem in general? It must be a common situation.

PS: I don’t want to use a full ORM library, that seems like overkill.

  • 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-11T09:36:56+00:00Added an answer on June 11, 2026 at 9:36 am

    I would suggest something like this:

    1. Run a query like SELECT * FROM <TABLE_NAME> LIMIT 1. Use the ResultSetMetaData to retrieve the columnNames and their columnType. Store it in a HashMap and the keys in a ArrayList

    2. Now create a PreparedStatement using the keys from the ArrayList as the columnNames. I guess that would not be hard to do

    3. Now given your data in the array, the fact that you know what SQL type they’re from the HashMap, you can easily use this information to map the values that you get from your array in the ? of your generated PreparedStatement.

    Now the question is how to deal with null. Well since you know the type of column, if the value that you get is null then you can use PreparedStatement.setNull(...) to set them as null.

    Also, just wanted to point out that according to this document, there’s an important guide to the setXXX(…) of JDBC:

    6.1.5 Sending JDBC NULL as an IN parameter

    The setNull method allows a programmer to send a JDBC NULL (a generic
    SQL NULL) value to the database as an IN parameter. Note, however,
    that one must still specify the JDBC type of the parameter.

    A JDBC NULL will also be sent to the database when a Java null value
    is passed to a setXXX method (if it takes Java objects as arguments).
    The method setObject, however, can take a null value only if the JDBC
    type is specified.

    This would imply that you can send null as the value for the parameter of your setXX(...) method and you really not have to call the setNull(..) method explicitly.

    I hope this works for you!

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

Sidebar

Related Questions

to explain my problem, I'll give a simple example: My database has three tables:
Can someone give me a simple example involving threads in this manner, please. Problem
I need to create a table (possibly a view) that relates two tables, one
Could anybody be so kind to give me a simple example of reification in
Could anyone give an example program that explains Java Threads in a simple way?
http://jsfiddle.net/nicktheandroid/BcaW3/1/ I'm trying to create a simple canvas version of my jQuery example. When
The problem isn't so simple (as you would probably imagine), take the following example:
Can anyone give me an example of successful non-programmer, 5GL (not that I am
I am trying to create a simple hello world example using swapcontext() Here is
First off...here's the problem... Give an example of a directed graph G = (V,

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.