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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:49:03+00:00 2026-05-14T04:49:03+00:00

I am working on a group project for class and we are trying out

  • 0

I am working on a group project for class and we are trying out CheckStyle.

I am fairly comfortable with Java but have never touched JDBC or done any database work before this.

I was wondering if there is an elegant way to avoid magic number errors in preparedStatement calls, consider:

        preparedStatement = connect.prepareStatement("INSERT INTO shows "
                + "(showid, showtitle, showinfo, genre, youtube)"
                + "values (default, ?, ?, ?, ?);");
        preparedStatement.setString(1, title);
        preparedStatement.setString(2, info);
        preparedStatement.setString(3, genre);
        preparedStatement.setString(4, youtube);
        result = preparedStatement.executeUpdate();

The setString methods get flagged as magic numbers, so far I just added the numbers 3-10 or so to the ignore list for magic numbers but I was wondering if there was a better way to go about inserting those values into the statement. I also beg you for any other advice that comes to mind seeing that code, I’d like to avoid developing any nasty habits, e.g. should I be using Statement or is PreparedStatement fine? Will that let me refer to column names instead? Is that ideal? etc…

Thanks!

  • 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-14T04:49:04+00:00Added an answer on May 14, 2026 at 4:49 am

    Create an utility method which does something like this:

    public static void setValues(PreparedStatement preparedStatement, Object... values) throws SQLException {
        for (int i = 0; i < values.length; i++) {
            preparedStatement.setObject(i + 1, values[i]);
        }
    }
    

    And use it as follows:

    setValues(preparedStatement, title, info, genre, youtube);
    

    or

    Object[] values = {
        title, info, genre, youtube
    };
    
    setValues(preparedStatement, values);
    

    More “best practices” with regard to basic JDBC coding can be found in this article.

    Hope this helps.

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

Sidebar

Related Questions

I'm working on a Java project for class that stores workout information in a
Currently, I am working on a project that is trying to group 3d points
I'm working with a group of other programmers on an open source project built
I remember working on a project with a group of developers and they always
I've been working with a small group of people on a coding project for
Background: We have an offshore group working up a Silverlight 2 prototype for us.
G'day, I am working with a group of offshore developers who have been using
My background is primarily as a Java Developer, but lately I have been doing
I've been at this for hours but it just isn't working. I'm trying to
I'm trying to get the ADT working in Eclipse, but I'm getting random hangs

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.