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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:51:30+00:00 2026-06-12T16:51:30+00:00

My Database Table Schema is something like DocumentID: Name1: Name2: Name3: Name4: Location1: Location2:

  • 0

My Database Table Schema is something like

DocumentID: Name1: Name2: Name3: Name4: Location1: Location2: Location3....:Organization1:..

Now I have 3 Hashset’s available having the above values (i.e one for name, one for location and one for organization)

In each single iteration of loop these hashset are being populated with above values.

At the end of each iteration the data from these hashset’s is removed and new one’s are created.

Now my problem is at each iteration I have to populate the sql table row (just 1 row each iteration) from these hashset values.

What I am not able to understand is if I have hard coded strings than simply I can use something like:

String sql = "INSERT INTO Table " +
               "VALUES ('100', 'Zara', 'Akli', '18','100', 'Zara', 'Ali', '18')";

However I need to iterate through each hashset and insert (something like above) the data of all 3 hashset’s in a single row. I am confused of how to write such statement. Remember my table is initially completely empty so I cant’t use the where clause (something like “insert into…..where documentID=”23423”)

  • 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-12T16:51:32+00:00Added an answer on June 12, 2026 at 4:51 pm

    Assuming you have created these Sets these way:

    long DocumentId
    names {"A", "B", "C"}
    location {"1", "2", "3"}
    and so on...
    

    I think the easiest is to build dinamically the SQL to execute:

    {
    ...
        StringBuilder sb = new StringBuilder("insert into mytable (");
        List<Object> params = new ArrayList<Object>();
    
        addColumnAndValue(sb, "DocumentID", docIdYouHaveSomewhere, params);
        int i = 0;
        for (String name: names)
           addColumnAndValue(sb, ",name" + i, name, params);
        i = 0;
        for (String location: locations)
           addColumnAndValue(sb, ",location" + i, location, params);
        // now close the thing
        sb.append(") values (?");
        for (i = 1; i<params.size(); i++)
           sb.append(",?");
        sb.append("=");
    
        // and now sb.toString() will contain valid SQL and the param values for a PreparedStatement will be in params array-list:
    
        PreparedStatement ps = conn.prepareStatement(sb.toString());
        for (i=0; i<params.size(); i++)
           ps.setObject(i+1, params.get(i));
    
        ps.executeUpdate(); // voi là!
        ...
    
    }
    
    private void add addColumnAndValue(StringBuilder sb, String columnName, Object value, List<Object> params) {
       sb.append(columnName);
       params.add(value);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MySQL database table called Participant that looks something like this: (idParticipant)
I execute SQL scripts to change the database schema. It looks something like this:
The basic table schema looks something like this (I'm using MySQL BTW): integer unsigned
I am working with a legacy database schema that looks like this: product_table table
I have a legacy SQL schema which looks something like this: CREATE TABLE `User`
In my SQL Server database schema I have a data table with a date
I have no control over database schema and have the following (simplified) table structure:
The database schema is CREATE TABLE sites ( site_id INTEGER PRIMARY KEY AUTOINCREMENT, netloc
Now I am using something like: dbResult = dbStatement.executeQuery(SELECT COUNT(*) FROM information_schema.tables WHERE table_schema
I wrote the database schema (only one table so far), and the INSERT statements

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.