private void GenerateAttackPayloadsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_GenerateAttackPayloadsButtonActionPerformed
    PreparedStatement ps = null, ps_attack = null;
    ResultSet rs = null;
    Connection conn = null;
    String HTTPRequest = "", ParameterName = "", ParameterValue = "", ParameterEncodedValue = "", HTTPRequestHeader = "", HTTPRequestGET = "", NewHTTPRequestGET = "", HTTPRequestPOST = "", Type = "";
    int ATTACK_PAYLOADS_ID = 0;
    int posStart = -1, posEnd = -1, ContentLength = 0, count = 0;
    ToolEncodeDecode encodeDecode = new ToolEncodeDecode();

    try {
        if ((conn = JavaDBAccess.setConn()) != null) {
            System.out.println("Connected to database " + JavaDBAccess.getDatabaseName());
        } else {
            throw new Exception("Not connected to database " + JavaDBAccess.getDatabaseName());
        }

        conn.setAutoCommit(false);

        ps = conn.prepareStatement("DELETE FROM ATTACK_PAYLOADS");
        ps.executeUpdate();
        //Inserting the Foreign Keys in the ATTACK_PAYLOADS table
        //Although there is a DISTINCT clause in the following query, there it does not work because of a annoying "bug"/"feature" of the JavaDB.
        //This error can be seen if executing the query directely in a JAvaDB console
        //I corrected the error by using a GROUP BY instead of the DISTINCT!!!!!!!!
        ps = conn.prepareStatement("INSERT INTO ATTACK_PAYLOADS (VULNERABILITY_INJECTION_RESULT,TARGET_PHP_FILES_RUNS_FUNCTION,TARGET_PHP_FILES_VARIABLE,PAYLOAD,TYPE) (SELECT VULNERABILITY_INJECTION_RESULTS.ID,TARGET_PHP_FILES_RUNS_FUNCTIONS.ID,VULNERABILITY_INJECTION_RESULTS.TARGET_PHP_FILES_VARIABLE,PAYLOADS.ID,TARGET_PHP_FILES_RUNS_FUNCTIONS_VARIABLES.TYPE FROM VULNERABILITY_INJECTION_RESULTS,TARGET_PHP_FILES_VARIABLES,TARGET_PHP_FILES_RUNS_FUNCTIONS_VARIABLES,TARGET_PHP_FILES_RUNS_FUNCTIONS,PAYLOADS WHERE TARGET_PHP_FILES_VARIABLES.ID=VULNERABILITY_INJECTION_RESULTS.TARGET_PHP_FILES_VARIABLE AND TARGET_PHP_FILES_VARIABLES.ID=TARGET_PHP_FILES_RUNS_FUNCTIONS_VARIABLES.TARGET_PHP_FILES_VARIABLE AND TARGET_PHP_FILES_RUNS_FUNCTIONS_VARIABLES.TARGET_PHP_FILES_RUNS_FUNCTION=TARGET_PHP_FILES_RUNS_FUNCTIONS.ID AND TARGET_PHP_FILES_RUNS_FUNCTIONS.MODE='INSPECTION')");
        ps.executeUpdate();
        ps.close();

        ps = conn.prepareStatement("SELECT ATTACK_PAYLOADS.ID,ATTACK_PAYLOADS.TARGET_PHP_FILES_RUNS_FUNCTION,TARGET_PHP_FILES_RUNS_FUNCTIONS_VARIABLES.TYPE,TARGET_PHP_FILES_RUNS_FUNCTIONS_VARIABLES.NAME,TARGET_PHP_FILES_RUNS_FUNCTIONS_VARIABLES.VALUE,PAYLOADS.PAYLOAD,TARGET_PHP_FILES_RUNS_FUNCTIONS.HTTP_REQUEST_HEADER,TARGET_PHP_FILES_RUNS_FUNCTIONS.HTTP_REQUEST_GET,TARGET_PHP_FILES_RUNS_FUNCTIONS.HTTP_REQUEST_POST FROM ATTACK_PAYLOADS,TARGET_PHP_FILES_RUNS_FUNCTIONS_VARIABLES,PAYLOADS,TARGET_PHP_FILES_RUNS_FUNCTIONS WHERE TARGET_PHP_FILES_RUNS_FUNCTIONS_VARIABLES.TARGET_PHP_FILES_VARIABLE=ATTACK_PAYLOADS.TARGET_PHP_FILES_VARIABLE AND PAYLOADS.ID=ATTACK_PAYLOADS.PAYLOAD AND TARGET_PHP_FILES_RUNS_FUNCTIONS.ID=ATTACK_PAYLOADS.TARGET_PHP_FILES_RUNS_FUNCTION AND TARGET_PHP_FILES_RUNS_FUNCTIONS.ID=TARGET_PHP_FILES_RUNS_FUNCTIONS_VARIABLES.TARGET_PHP_FILES_RUNS_FUNCTION AND ATTACK_PAYLOADS.TYPE=TARGET_PHP_FILES_RUNS_FUNCTIONS_VARIABLES.TYPE");
        rs = ps.executeQuery();
        while (rs.next()) {
            count++;
            ATTACK_PAYLOADS_ID = rs.getInt("ID");
            HTTPRequestHeader = rs.getString("HTTP_REQUEST_HEADER");
            HTTPRequestGET = rs.getString("HTTP_REQUEST_GET");
            HTTPRequestPOST = rs.getString("HTTP_REQUEST_POST");
            Type = rs.getString("TYPE");
            ParameterName = rs.getString("NAME");

            //Apply the Payload
            ParameterValue = rs.getString("VALUE") + " " + rs.getString("PAYLOAD");
            ParameterEncodedValue = encodeDecode.Encode(ParameterValue, false, "", "HTTP");

            if (Type.contentEquals("GET")) {
                posStart = HTTPRequestGET.lastIndexOf(ParameterName);
                posEnd = HTTPRequestGET.indexOf("&", posStart);
                if (posEnd == -1) {
                    NewHTTPRequestGET = HTTPRequestGET.substring(0, posStart) + ParameterName + "=" + ParameterEncodedValue;
                } else {
                    NewHTTPRequestGET = HTTPRequestGET.substring(0, posStart) + ParameterName + "=" + ParameterEncodedValue + HTTPRequestGET.substring(posEnd);
                }
                posStart = HTTPRequestHeader.indexOf(HTTPRequestGET);
                posEnd = posStart + HTTPRequestGET.length();
                HTTPRequestGET = NewHTTPRequestGET;
                HTTPRequestHeader = HTTPRequestHeader.substring(0, posStart) + HTTPRequestGET + HTTPRequestHeader.substring(posEnd);
            } else if (Type.contentEquals("POST")) {
                posStart = HTTPRequestPOST.lastIndexOf(ParameterName);
                posEnd = HTTPRequestPOST.indexOf("&", posStart);
                if (posEnd == -1) {
                    HTTPRequestPOST = HTTPRequestPOST.substring(0, posStart) + ParameterName + "=" + ParameterEncodedValue;
                } else {
                    HTTPRequestPOST = HTTPRequestPOST.substring(0, posStart) + ParameterName + "=" + ParameterEncodedValue + HTTPRequestPOST.substring(posEnd);
                }
            }
            posStart = HTTPRequestHeader.indexOf("Content-Length: ");
            if (posStart > 0) {
                posEnd = HTTPRequestHeader.indexOf("\r\n", posStart);
                ContentLength = HTTPRequestPOST.length();
                HTTPRequestHeader = HTTPRequestHeader.substring(0, posStart + 16) + ContentLength + HTTPRequestHeader.substring(posEnd);
            }
            HTTPRequest = HTTPRequestHeader + HTTPRequestPOST;

            ps_attack = conn.prepareStatement("UPDATE ATTACK_PAYLOADS SET ATTACK_INPUT=?, ATTACK_EXPECTED_OUTPUT=?, HTTP_REQUEST=? WHERE ID=?");
            ps_attack.setString(1, ParameterName + "=" + ParameterEncodedValue);
            ps_attack.setString(2, ParameterValue);
            ps_attack.setString(3, HTTPRequest);
            ps_attack.setInt(4, ATTACK_PAYLOADS_ID);
            ps_attack.executeUpdate();

        }
        ps.close();
        conn.commit();
        conn.close();
        AttackTextArea.setText(count + " Attack Payloads generated!\n");

    } catch (Throwable e) {
        System.out.println("Errors in GenerateAttackPayloadsButtonActionPerformed!");
        System.out.println("exception thrown:");

        if (e instanceof SQLException) {
            JavaDBAccess.printSQLError((SQLException) e);
        } else {
            e.printStackTrace();
        }
    }
}