Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?


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.


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 )


Use triple single quote characters:

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