69

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.

TM.
  • 108,298
  • 33
  • 122
  • 127
  • 2
    Turns out that `''{0}''` does work after all. I just needed to do a full clean of my build directory, as my build process wasn't correctly updating and I didn't realize it. Oops! – TM. Oct 10 '08 at 02:55
  • [This answer](http://stackoverflow.com/questions/17569608/format-a-message-using-messageformat-format-in-java#17569639) explains the reason why the extra quote is needed. – Steve Chambers Oct 29 '15 at 16:14

5 Answers5

123

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?

serg
  • 109,619
  • 77
  • 317
  • 330
  • 1
    I had tried this before, but it wasn't working. Something was screwed up with the build process... after a full clean -> rebuild, this method worked! *groan*. Thanks! – TM. Oct 10 '08 at 02:53
27

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.

Aidos
  • 2,753
  • 3
  • 27
  • 31
11

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 )

Pang
  • 9,564
  • 146
  • 81
  • 122
Chris Boran
  • 4,781
  • 2
  • 25
  • 25
  • Thanks. I read this in the Java docs already but I was confused (And unable to get what I wanted from it), Although there's definitely a big possibility that I was doing it wrong. Care to elaborate? – TM. Oct 10 '08 at 02:21
  • 1
    I think the important part is: "A single quote itself must be represented by doubled single quotes '' throughout a String" - the rest only explains how single quotes and curly braces are parsed in combination. Quite a sentence... – wemu Oct 12 '17 at 13:43
  • This should be the right answer as it explains why the single quote needs to be doubled. – Kiril Aleksandrov Jun 28 '18 at 06:49
2

Use triple single quote characters:

MessageFormat messageFormat = "insert into {0} values ( '''{1}''', '''{2}''', '''{3}''', '''{4}''' )";
Brian Duff
  • 406
  • 3
  • 12
  • 1
    This just results in `"insert into values ( '{1}', '{2}', '{3}', )"`. So it has the single quotes now but the argument values still aren't being inserted. – TM. Oct 10 '08 at 02:32
0

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.

billjamesdev
  • 14,554
  • 6
  • 53
  • 76
  • Thanks. This is my current workaround but I'd prefer something a bit faster/cleaner. – TM. Oct 10 '08 at 02:23