Java MessageFormat - How can I insert values between single quotes?

Java

Java Problem Overview


I'm having a problem using the java.text.MessageFormat object.

I'm trying to create SQL insert statements. The problem is, when I do something like this:

MessageFormat messageFormat = "insert into {0} values ( '{1}', '{2}', '{3}', {4} )";
Object[] args = { str0, str1, str2, str3, str4 };
String result = messageFormat.format(args);

I get this for the value of result:

"insert into <str0> values ( {1}, {2}, {3}, <str4> )"

As you can see, the problem is that any of the target locations that are enclosed by single quotes do not get replaced by arguments. I have tried using double single quotes like this: ''{1}'' and escaped characters like this: \'{1}\' but it still gives the same result.

edit: I forgot to mention that I also tried '''{1}'''. The result is: "insert into <str0> values ( '{1}', '{2}', '{3}', <str4> )". It is keeping the original quotes around but still not inserting the values.

How can I resolve this issue? For the record, I am using JDK 6u7.

Java Solutions


Solution 1 - Java

I just tried double quotes and it worked fine for me:

MessageFormat messageFormat = new MessageFormat("insert into {0} values ( ''{1}'', ''{2}'', ''{3}'', {4} )");
Object[] args = {"000", "111", "222","333","444","555"};
String result = messageFormat.format(args);

The result is:

insert into 000 values ( '111', '222', '333', 444 )

Is this what you need?

Solution 2 - Java

Sorry if this is off the side, but it looks like you're trying to replicate the PreparedStatement that is already in JDBC.

If you are trying to create SQL to run against a database then I suggest that you look at PreparedStatement, it already does what you're trying to do (with a slightly different syntax).

Sorry if this isn't what you are doing, I just thought I would point it out.

Solution 3 - Java

> Within a String, a pair of single quotes can be used to quote any arbitrary characters except single quotes. For example, pattern string "'{0}'" represents string "{0}", not a FormatElement. A single quote itself must be represented by doubled single quotes '' throughout a String. For example, pattern string "'{''}'" is interpreted as a sequence of '{ (start of quoting and a left curly brace), '' (a single quote), and }' (a right curly brace and end of quoting), not '{' and '}' (quoted left and right curly braces): representing string "{'}", not "{}".

From: MessageFormat (Java Platform SE 8 )

Solution 4 - Java

Use triple single quote characters:

MessageFormat messageFormat = "insert into {0} values ( '''{1}''', '''{2}''', '''{3}''', '''{4}''' )";

Solution 5 - Java

First thing that came to mind was to change str1, str2, str3 to have the single quotes around them.

Object[] args = { str0, "'" + str1 + "'", "'" + str2 + "'", "'" + str3 + "'", str4 };

Then, of course, remove the single-quotes from your query string.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTM.View Question on Stackoverflow
Solution 1 - JavasergView Answer on Stackoverflow
Solution 2 - JavaAidosView Answer on Stackoverflow
Solution 3 - JavaChris BoranView Answer on Stackoverflow
Solution 4 - JavaBrian DuffView Answer on Stackoverflow
Solution 5 - JavabilljamesdevView Answer on Stackoverflow