Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
prm aditya
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 35
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show us how you're comparing the Strings?
 
prm aditya
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code i have used to compare Strings was


 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20958
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to compare two Strings. What's the other?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic