Hello All, I'm not sure how to use escape characters in java. I'm writing a short java program that reads lines from a textfile and then writes them to a database. The first place this came up was the path to the database "d:\database\filename.gdb" had to be written "d:\\database\\filename.gdb". Then in the insert SQL I'm seeing examples like this int i = st.executeUpdate("insert into kwality values(\"" + arg + "\", + arg + "\")"); could anybody explain this to me. Thank and Cheers Randy
The problems telling the compiler that you want to treat some characters that it normally thinks of as part of java syntax, that in this case you want it treated as a literal. To do that you use the \ character. For: int i = st.executeUpdate("insert into kwality values(\"" + arg + "\", + arg + "\")"); This is trying to get the sentence: "insert into kwality values("arg,arg")" into the executeUpdate(string); syntax. The arg values need to be concatenated outside of the double quotes like: executeUpdate("string1" + arg + "string2" + arg + "string3"); However the embedded double quotes would end the first string at the wrong places so you need to break it down into: "insert into kwality values(\"" + arg + "\", \" "+ arg + "\")" Where the red quotes are part of the syntax and the black quotes are part of what is trying to get written and therefore need escapes.
[This message has been edited by Cindy Glass (edited September 13, 2001).]
"JavaRanch, where the deer and the Certified play" - David O'Meara
The symbol '\' has a special meaning in many programming languages, including Java. It is called the "escape character", and is used to change the meaning of the thing that follows. For example, how do you think you can create a String that looks like '"Java"'? Here, we want the double quotes to be part of the string. Well, you could try String javaString = "Java"; However, your string will end up looking like 'Java', without the double quotes. That's because Strings are normally surrounded with double quotes to let Java know it's a String. If you want your String to contain the double quotes, you need to "escape" the special meaning of '"', so Java knows you want the double quotes to be part of the string. We do that like this: String javaString = "\"Java\""; Now, how do you get the '\' as part of the string? If you don't escape the '\', Java will think that you want the escape character and not the '\' symbol. To get this, we need to escape the escape character, like such: String javaString = "\\Java\\"; Now, our string will look like '\Java\'. Clear? Jason