Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Why are INSERT SQL statements concatenated?

  1. #1
    Junior Member
    Join Date
    Feb 2021
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Why are INSERT SQL statements concatenated?

    A relatively newbie/wannabe developer here. I've been using various tutorials and some online examples as my current learning tools and have come across something that I'm curious about but haven't really been able to find an answer for.

    Hopefully I can explain it in a manner that makes sense. I've been looking at some basic CRUD tutorials and what I've seen in most tutorials is when a SQL INSERT statement is included it is concatenated. Is there a valid reason the statements are concatenated, instead of more standard INSERT statement as per the below examples? If anyone has any information on the reasoning for this it would be greatly appreciated.

    Example 1 of a concatenated INSERT statement:
    String addUserSQL = "INSERT INTO users " + "VALUES (1, 'First', 'Last', 25)";

    Example 2 of a concatenated INSERT statement:
    String addUserSQL = "INSERT INTO users " + "(id, firstname, lastname, age) VALUES " + "(?, ?, ?, ?);";

    Example of a standard INSERT statement:
    String addUserSQL = "INSERT INTO users (id, firstname, lastname, age) VALUES (1, 'First', 'Last', 25)";

    Thanks in advance.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Why are INSERT SQL statements concatenated?

    I do not see any reason for using String concatenation to build a single String from two or more constant content Strings.

    The only reason I can see is to keep the source line short enough to show the whole line without requiring shifting to see the end.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Why is my 2nd insert not working ?
    By tangara in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 7th, 2017, 12:03 PM
  2. [SOLVED] LinkedList -- insert()
    By javaStooge in forum Object Oriented Programming
    Replies: 33
    Last Post: July 24th, 2014, 05:52 AM
  3. Questions about if statements and if-else statements
    By chakana101 in forum Java Theory & Questions
    Replies: 5
    Last Post: March 23rd, 2014, 05:37 PM
  4. Can a null value be concatenated with an existing String? Exemple:
    By javaDruide in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 16th, 2014, 02:13 PM