Inserting XML words to MySQL using Java Pattern and Matcher

Jyuedsemvyonq

New Member
I want to keep track of word usage in a group chat using MySQL database. Currently messages passed into the insertWords method is an XML string. The XML string can have special characters such as \[code\]'\[/code\] and \[code\]"\[/code\]. Is there a better way than using String.replace to convert XML formatted strings to normal messages?If my message is: \[code\]I'm bad, but they aren't that "good"\[/code\]How can I convert it to: \[code\]I'm bad, but they aren't that "good"\[/code\]My code will insert apos 2 times and quot 2 times. How do I fix this?\[code\]Pattern p = Pattern.compile("[\\w']+");PreparedStatement insertWordStmt = connection.prepareStatement("INSERT INTO word (word, count) VALUES (?, 1) " + "ON DUPLICATE KEY UPDATE count=count+1");public void insertWords(String msg) { msg = msg.toLowerCase(); try { Matcher m = p.matcher(msg); while ( m.find() ) { String word = msg.substring(m.start(), m.end()); insertWordStmt.setString(1, word); insertWordStmt.executeUpdate(); } } catch (SQLException e) { e.printStackTrace(); }}\[/code\]
 
Back
Top