Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Java's PreparedStatement work?

I am planning to replace repeatedly executed Statement objects with PreparedStatement objects to improve performance. I am using arguments like the MySQL function now(), and string variables.

Most of the PreparedStatement queries I have seen contained constant values (like 10, and strings like "New York") as arguments used for the ? in the queries. How would I go about using functions like now(), and variables as arguments? Is it necessary to use the ?s in the queries instead of actual values? I am quite confounded.

like image 685
Epitaph Avatar asked Jan 07 '09 02:01

Epitaph


1 Answers

If you have a variable that comes from user input, it's essential that you use the ? rather than concatenating the strings. Users might enter a string maliciously, and if you drop the string straight into SQL it can run a command you didn't intend.

I realise this one is overused, but it says it perfectly:

Little Bobby Tables

like image 87
Marcus Downing Avatar answered Oct 07 '22 06:10

Marcus Downing