Issues arise when attempting to insert quotes within a JSON object for database insertion

I am currently facing an issue where I need to store a JSON object in the database to retrieve it later and manipulate it using JavaScript. However, I am encountering a problem I have never faced before. When I store the JSON object as a String and then try to retrieve it, the quotes are lost, making it impossible to escape the String and convert it back to a JSON object.

For example, the JSON string that is sent:

 {"footer":"this is a double quote \"","header":""}

The String that is written to the database:

 {"footer":"this is a double quote "","header":""}

This results in an error when trying to convert it back to JSON.

In my code:

// My SQL
String sql = "UPDATE table SET val='%s' WHERE 1";

// The JSON string to be stored (This value is stored in a variable, not as a literal)
String jsonString = // {"footer":"this is a double quote \"","header":""};

// Create JDBC statement
con.prepareStatement(String.format(sql, jsonString)).execute();

// The above SQL statement generates the toString();
UPDATE table SET val='{"footer":"this is a double quote \"","header":""}' WHERE 1

// The following String is recorded in the database
{"footer":"this is a double quote "","header":""}

My table is configured with the following settings: utf8 utf8_general_ci

Why is this happening and what can I do to solve it?

Thank you.

Answer №1

To ensure proper data security, it is important to utilize prepared statements:

http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html

Here is an example of how prepared statements can be implemented:

PreparedStatement statement = null;
String query = "UPDATE table SET val=? WHERE 1";

statement = connection.prepareStatement(query);
statement.setString(1, jsonData);
statement.executeUpdate();
connection.commit();

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the appropriate response to send to the user in a web application?

I am currently developing a web application that utilizes AngularJS for the frontend, a REST API, and MongoDB as the backend, all powered by Node.js. Background to the challenge: One key requirement is to authenticate users using token-based authenticati ...

Is there a way to showcase my information on flash cards using JavaScript?

Currently, I am developing a full stack application that utilizes JavaScript on both the front and back end. This application allows users to create their own flashcards set. Upon clicking "View Cards," the data is fetched and the question-answer pair is d ...

Show real-time server responses using jQuery while waiting for the AJAX request to complete

I have a challenge in creating a function that displays the server response while waiting for Ajax to complete. Here's the scenario: I have selected 10 checkboxes. When I click the submit button, Ajax is triggered. On the server side, there is a loo ...

Concentrate and select non-interactive elements with your mouse

Is it possible to set tab index on non-form elements like the div tag? I tried using tab index, but when the div is focused and the enter button is tapped, the ng-click event associated with the div tag is not triggered. <div tabindex="0" role="butto ...

Transfer the file image stored in a MySQL database to an excel spreadsheet

Having an issue with capturing image data input from a PHP and HTML database to Excel. I am trying to display the images in a specific size within an Excel file. Below is the PHP code used to create the table: <?php header("Content-type: application/o ...

Using jQuery, we can replace all `<span>` elements with `<img>` elements and then verify if the images have been successfully loaded

Similar Inquiry: jQuery check if image is loaded I find myself in a scenario where the following HTML structure is utilized: <div class="image"> <img class="" src="content-images/1.jpg"> <span class="slide" rel="content-images/ ...

Mastering React: Implementing default values in components using Ajax

I am facing an issue while trying to create a form for editing existing values. The problem arises with the initial data retrieved from an ajax request, causing my component to render twice - first with empty values and then again when the data is populate ...

Retrieve the public URL from the index.html file following the build process

In my project, I utilized ReactJS by creating an app with the command npx create-react-app my-app. To build the project, I used yarn build. Within the package.json file, the scripts section appears as follows: "scripts": { "start": "react-scripts sta ...

Tips for utilizing ngDoc comments in routeConfig

I am currently working on documenting my Angular WebApp using ngdocs and grunt. I am facing a challenge with the complexity of my routing system and feel that it needs more attention in terms of documentation. Is there a way to document the route configur ...

Eliminate items from the array if the property of an object includes any of the provided substrings

In an attempt to minimize the objects within arrays that have a property "Title" containing any substring listed in a separate JSON file, I have been struggling with filtering the arrays based on the object's property. However, I believe using the red ...

Waiting for Node/Express to process request

I'm fairly new to the world of Node.js and so far, it's been amazing. However, I've run into a minor issue while running node (with express) locally - Once I reach the 10th request, each subsequent one hangs and shows as Pending in the Chrom ...

Exploring jQuery Ajax: A Guide to Verifying Duplicate Names

When I apply the blur function to a textbox to check for duplicate names using jQuery AJAX, it works perfectly. Here is the code snippet: function checkForDuplicate(data){ $.post("test.php", {name: data}, function (data){ if(data){ ...

Error: Conversion of "2018-01-01-12:12:12:123456" to a date is not possible for the 'DatePipe' filter

<td>{{suite.testSuiteAttributes && suite.testSuiteAttributes.modifiedTimestamp | date: 'yyyy-MM-dd' }} </td> I am trying to display the date in the "05-Feb-2018 11:00:00 PM CST" CST format, but I keep getting an ...

Determining the Clicked Button in React When Multiple Buttons are Present

Within my functional component, I have a table with rows that each contain an edit button. However, when I click on one edit button, changes are applied to all of them simultaneously. How can I identify which specific button was clicked and only apply chan ...

Customize the jQuery datepicker by assigning a class to the first 17 days

How can I apply a class to only the first 17 days on a jquery datepicker calendar? I've attempted the following code, but it ends up adding the class to every day... beforeShowDay: function(date) { for (i = 0; i < 17; i++) { return [t ...

What is the best way to attach an EventListener to all DOM elements with a particular class?

Within my DOM, I have dynamically created spans that all have the class "foo". Utilizing TypeScript, I aim to attach an onClick event to each of these spans post-creation. Here is my current approach: var foos = document.body.querySelectorAll(".foo"); f ...

Can a single static variable be shared among all connections on the server?

I am currently working on a turn-based Chinese checkers game. I have added an onload function in the body that sends an ajax request to the server to obtain the player number for the connection. However, I am encountering an issue where the response always ...

Prevent the browser from autofilling password information in a React Material UI textfield when it is in focus

I am currently utilizing React Material UI 4 and I am looking to disable the browser autofill/auto complete suggestion when focusing on my password field generated from `TextField`. Although it works for username and email, I am encountering issues with d ...

The live() function is causing issues with my ajax request

Within my webpage, I have a link with an onclick() event that should display a div containing a date input text named "datepicker0", followed by another div with the id of "bContent". I've included the script below to implement a date filter on the d ...

The Backbone.js template does not automatically inherit attributes from the model

Hey there, I'm currently working on setting up a simple SPA using Backbone for testing purposes. However, when I try to render my view, the Underscore template doesn't seem to be picking up any attributes from my model. I've been trying to t ...