JTDS Issues with Queries “Solved”

I was developing a project and connected my android application successfully with SQL server database. I displayed the data in a list view where all the companies were listed, some of them were containing ‘Pvt. Ltd.’ in names or open brackets () and much more like dash ‘-‘ and other symbols other than the small case and uppercase letters.

But the query execution was not successful when I pass a string containing all these symbols in the query while selecting data for the specific company. So I tried running the query in SQL server and it runs successfully. So the problem I found was these symbols were passed to query but there was some problem so when I replaced all the symbols with ‘ ‘ (empty string) the query gave exact results. So this is what you have to do if you are facing the same issue while getting results due to special characters in the string.

Remove all characters from a string other than numbers and small case and upper case letters. Using pattern matcher and replace all function.

*Declare variables globally

pattern = Pattern.compile("[^a-zA-Z0-9]");
matcher = pattern.matcher(yourstring);
String newstringname= matcher.replaceAll("");

write this in your query string:
SELECT * FROM yourtable where (REPLACE(REPLACE(REPLACE(REPLACE(Replace(REPLACE(Comp_Name,' ',''),'.',''),'+',''),'(',''),')',''),',','') =
REPLACE(REPLACE(REPLACE(REPLACE(Replace(REPLACE('"+newstringname+"'' ',''),'.',''),'+',''),'(',''),')',''),',',''));

Tips:

  1. You can add more replace functions in your query if your string contains more special characters.
  2. Don’t forget to replace special characters from the original string you are passing to query.

How useful was this post?

Do not forget to rate! this helps us to Improve.

Average rating / 5. Vote count:

Read More Like This..

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Safiya Akhtar

Safiya Akhtar is Android Application Developer in India. She is currently working as a public consultant for career and education too, besides blogging and freelancing. She believes that it is never too late or too early to start what you have always dreamed of.

Your Views And Question are Welcome Here!