![]() |
In this article, we can learn how to prevent SQL injection in JDBC by using Java programming. Basically, SQL injection is a type of security vulnerability It occurs when hackers or attackers can change or manipulate the SQL query, and this can lead to unauthorized access to resources and also perform different malicious activities once get access by using SQL injection. To avoid this situation, we have one solution in JDBC with Java which is PreparedStatement. Handle SQL injection using PreparedStatementNow, we will explain how to prevent SQL injection in JDBC by using PreparedStatement. This PreparedStatement can be able to allow us to write SQL statements in the form of placeholders, The placeholders always represented by using the symbol “?”. For a better understanding of the concept, we will explain this with one example. PreparedStatement syntax:PreparedStatement preparedStatement = connection.prepareStatement(sql_query)
Example to Handle SQL injection in JDBC Using PreparedStatementIn this example, we insert some rows in MySQL Database. For this first you need to create one database after that create one table in that database, for that we have created one database called books. In this Database, we create one table That is book, Now, we have provided the images for Table schema. The above picture is help us to describe the table.
In the below Java code, we have taken input from user after that we have inserted that data into Table. Java
Output:
Now we have provided picture for data in the table. Initially we have one row in database after we have inserted one more row. After this the output will look like below: Explanation of the above Program:
|
Reffered: https://www.geeksforgeeks.org
Java |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |