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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:08:50+00:00 2026-05-22T16:08:50+00:00

I want to make a single method to make inserts on different tables, I

  • 0

I want to make a single method to make inserts on different tables, I want the user to
put as parameters the name of the table and a list of the data to be inserted.

Something like this:

public void insertar(String tabla, LinkedList data) 
{
    String alta = "insert into " + tabla + " values(";
    for(int i = 0; i < datos.size(); i++) {
        alta = alta + "?,";
    }
    alta = alta.substring(0, alta.length() - 1) + ")";
    System.out.println(alta);
    try 
    {
        PreparedStatement insertar = con.prepareStatement(alta);
    } 
    catch (SQLException ex) 
    {
    }
}

My problem is that I can’t figure out how to set the values once the
String is made because the size and type of the data can change depending of
the table i’m using.

Is there a way to do this?, or I need to make it different?

Thanks in advance!

  • 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-22T16:08:51+00:00Added an answer on May 22, 2026 at 4:08 pm

    Your linked list should probably contain something other than String‘s. Have you considered creating a type of Binding class that knows how to go about setting the values on your preparedStatement? I am envisioning something like this:

    public abstract class Binding {
        public abstract void bind(PreparedStatement ps) throws SQLException;
    }
    
    public class IntegerBinding extends Binding {
        private int value;
        private int index;
    
        public IntegerBinding(int _index, int _value) {
            index = _index;
            value = _value;
        }
    
        public void bind(PreparedStatement ps) throws SQLException {
            ps.setInt(index, value);
        }
    }
    

    You can use the value you are trying to apply to the PreparedStatement and the index into the PreparedStatement to create your concrete Binding implementation. Of course you would need to write your own Binding subclasses to encompass the other types of data you are interested in using.

    Here is a slight modification to your code to be able to use the ideas above:

    public void insertar(String tabla, List<Binding> bindings) 
    {
        String alta = "insert into " + tabla + " values(";
        for(int i = 0; i < datos.size(); i++) {
            alta = alta + "?,";
        }
        alta = alta.substring(0, alta.length() - 1) + ")";
        System.out.println(alta);
        try 
        {
            PreparedStatement insertar = con.prepareStatement(alta);
            for(Binding binding : bindings)
            {
                binding.bind(insertar);
            }
            insertar.execute();
        } 
        catch (SQLException ex) 
        {
        }
    }
    

    I think that this is a pretty handy solution and I hope it will help you out. Good Luck!

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

Sidebar

Related Questions

I want to make many inserts in a single SQL statement, pretty much like
I want to make a generic method that will take either a single object
I want to make a really simple iphone app: one screen with a single
I want to make a simple game: 2d, single-player, without tons of animations and
I want to make a copy of an ActiveRecord object, changing a single field
I have style sheet with a class name changebackgroundcolor i want make change in
I am traversing a HTML document using javascript DOM. I want make a list
I want to make a table in SqlServer that will add, on insert, a
guys, I want to set actions to touches. If people make single touch -
I am using HttpWebRequest.BeginGetRequest() to make 500 asynchronous HTTP requests from a single method.

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.