Special character Symbol replacing ''�" - java

how to replace this symbol from java coding .. when i copied it into here its look like "�"
but in the coding it looks like a
small square box
if i saved the code its asking to save as 'utf-8' any one help me? Actually this is saved in the MYSQL Table while accession iam getting the problem
i will explain the scenario here..
I have to replace the single quotes and new line and double quotes
characters in a string in java..
when i was iterating, i found a string like 'RS approves President?s
Rule', i've checked in the database it is saved as 'RS approves
President[small square box]s Rule '....i first think that 'single
qoute' and tried to replace,
how can i replace this symbol..

Can you give more info on the flow of the string?
like:
input - webform
java - parses it or plain inserts it into the DB?
MySQL - stores the data
Make sure the input is set to (allow) UTF-8?
That the string in the Java is handled as UTF-8
Also that MySQL is set to UTF-8

Related

Encode a string to be safe for html element fields

From client to server, my app correctly accepts text of any kind (I'm using prepared statements to stay safe on the database end). When I display text from server to client I'm running into a problem now with text that has quotation marks, and perhaps there are other cases I haven't thought of or encountered yet.
label:"<%=myObj.getText()%>"
is being translated into:
label:""Hello World""
I think what I need is for the quotation marks in the data to be escaped like so: \"
Is there a class that will do this encoding already so I don't have to write my own parser?
Searching through StackOverflow, I found a promising answer in java.net.URLEncoder that encodes special characters. In reading through it's documentation, though, I found one translation I didn't want:
The space character " " is converted into a plus sign "+".

Storing Unicode and special characters in MySQL tables

My current requirement is to store Unicode and other special characters, such as double quotes in MySQL tables. For that purpose, as many have suggested, we should use Apache's StringEscapeUtils.escapeJava() method. The problem is, although this method does replace special characters with their respective unicodes (\uxxxx), the MySQL table stores them as uxxxx and not \uxxxx. Due to this, when I try to decode it while fetching from the database, StringEscapeUtils.unescapeJava() fails (since it cannot find the '\').
Here are my questions:
Why is it happening (that is, '\' are skipped by the table).
What is the solution for this?
Don't use Unicode "codepoints" (\uxxxx), use UTF8.
Dont' use any special functions. Instead announce that everything is UTF-8 (utf8mb4 in MySQL).
See Best Practice
(If you are being provided \uxxxx, then you are stuck with converting to utf8 first. If your real question is on how to convert, then ask it that way.)
`

Java - Determine difference between user entering \n and pressing enter in html form

Searched the web for this but not sure I'm asking the question correctly. I have a web form with a textarea. Users can type what ever they want (can paste emails, etc). When they submit, I escape things like newline so that when I store in a PostgreSQL db (json column type) it saves correctly. That all works fine. However, if a user type something like c:\foo\bar\notworking.txt the \n is treated like a new line so I end up with
c:\foo\bar
otworking.txt
If I look at the string (user hits enter) coming into the controller (Spring based) I see \n.
Question is, how do I differentiate between someone typing \n and hitting the enter key?
easiest solution:
String s = ...;
s = s.replaceAll("\\","\\\\");
Then the opposite after you load it back in
In order to insert raw text into a JSON column, you need to encode the text as JSON, meaning:
The following fails «badly»:
"c:\foo\bar\notworking.txt"
encodes to (assuming non-ASCII needs encoding, which it does if the DB is not UTF-8):
"The following fails \u00ABbadly\u00BB:\r\n \"c:\\foo\\bar\\notworking.txt\""
Side note: As a Java String literal, that would be:
String json = "\"The following fails \\u00ABbadly\\u00BB:\\r\\n \\\"c:\\\\foo\\\\bar\\\\notworking.txt\\\"\"";
You will then of course use PreparedStatement or Spring, such that SQL escaping and SQL Injection issues are non-existent.

Error while parse an XML with cyrillic serbian chars anda save into DB

I've a complex XML file and I've to parse it with Java to get some text inside some tags.
This is done correctly, but there are some rows with cyrillic characters (serbian) and in XML appear in correct mode, when I get it with Java in another one, and when I save it into Oracle, in another one!
How I can elaborate and save this kind of data in the correct cyrillic format from xml to oracle? Thanks.
First: read http://www.joelonsoftware.com/articles/Unicode.html
Second: you don't get a "simple string", you have a file. Which contains bytes. That given an encoding represent a string. When you read it in as a string, you need to specify that encoding or things will get corrupted.
Once you have a java.lang.String, it is an actual unicode representation and encoding-independent but when you want to push that string to a database, you once again need to think about encoding because at some point somewhere, the database will have to transform that string to bytes to store it.
Additionally: never "trust" an editor when it comes to examining encoding issues. They almost always have automagic stuff to make stuff work so something that "looks fine" might actually be corrupt or only valid given the assumptions that that specific editor made.

Newline escape sequence not unescaping in proper way in Java

I am fetching a String from SQL server 2008 database into my Java code and trying to print it. Unfortunately the newline escape sequence is not automatically converted into newline.
I know the reason is we are not putting the string inside the double quotes in the Database table. Below is the sample value stored in the varchar column :
Remarks \nTestRemarks Issue\nTestIssue\n\nRegards \nSunny
When I am printing it on log file it is printing along with \n. My application convention doesn't allow me to store String within double quotes inside Database varchar column, therefore I chose to explicitly unescape it using Apache StringEscapeUtils.unescapeJava(str). Unfortunately, the result is that 1st and last newline escape sequence is successfully converted to newlines, but rest all newline escapes remain unchanged. If I put space before the newline escape sequence in the DB, then it gets recognized and converted,but not otherwise. Can you please help how I solve this situation.
How about doing the opposite once you retrive it, ie StringEscapeUtils.escapeJava(str) or repeat StringEscapeUtils.unescapeJava(str) after you retrieve it from the database. Either one might work.
my setup is working in wierd manner. for some reason after system restart and eclipse restart and tomcat restart, everything seems to work seamlessly. closing the answer as non-issue

Categories