title: Writing complex trigger using JDBC, How to properly escape
body: <p>This is a trigger I wrote in MySql WorkBench to save newly created row as json.</p>
  
  <pre><code>delimiter |
  
  CREATE TRIGGER insert_movies AFTER INSERT ON movies
   FOR EACH ROW
   BEGIN
    SET @json = " { ";
    SET @json = CONCAT(@json,"\"","tableName","\"",":","\"","movies","\"",",");
    SET @json = CONCAT(@json,"\"","action","\"",":","\"","insert","\"",",");
    SET @json = CONCAT(@json,"\"","NewData","\"",":");
    SET @json = CONCAT(@json," { ");
    IF NEW.id is not null then
    SET @json = CONCAT(@json, "\"","ID","\"", ":", "\"",replace(NEW.id,'"','\\"'), "\"");
    END IF;
    if New.title is not null then 
    SET @json = CONCAT(@json, ",");
    SET @json = CONCAT(@json, "\"","TITLE","\"", ":", "\"",replace( NEW.title,'"','\\"'), "\"");
    END IF;
    SET @json = CONCAT(@json," } ");
    SET @json = CONCAT(@json, "}");
    INSERT INTO updated(d) values(@json);
  END;
  </code></pre>
  
  <p>It worked. But I want to use JDBC to write this. </p>
  
  <pre><code> Connection con = ds.getConnection();
   Statement sm = con.createStatement();
   StringBuilder triBuilder = new StringBuilder();
   triBuilder.append("CREATE TRIGGER insert_movies");
   ....
   triBuilder.append(" CONCAT(@json,\"\\\"\","tableName",\"\\\"\",\":\",\"\\\"\","movies","\"",",") ");
  </code></pre>
  
  <p>I tried but the escaping things become too complex and I can't get it to work. Help me please.</p>