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(), ...

Get Java™ Phrasebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.