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

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.


Asked by: Max123 | Posted: 28-01-2022






Answer 1

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?

Answered by: Stella309 | Posted: 01-03-2022



Answer 2

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.

Answered by: Aldus169 | Posted: 01-03-2022



Answer 3

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 )

Answered by: Miller725 | Posted: 01-03-2022



Answer 4

Use triple single quote characters:

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

Answered by: Julia327 | Posted: 01-03-2022



Answer 5

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.

Answered by: Ryan808 | Posted: 01-03-2022



Similar questions

java - Logging complexity and concern about MessageFormat performance

My application uses log4j for logging and usually I do logging by checking whether a particular level is enabled and then log like the following if(Logger.isApplicationDebugEnabled()){ Logger.logApplicationDebug("something"+values); } Now this if check reduces our branch coverage during jUnit testing. In order to overcome this, my friend suggested getting rid of "if" checks for loggi...


escaping - Can I escape braces in a java MessageFormat?

I want to output some braces in a java MessageFormat. For example I know the following does not work: MessageFormat.format(" public {0} get{1}() {return {2};}\n\n", type, upperCamel, lowerCamel); Is there a way of escaping the braces surrounding "return {2}"?


messageformat - How do I format a long integer as a string without separator in Java?

Simple question, but I'll bet that asking on here will probably be more straight forward than trying to understand the documentation for MessageFormat: long foo = 12345; String s = MessageFormat.format("{0}", foo); Observed value is "12,345". Desired value is "12345".


java - Test for null parameter in .properties file for MessageFormat

Is it possible, with resource bundles and MessageFormat to have the following result? when I call getBundle("message.07", "test") to get "Group test" when I call getBundle("message.07", null) to get "No group selected" Every example I found on the Internet is with planets, with files on the disk and so on. I only need to chec...


java - Aligning messageformat on printing a JTable

I'm using this for the moment to print out my table, and it works. But I'm not really happy with the layout of the messageformatting, I would like to have both pagenumber and date in the footer, and date format aligned to the left side of the table, and page to the right. How can I do that? Been reading some stuff about overriding the PrintTable method, but seems to get pretty complex from what I've read. H...


java - Enhanced MessageFormat?

I've been using MessageFormat.format() for a while, but there's one thing that I find annoying. Everytime you declare a parameter in a message, you have to know the position of the mapped argument. e.g. MessageFormat.format("{0} is annoying {1}.", "this", "indeed") Is there a class that is the same as MessageFormat in every other way but lets you omit the ...


How do I retain the encoding in argument when using MessageFormat in Java

I am trying to use MessageFormat as follows, String downloadsUrl = "http://host/downloads?tags={0}"; Object[] formatArgs = {"sequence%20diagram"}; String url = new MessageFormat(downloadsUrl).format(formatArgs); However, when I look at the final url string, it is, http://host/downloads?tags=sequence diagram ...


java - MessageFormat formatting pattern Query

i am Using DisplayTag table library to render my tables, which gives the option to specify messageFormat patterns for the data. i am having some hard time in finding the correct format's following are the formats i am trying to write 1. given a double print its currency representation without decimal points e.g 25.25 as $25 2. if a negative double is given it must print -$25 currently its printing ($2...


java - Fill preformatted text file to send email - MessageFormat alternative

I Used MessageFormat to format the contents of a file with parameters and get so a formated string with correct parameters. (I used it to format email body.finally I had one file per email body, the application needs to send a lot of different emails, so I got a lot of preformatted body files) So far, I had six parame...


java - How to customize currency formatting in MessageFormat in ICU4J

I have a system that generates a lot of documents. Its contents are defined in ResourceBundles. I want to customize the way MessageFormat prints currency values. Sometimes I want it to display currencies without fraction digits (but not always). This should be working as expected but it is not: System.err.println( com.ibm.icu.text.MessageFormat.format( "{0,number,\u00A4#}", ...






Still can't find your answer? Check out these amazing Java communities for help...



Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)



top