• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Escape Characters?

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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[0] + "\", + arg[1] + "\")");
could anybody explain this to me.
Thank and Cheers
Randy
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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[0] + "\", + arg[1] + "\")");
This is trying to get the sentence:
"insert into kwality values("arg[0],arg[1]")"
into the executeUpdate(string); syntax.
The arg[] values need to be concatenated outside of the double quotes like:
executeUpdate("string1" + arg[0] + "string2" + arg[1] + "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[0] + "\", \" "+ arg[1] + "\")"
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).]
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Jason Ford
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good explanation, Cindy. You beat me to it. I'm just now starting to build up the typing speed I had before I made the switch from QWERTY to Dvorak.
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you use a PreparedStatement rather than a Statement in the database query, it will take care of all of the special character substitution for you.
Saves a lot of hassle.
Dave.
 
He's giving us the slip! Quick! Grab this tiny ad!
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic