0

Hello i am using prepared statement to insert the values into mysql.

I am facing an issue in an string which is inserting as "c2a054656e6e6973c2a0" for value " Tennis " and i tried to use trim to trim the whitespace in front of the "Tennis" it did not work .From this question Why trim is not working? i tried using the solution poNumber.replace(String.valueOf((char) 160), " ").trim(); and it worked. can any one give solution for this?

SAMPLE CODE

CREATE TABLE `news1` (
  `article_id` int(11) NOT NULL AUTO_INCREMENT,
  `article_title` varchar(500) COLLATE utf8_bin NOT NULL,
  `article_subcategory` varchar(255) COLLATE utf8_bin DEFAULT NULL,
  PRIMARY KEY (`article_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=22 ;

//Sample category will be "Home » Tennis"

 String category = item.getArticle_category();
                String categoryArray[] = category.split("»");
                preparedStatement = connect
                        .prepareStatement("INSERT INTO news1 VALUES(default,?,?)");
                System.out.println(categoryArray[2].replace(String.valueOf((char) 160), " ")
                        .trim());
                preparedStatement.setString(1, item.getArticle_title());
                preparedStatement.setString(2, categoryArray[2]);
Community
  • 1
  • 1
Kathick
  • 1,395
  • 5
  • 19
  • 30

1 Answers1

0

you are doing this categoryArray[2].replace(String.valueOf((char) 160), " ") .trim() in sys out but you do preparedStatement.setString(2, categoryArray[2]); in your statement

String temp = categoryArray[2].replace(String.valueOf((char) 160), " ") .trim(); 
System.out.println(temp);

and use temp in

preparedStatement.setString(2, temp);
shazin
  • 21,379
  • 3
  • 54
  • 71
  • No. i am scrapping data from an website which has the char "»" and split it and use it in db while making ans SYSOUT it works it prints the string while inserting it fails – Kathick Feb 13 '13 at 07:08