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

The Archive Base Latest Questions

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

I am encountering some difficulties trying to, in a dynamic way, create a new

  • 0

I am encountering some difficulties trying to, in a dynamic way, create a new table using PreparedStatement. I am trying to add a unknown number of columns and their respective types and sizes. I keep getting SQL Syntax errors, but I suspect this may not be the case here.

Variables used:

  • con = Connection object
  • colNames = String[] containing column names (“person”, “email” etc)
  • colTypes = String[] containing column types (“varchar”, “int” etc)
  • colSizes = String[] containing column sizes (“100”, “11”, etc)

Should be pretty self-explanatory.

con.setAutoCommit(false);
String sql = "? ?(?),";
PreparedStatement ps = con.prepareStatement(sql);
ps.addBatch("create table " + tablename + "( ");

for (int i = 0; i < colNames.length; i++){

    if (!(i == colNames.length-1)) {
        ps.setString(1, colNames[i]);
        ps.setString(2, colTypes[i]);
        ps.setString(3, colSizes[i]);

    } else {
        String format = "%s %s(%s)";
        String lastLine = String.format(format, colNames[i], colTypes[i], colSizes[i]);
        ps.addBatch(lastLine);

    }
}

ps.addBatch(");");
ps.executeBatch();

NOTE: Yes, this is homework. I don’t want any dead giveaways, rather pointers as to in what way I am misusing some functions, which I suspect.

Best regards,
Krys

  • 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-11T06:13:10+00:00Added an answer on June 11, 2026 at 6:13 am

    You need to give the full SQL statement to addBatch. It is not a tool to construct a dynamic SQL statement. It is a tool to improve performance when running multiple statements. You don’t need it here.

    You also don’t need a PreparedStatement here, as you are not going to have bind variables (i.e. column data as opposed to column names) and are not going to run the same SQL repeatedly (but it does not hurt, either). setString and friends do not work for column or table names, just for data.

    A StringBuilder is a good tool to construct a String with variable parts.

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

Sidebar

Related Questions

I'm quite new to querying Microsoft's Active Directory and encountering some difficulties: The AD
So I am seemingly encountering some strange behavior when using NSString 's -sizeWithFont family
I've been encountering some strange behavior when trying to find a key inside a
I´m encountering this problem trying to mock some objects that receive complex lambda expressions
We have some users which are using lower-CPU powered machines and they're encountering slow
I'm encountering some troubles while trying to access some properties with parameters. Well, I
I am developing using Entity Framework and WPF, and I am encountering some errors
Greetings, I'm using appcelerator to develop some apps, but I'm encountering some problems. Every
I'm using simple_html_dom_helper so do some screen scraping and am encountering some errors. The
I am encountering some strange behavior with using the matplotlib.pyplot ginput() function to store

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.