aspose file tools*
The moose likes JDBC and the fly likes String Comparision not correct when comparing unicode data from Database to Textfield data in GUI Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "String Comparision not correct when comparing unicode data from Database to Textfield data in GUI" Watch "String Comparision not correct when comparing unicode data from Database to Textfield data in GUI" New topic
Author

String Comparision not correct when comparing unicode data from Database to Textfield data in GUI

prm aditya
Greenhorn

Joined: Feb 09, 2012
Posts: 5

I have written an application to compare the data stored in Mysql database with the data entered in the textfield in Swings. If both do not match i want to throqw a popup.
But the comparision is not taking place properly. Even if both match the pop up is being shown.
I am doing the comparision for the text in the language Telugu which is stored in the database with charset UTF8 and entered in the GUi using a TTF font.
A result set has been used to retrieve values as String and the equals() method in String is used to check for equality.

Need help to compare the data


Thanks
Deepak Rao
Ranch Hand

Joined: Jan 24, 2012
Posts: 31

Hi Aditya,

Try converting the text in the GUI to the same format as the db string,UTF-8 before comparing it with equals().

byte[] strbytes = oldstring.getBytes("UTF-8");
String newstring = new String(strbytes, "UTF-8");

Thanks
Deepak
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

Can you show us how you're comparing the Strings?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
prm aditya
Greenhorn

Joined: Feb 09, 2012
Posts: 5
The code i have used to compare Strings was


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

After line 8 in that code, both ResultSet objects are set to before the first result. You must first call next() on both, and only if both return true can you check the Strings.
If you don't mind the following syntax you need to change only line 10:
If either ResultSet has no records or the strings don't match your insert statement's values will be set.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18903
    
    8

Deepak Rao wrote:Try converting the text in the GUI to the same format as the db string,UTF-8 before comparing it with equals().

byte[] strbytes = oldstring.getBytes("UTF-8");
String newstring = new String(strbytes, "UTF-8");


No, don't waste your time with that code. After it's run the two strings newstring and oldstring will always contain the same data, so it's a pointless waste of time. Not to mention it clutters up your code.
prm aditya
Greenhorn

Joined: Feb 09, 2012
Posts: 5
I tried putting rs.next() after each initialisation of result set objects but still i see that the comparision is not happening correctly....

I am using a text field for which i set a font in Swings . When i am comparing it with UTF-8 data do i need to do anything else to check for equality


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

prm aditya wrote:I tried putting rs.next() after each initialisation of result set objects but still i see that the comparision is not happening correctly....

What are the exact values of the two Strings?

Swings

Swing is a proper noun.
prm aditya
Greenhorn

Joined: Feb 09, 2012
Posts: 5
What are the exact values of the two Strings?

The String that i am comparing is

రామిశెట్టి రమేష్

That is a name written in the language telugu. present in both the tables.

Thanks
Aditya
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

You have to compare two Strings. What's the other?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String Comparision not correct when comparing unicode data from Database to Textfield data in GUI