- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to escape backslashes in MySQL with JDBC?
To escape backslashes, use PreparedStatement while inserting records. Let us first create a table −
mysql> create table DemoTable1904 ( ClientId int, ClientName varchar(20), ClientAge int ); Query OK, 0 rows affected (0.00 sec)
The Java code is as follows −
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; public class EscapeBackslashesDemo { public static void main(String[] args) { Connection con = null; PreparedStatement ps = null; try { con = DriverManager.getConnection("jdbc:mysql://localhost:3306/web?" + "useSSL=false", "root", "123456"); String query = "insert into DemoTable1904(ClientId,ClientName,ClientAge) values(?,?,?) "; ps = con.prepareStatement(query); ps.setInt(1, 1001); ps.setString(2, "David Miller"); ps.setInt(3, 35); ps.executeUpdate(); System.out.println("One row is inserted....."); } catch (Exception e) { e.printStackTrace(); } } }
This will produce the following output −
Let us check the table records −
mysql> select * from DemoTable1904;
This will produce the following output −
+----------+--------------+-----------+ | ClientId | ClientName | ClientAge | +----------+--------------+-----------+ | 1001 | David Miller | 35 | +----------+--------------+-----------+ 1 row in set (0.00 sec)
- Related Articles
- How to escape single quotes in MySQL?
- How to escape apostrophe (') in MySQL?
- What is JDBC SQL Escape Syntax Explain?
- How to escape parentheses in MySQL REGEXP clause and display only specific values with parentheses?
- MySQL query to replace only the backslashes from folder path?
- How can we escape special characters in MySQL statement?
- How to create a table with auto-increment column in MySQL using JDBC?
- How to establish connection with JDBC?
- How do backslashes work in Python Regular Expressions?
- How to use try-with-resources with JDBC?
- How can we match the values having backslashes, like ‘ab’, from MySQL column?
- Example to create a table with all datatypes in MySQL using JDBC?
- Resolve Unknown database in JDBC error with Java-MySQL?\n
- How to create a database in MySQL using a JDBC API?
- How to escape square brackets in jQuery selector?

Advertisements