O'Reilly logo

Java™ Phrasebook by Timothy Fisher

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using a Prepared Statement

PreparedStatement stmnt =
											conn.prepareStatement(
											"INSERT into users values (?,?,?,?)");
											stmnt.setString(1, name);
											stmnt.setString(2, password);
											stmnt.setString(3, email);
											stmnt.setInt(4, employeeId);
											stmnt.executeUpdate( );

To create a prepared statement in JDBC, we use a PreparedStatement object in place of a Statement object. We pass the SQL into the prepareStatement() method on the Connection object. This creates a PreparedStatement object. When using a prepared statement, data values in the SQL statement are specified with a question mark. The actual values for these question mark placeholders are set later using the PreparedStatement set methods. The set methods available include setArray(), setAsciiStream(), ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required