Storing Submitted Data in Database
Last update: January 25, 2024
- Topics:
- Adaptive Forms
CREATED FOR:
- Experienced
- Developer
It is recommended to use MySQL 8 as your database as it has the support for JSON data type. You will also need to install the appropriate driver for MySQL DB. I have used the driver available in this location https://mvnrepository.com/artifact/mysql/mysql-connector-java/8.0.12
To store the submitted data in database, we will write a servlet to extract the bound data and the form name and store. The complete code to handle the form submission and store the afBoundData in the database is given below.
We created custom submit to handle the form submission. In this custom submit’s post.POST.jsp we forward the request to our servlet.
To learn more about custom submit pleas read this article
com.adobe.aemds.guide.utils.GuideSubmitUtils.setForwardPath(slingRequest,“/bin/storeafsubmission”,null,null);
package com.aemforms.json.core.servlets;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.sql.DataSource;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.json.JSONException;
import org.json.JSONObject;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(service = Servlet.class, property = {
"sling.servlet.methods=get", "sling.servlet.methods=post",
"sling.servlet.paths=/bin/storeafsubmission"
})
public class HandleAdaptiveFormSubmission extends SlingAllMethodsServlet {
private static final Logger log = LoggerFactory.getLogger(HandleAdaptiveFormSubmission.class);
private static final long serialVersionUID = 1L;
@Reference(target = "(&(objectclass=javax.sql.DataSource)(datasource.name=aemformswithjson))")
private DataSource dataSource;
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException {
JSONObject afSubmittedData;
try {
afSubmittedData = new JSONObject(request.getParameter("jcr:data"));
// we will only store the data bound to schema
JSONObject dataToStore = afSubmittedData.getJSONObject("afData").getJSONObject("afBoundData")
.getJSONObject("data");
String formName = afSubmittedData.getJSONObject("afData").getJSONObject("afSubmissionInfo")
.getString("afPath");
log.debug("The form name is " + formName);
insertData(dataToStore, formName);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void insertData(org.json.JSONObject jsonData, String formName) {
log.debug("The json object I got to insert was " + jsonData.toString());
String insertTableSQL = "INSERT INTO aemformswithjson.formsubmissions(formdata,formname) VALUES(?,?)";
log.debug("The query is " + insertTableSQL);
Connection c = getConnection();
PreparedStatement pstmt = null;
try {
pstmt = null;
pstmt = c.prepareStatement(insertTableSQL);
pstmt.setString(1, jsonData.toString());
pstmt.setString(2, formName);
log.debug("Executing the insert statment " + pstmt.executeUpdate());
c.commit();
} catch (SQLException e) {
log.error("Getting errors", e);
} finally {
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (c != null) {
try {
c.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public Connection getConnection() {
log.debug("Getting Connection ");
Connection con = null;
try {
con = dataSource.getConnection();
log.debug("got connection");
return con;
} catch (Exception e) {
log.error("not able to get connection ", e);
}
return null;
}
}
To get this working your system please follow the following steps
- Download and unzip the zip file
- Create AdaptiveForm With JSON Schema. You can use the JSON schema provided as part of this article assets. Make sure you submit action of the form is configured appropriately. Submit action needs to be configured to the “CustomSubmitHelpx”.
- Create a schema in your MySQL instance by importing the schema.sql file using the MySQL Workbench tool. The schema.sql file is also provided to you as part of this tutorial assets.
- Configure the Apache Sling Connection Pooled DataSource from the Felix web console
- Make sure you name your datasource name “aemformswithjson”. This is the name that is used by the sample OSGi bundle that is provided to you
- Refer to the image above for properties. This is assuming you are going to use MySQL as your Database.
- Deploy the OSGi bundle(s) that are provided as part of this article assets.
- Preview the form and submit.
- The JSON data is stored in the database that was created when you imported “schema.sql” file.
Experience Manager
- Overview
- Best practices to follow
- Setting up OKTA authentication with AEM Author
- Creating your first Adaptive Form
- 1 - Introduction and Setup
- 2 - Create Adaptive Form template
- 3 - Create form fragment
- 4 - Create Adaptive Form
- 5 - Configuring root panel and adding child panels
- 6 - Adding components to People panel
- 7 - Adding table to income panel
- 8 - Configuring assets panel
- 9 - Using custom functions and code editor
- Handling Form Submissions
- Useful Integrations
- Creating OSGi bundle
- Adaptive Forms
- Installing AEM Forms on Windows
- Installing 32 bit packages on Linux
- Prefill Service in Adaptive Forms
- Populate Adaptive Form using query parameters
- QR Code in AEM Forms
- Custom Submit in Adaptive Forms
- Captcha in Adaptive Forms
- Custom Functions in Rule Editor
- Embedding Adaptive Form in web page
- Creating array of strings
- Item Load Path in AEM Forms
- Using Set Value in XML in OSGI Workflow
- Using Set Value in JSON in OSGI Workflow
- Send Email Component in AEM Forms Workflow
- Generating DocumentOfRecord using API
- Displaying Inline Images
- Displaying DAM images inline
- Using GeoLocation API
- Using Transaction Reporting
- User Profile Data Integration in AEM Forms
- Microsoft Dynamics with AEM Forms
- Rule Editor Enhancements
- Restricting Access to Rule Editor
- Theme Editor Enhancements
- Form Editor Enhancements
- Automated Testing Of Adaptive Forms
- Automated Testing Of Adaptive Forms
- Integrating LDAP with AEM Forms
- Using Service User in AEM Forms
- Tagging and Storing DoR on Adaptive Form Submission
- Populating table with the results from Form Data Model Service Invocation
- Capturing workflow comments
- Storing Submitted Form Data in DB
- Inserting form attachment in DB
- Modify Data Source Configuration
- Override Form Data Model Properties
- Form Data Model Service in AEM 6.4 Workflow
- Handling Error Messages in Form Data Model Service Step
- Form Data Model Service in AEM 6.5.1 Workflow
- Form Data Model without Data Source
- Computed Form Data Model Element
- Setting up JDBC Data Source in AEM Forms
- JDBC Based Form Data Model
- Create Associations between 2 entities in Form Data Model
- Using Form Data Model’s invoke service in Rule Editor
- Post Binary Data to AEM DAM Using Form Data Model
- Creating Re-Usable Workflow Models
- Custom Process Step AEM Workflow
- Pre-Populating HTML5 Forms
- Form Data Model with Salesforce
- Adding Items Dynamically to Choice Component
- Fill a PDF form with ChatBot
- Core- Components based forms
- Select and assemble DAM folder content
- Prefilling adaptive form using form data model
- UI Tips and Tricks
- Customizing inbox
- Email form attachments
- Prefilling form with data and attachments
- Storing and Retrieving Adaptive Form
- Import data from pdf file
- Exporting Submitted Data in CSV Format
- HTML5 Forms
- AEM Forms and Acrobat Sign
- Introduction
- Set up SSL
- Create Acrobat Sign API Application
- Create Acrobat Sign Cloud Configuration
- Create Workflow to send document for signing
- Create and Configure Adaptive Form
- Configure Adaptive Form for single signer
- Configure Adaptive Form for two signers
- Configure Automated Forms Conversion Service
- Convert PDF Form into Adaptive Form
- Configure converted Adaptive Form
- Using XDP templates with Acrobat Sign
- Using Acrobat Sign helper methods
- Sign Multiple Forms
- Custom workflow steps
- Custom workflow component
- Variables in AEM Workflow
- Review form data
- Storing and Retrieving Form Data in MySQL DB
- Triggering AEM Workflow on PDF Form submission
- Trigger AEM workflow on Mobile Form Submission
- Querying Form Submissions
- Listing Custom Asset Types in Forms Portal
- AEM Forms With Marketo
- AEM Forms with Adobe Campaign Standard
- Welcome Kit
- Headless Forms API
- React App with Forms and Acrobat Sign
- Document Services
- Types of PDF forms
- Embed pdf in adaptive form
- PDF’s in carousel component
- Using Assembler Service in AEM Forms
- XDP Stitching with Assembler Service
- Using Watched Folder in AEM Forms
- Using Barcode Service in AEM Forms
- Using PDFG Service in AEM Forms
- Encrypt PDF with permissions password
- Using Output and Forms Service in AEM Forms
- Using xdp fragments in output service
- ECMA script to generate pdf with fragments
- Generating multiple pdf’s from one data file
- Generate PDF on Mobile Form Submission
- Generate PDF on Core Component based form submission
- Generate Interactive PDF from Mobile Form
- Generate Interactive DoR from Adaptive Form
- Configure Reader Extensions Credential
- Apply Usage Rights to PDF on XDP Rendition
- Apply Usage Rights to Uploaded PDF
- Certify Documents
- Assemble Form Attachments
- Useful Utility Services
- Forms Designer
- AEM Forms Workbench
- Interactive Communications for Print Channel
- 1 - Introduction
- 2 - Set up Tomcat
- 3 - Create Data Source
- 4 - Create Form Data Model
- 5 - Create XDP Layout using Forms Designer
- 6 - Create Document Fragment
- 7 - Create Print Channel Document
- 8 - Adding Content to Target Areas
- 9 - Configuring Line Chart
- 10 - Adding Table to Print Channel
- 11 - Using Watched Folder to Generate Documents
- 12 - Opening Agent UI on Form Submission
- Save and Retrieve Draft Letters
- Interactive Communications
- Using Batch API
- MultiSeries Charts
- Generate Print Channel Documents From Submitted Data
- Using Reducer Functions
- Two Column Layout in Print Channel Documents
- Using Table Component in AEM Forms Print Channel Document
- Generating Interactive Communications Document for Print Channel using Watch Folders
- Customize text editor
- Interactive Communications For Web Channel
- 1 - Introduction
- 2 - Set up Tomcat
- 3 - Create Data Source
- 4 - Create Form Data Model
- 5 - Creating Web Channel Document Template
- 6 - Creating Document Fragment
- 7 - Creating Interactive Communication Document
- 8 - Adding Text and Images
- 9 - Configuring Line chart
- 10 - Configuring Table
- 11 - Configuring Column Chart
- 12 - Configuring Pie Chart
- 13 - Delivery Of Web Channel Document
- Email Delivery of Web Channel Document
- Troubleshooting
- Document Services: Steps to troubleshoot DDX related issues
- Document Services: Steps to troubleshoot PDFA conversion issues
- Document Services: How to debug font related issue for Forms and Output Service
- Document Services: How to embed fonts for Forms and Output Service
- Document Services: How to enable performance logging to debug Forms and Output Service
- Document Services: How to increase aries transaction timeout for conversion of large files
- Document Services: How to run load tests using FormsIVS and OutputIVS
- Document Services: How to update custom XDPs and data files in FormsIVS and OutputIVS
- Document Services: Steps to enable verbose ORB trace
- Document Services: Steps to tweak bmc pool size for XMLFormService part1
- Document Services: Steps to tweak bmc pool size for XMLFormService part2
- Document Services: Steps to tweak parameters on JMX console
- Enable merging of large xml data files with template
- PDFG: How to change the transaction timeouts in PDFG
- PDFG: How to update CORBA timeout in PDFG
- PDFG: How to update the timeouts in PDFG
- PDFG: Steps to add PDFG user in windows server 2016
- PDFG: Steps to check and install 32 bit libraries and their dependencies
- PDFG: Steps to check folder permission on windows server 2016
- PDFG: Steps to enable strace on unix
- PDFG: Steps to install required 32bit Microsoft Visual C++ redistributables
- PDFG: Steps to run and interpret output of System Readiness Tool
- PDFG: Steps to troubleshoot acrobat specific issues
- PDFG: Steps to troubleshoot HtmltoPDF issues on windows
- PDFG: Steps to troubleshoot HtmltoPDF specific issues on unix(PhantomJS)
- PDFG: Steps to troubleshoot HtmltoPDF specific issues on unix(WebKit conversion)
- PDFG: Steps to troubleshoot multi user PDFG Conversion on unix
- PDFG: Steps to troubleshoot multi user PDFG Conversion on windows
- PDFG: Steps to troubleshoot OpenOffice specific issues on unix
- PDFG: Steps to troubleshoot OpenOffice specific issues on windows
- Document Security: How to apply policy created using document security module in Acrobat
- User Management: How to add users or groups in AEM Forms JEE
- User Management: How to increase UM Session timeout
- Workbench: Connecting workbench using https
- Workbench: Gathering Workbench logs from help menu
- Workbench: How to enable debug output for Workbench
- Workbench: How to tweak workbench memory parameters
- Workbench: How to tweak workbench performance parameters
- Designer: Dependency of Designer on C++ redistributable
- Designer: How to enable HTML Preview
- Designer: How to enable PDF Preview
- Designer: How to install Designer Patches
- JEE Foundation: How to change the value of entropy
- JEE Foundation: How to change transaction timeout of any service in AEM Forms JEE
- JEE Foundation: How to purge process records from the Job Manager table
- JEE Foundation: How to run process purge from command line
- JEE Foundation: Steps to bootstrap manually
- JEE Foundation: Steps to capture debug logs for mobile forms
- JEE Foundation: Steps to mitigate ForcedDisconnectException
- General: How to add management user for JBoss admin console
- General: How to capture HAR logs
- General: How to capture heap dumps of a JVM on a unix server
- General: How to capture heap dumps of a JVM on a windows server
- General: How to capture network logs
- General: How to capture Thread dumps of a JVM on a unix server
- General: How to capture Thread dumps of a JVM on a windows server
- General: How to generate a self signed certificate
- General: How to make web service call from HTML5 form
- General: Steps to enable debug logging for any classes in AEM Forms OSGI
- General: Steps to enable debug logging for any classes in JBOSS Application Server
- General: Steps to enable debug logging for any classes in Websphere Application Server
- General: Steps to enable SSL for AEM Forms OSGi
- General: Steps to encrypt keystore password to be used in lc_turnkey.xml
- General: Steps-to-reset-default-password-for-users-in-AEM-Forms-JEE
- General: Steps-to-reset-default-password-for-users-in-AEM-Forms-OSGI
- General: Steps to set up gemfire locators in a cluster and perform related configuration