Java MessageFormat - How to insert values ​​between single quotes?

I am having a problem using the java.text.MessageFormat object.

I am trying to create SQL insert statements. The problem is that 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 result value:

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

As you can see, the problem is that any of the target locations enclosed in single quotes is not replaced by arguments. I 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}''' . Result: "insert into <str0> values ( '{1}', '{2}', '{3}', <str4> )" . "insert into <str0> values ( '{1}', '{2}', '{3}', <str4> )" It retains the original quotation marks, but does not insert values.

How can I solve this problem? For recording, I use JDK 6u7.

+56
java
Oct 10 '08 at 2:15
source share
5 answers

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); 

Result:

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

This is what you need?

+100
Oct 10 '08 at 2:44
source share
— -

Sorry if this is not the case, but it looks like you are trying to replicate a PreparedStatement that is already in JDBC.

If you are trying to create SQL to work with a database, I suggest you look at PreparedStatement, it already does what you are trying to do (with a slightly different syntax).

Sorry if this is not what you are doing, I just thought I wanted to point it out.

+26
Oct 10 '08 at 3:44
source share

Inside String, a pair of single quotes can be used to quote any arbitrary characters except single quotes. For example, the pattern string "'{0}'" represents the string "{0}" , not FormatElement. The single quotation mark itself must be represented by double single quotation marks '' entire String . For example, the pattern line "'{''}'" interpreted as the sequence '{ (beginning of quote and left curly brace), '' (single quote) and }' (right curly brace and end of quotation), not '{' and '}' (quoted left and right braces): representing the string "{'}" rather than "{}" .

From: MessageFormat (Java SE 8 platform)

+9
Oct 10 '08 at 2:18
source share

Use triple single quote characters:

 MessageFormat messageFormat = "insert into {0} values ( '''{1}''', '''{2}''', '''{3}''', '''{4}''' )"; 
+2
Oct 10 '08 at 2:19
source share

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

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

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

0
Oct 10 '08 at 2:18
source share



All Articles