# Working with User Input

Pass user input to a query as a ? parameter instead of joining it into the SQL string, so nobody can run a SQL injection attack on your database.

Journeyman · Java · SQL · Updated November 16, 2026

By Gregor Dzierzon

User input is critical in application development. A user may want to search for products in a certain category, or enter shipping or payment information. Your application needs to accept their input and use it to interact with the database.

However, user input is also where applications can become vulnerable to various hacking attacks. If programmed incorrectly, your code could leave you open to a SQL injection attack. Hacksplaining's [interactive SQL injection lesson](https://www.hacksplaining.com/lessons/sql-injection/start) shows how one works.

The JDBC library makes it easy to stop SQL Injection attacks. The basic rule is, you should never trust input that you receive from an end user, because it may be code in disguise.

## Vulnerable (Dangerous) Code

In the following example we prompt the user to enter a product name. We will then use the name to search for the details of that product.

```java
Scanner userInput = new Scanner(System.in);

System.out.print("Please enter the product name: ");
String name = userInput.nextLine().strip();

// Define the base query
String query = """
    SELECT product_id
        , product_name
        , unit_price
    FROM products
    WHERE product_name =
    """;

// Concatenate the user input onto the query
query = query + "'" + name +"'";
```

This code leaves you vulnerable to a SQL Injection attack because the user could type malicious code that would be executed on your database.

## Safe Code

Let's modify this code to protect our database from malicious users. In your query, add a **?** as a placeholder for any values that are a result of user input. The `JdbcTemplate` will protect your database from user input by ensuring that it cannot be executed like a regular SQL command.

When executing the query, pass the user input as parameters. The `JdbcTemplate` will replace the **?** with the value provided by the user.

```java
Scanner userInput = new Scanner(System.in);

System.out.print("Please enter the product name: ");
String name = userInput.nextLine().strip();

// Define the full query
String query = """
    SELECT product_id
        , product_name
        , unit_price
    FROM products
    WHERE product_name = ?;
    """;

// Pass the parameter(s) as values when executing the query
SqlRowSet row = jdbcTemplate.queryForRowSet(query, name);
```

This code is no longer vulnerable to a SQL Injection attack.

## Do it: search for a product by name, safely

The store's search box asks the user for a product name, then looks that product up. Right now the method glues the name the user typed into the SQL, so a user can type SQL of their own. Make it safe with a `?` placeholder.

The method gets two values:

- `jdbcTemplate`: the object that runs the query.
- `name`: the text the user typed, such as `Chai`.

The practice box can't reach MySQL, so the bottom of `Main.java` has a stand-in for `JdbcTemplate`, named `StandInJdbcTemplate`. It runs nothing and gives nothing back: its `queryForRowSet(sql, values...)` keeps the SQL it was given, and the values passed after the SQL, so the tests can read them with `getLastSql()` and `getLastValues()`. Leave it as it is.

For example, when the user types `Chai`:

| The stand-in's | Holds |
|---|---|
| `getLastSql()` | `SELECT product_id, product_name, unit_price FROM products WHERE product_name = ?;` |
| `getLastValues()` | the list `Chai`: one value |

When the user types an attack such as `x'; DELETE FROM products; --`, the SQL is the very same, and the one value is that text, which is now only a name to compare with.

**Your job:** change the method `searchProducts(StandInJdbcTemplate jdbcTemplate, String name)` so the user's text never goes into the SQL.

- Run exactly this SQL, whatever the user typed: `SELECT product_id, product_name, unit_price FROM products WHERE product_name = ?;`
- Pass `name` to `queryForRowSet` as the one value after the SQL.

Practiced live on the lesson page: write the Java right there and check it.

## Cheat sheets

- [Working with User Input](https://www.ask-codey.com/cheat-sheets/working-with-user-input-cheat-sheet)

Source: https://www.ask-codey.com/lessons/working-with-user-input
