Parameterized Queries Java Guide: How to Prevent SQL Injection with Parameterized Queries

Ajay Monga
3 min readMay 5, 2024

--

SQL Injection Prevention for Java Developers: Parameterized Queries Explained

SQL injection (SQLi) remains one of the most dangerous web application security vulnerabilities.

There is no doubt that SQL Injection was introduced two decades ago as also mitigation of SQL Injection but still, it is a common vulnerability found in applications. For the last 25 years, it is always in OWASP Top 10.

Decades Later: Why SQL Injection Remains a Top Security Concern💉

Little intro of SQLi:

SQL injection (SQLi) occurs when malicious user input is directly inserted into an SQL query. Attackers can manipulate input fields on websites or applications to inject harmful SQL code and can extract sensitive data (usernames, passwords, financial data, etc.), modify or delete existing data, run system commands on the database server, potentially compromise the entire server.

s

Understanding SQL Injection

Let’s take a simple example of how SQL injection works. example of a login form:

SELECT * FROM users WHERE username = 'user_input' AND password = 'password_input';

If a user enters their username as “admin” and their password as “password”, all’s well.

However, if an attacker enters “‘ OR ‘1’=’1” as the username, the query transforms into:

SELECT * FROM users WHERE username = '' OR '1'='1' AND password = 'password_input';

Since ‘1’=’1' is always true, the attacker gains access, bypassing authentication.

One of the most popular remediations of SQL injection is parameterized query

What Are Parameterized queries?

Parameterized queries (also known as prepared statements) involve predefining an SQL query template with placeholders for user-supplied values. These placeholders are replaced with actual data at execution time, but the database engine treats them as data, not as executable code.

This technique enforces a clear separation between the SQL query structure and the dynamic data elements. Even if an attacker somehow includes malicious code within the input, the database engine will not interpret it as part of the SQL command.

How Parameterized Queries Work

  • Query with Placeholders: Instead of directly embedding user input into your SQL queries, you create a query template with placeholders:
String query = "SELECT * FROM customers WHERE username = ? AND password = ?";
  • Separate Parameters: You pass the actual user input as separate parameters alongside the SQL query:
String username = "ajay";
String password = "Whdu83uK";
String query = "SELECT * FROM customers WHERE username = ? AND password = ?";
PreparedStatement stmt = conn.prepareStatement(query);
stmt.setInt(1, Integer.parseInt(username));
stmt.setString(2, password);
  • PreparedStatement: The PreparedStatement object precompiles the SQL query.
  • setString() The setString() method safely sets the value of the first placeholder, ensuring that username is treated only as data, not as SQL code.
  • Execution: The executeQuery() method executes the query with the sanitized parameters.
  • Sanitization and Execution: The database engine treats provided parameters only as data, not as sql code. This fundamentally neutralizes SQLi attempts.
    The placeholder in the query is replaced with the parameter value, and the safely constructed query is executed.

Advantages of Parameterized Queries

  • Prevention of SQL Injection: The primary advantage; it’s exceptionally difficult to execute successful SQLi attacks using parameterized queries.
  • Improved Performance: The database can cache and reuse the precompiled query structure, potentially optimizing query execution times.
  • Code Readability: Parameterized queries often make SQL code cleaner and easier to understand by separating logic and data.

Parameterized queries are your most reliable shield against common SQL injection attacks. By understanding how they work and consistently applying them in your Java code, you significantly enhance the security of your database applications.

Let me know if you would like more in-depth explanations or examples in additional programming languages!

--

--

Ajay Monga

Security @ ADP | DevSecOps | AI Security | SAST | Shift Left |My writing is clear & concise, making complex security concepts understandable to a broad audience