0

I am trying to store comments from a facebook post. There are 25 comments and I can get them all in a Json array and print to console with a for loop in Java. But when I try to store them into a MySql database, something occurs with 14th element of array. Program just ends without any error or exception. It just stores 13 elements to database and prints them to console. If it is not connected to database it prints all 25 elements to console. I control it with an if clause, changing the comment content like this:

String content;
  for (int j = 0; j < array.size(); j++){   
        if (j==13)
           {
             content = "changed content";
           }
         else
           {
             content = ((JSONObject)array.get(j)).get("message").toString();
           }

And I could store all the elements into my db. but of course 14th elements content was stored as "changed content."

I store them with this code:

String addComments = "INSERT INTO comments VALUES("+commentID+",'"+commentFrom+"','"
+content+"',"+likeCount+",'"+createdDate+"',"+postID+")";
                       stmt.executeUpdate(addComments);

I really have no idea why this problem occurs. I'm waiting for your help.

greenmarker
  • 1,599
  • 1
  • 21
  • 29
Toolbox
  • 21
  • 8
  • You might want to log the INSERT statements to console to take a look at the created statement. This could tell you in what way the syntax might be wrong. It is likely that some character in the original post is not escaped as it should be. That is why you should never just concatenate Strings into SQL statements. – Tobias Kremer Jul 23 '15 at 09:00
  • What was the original value of the 14th element, it might contain character needed to be escaped first before the INSERT call. – owenrb Jul 23 '15 at 09:25
  • " Es un proyecto de formación que trae muchos beneficios, partiendo de su base que es que demuestra el papel relevante del uso de las TIC'S en la educación, el aporte de conocimientos a los docentes para incorporar las TIC's como herramientas de trabajo colaborativo, la intercomunicación y la conectividad multicultural. Este proyecto ha logrado que muchos docentes se integren a las nuevos procesos , logrando un nuevo escenario donde cada vez son mas los profesionales de la educación que se integran y generan una educación más reflexiva, crítica " This is the original text of 14th element – Toolbox Jul 23 '15 at 09:51
  • There is an un-escaped single quote in `TIC's commo...` – owenrb Jul 23 '15 at 10:02
  • How to escape single quote: http://stackoverflow.com/questions/9596652/how-to-escape-apostrophe-in-mysql Also check your comment field length. Given this example, it must hold at least 550 characters. – owenrb Jul 23 '15 at 10:03

0 Answers0