Insert record using Prepared Statement in Java

PreparedStatement is an interface and has pre-compiled SQL statements. PreparedStatements are used to efficiently execute the same SQL statement multiple times. They are also used to set the parameters in query dynamically.

Steps to Insert record using Prepared Statement

1. Get a connection Object

2. Create a prepareStatement object

3. Add parameters (if required)
As you can see we have ? in the query. So thats a parameter and we need to pass values for that parameter.

The first parameter in the setInt method is the parameterIndex and the 2nd param is the parameter value for the query.

Please note: ParameterIndex starts from 1.

4. execute the query using executeUpdate.

Full Example : Insert record using Prepared Statement

Output

Reference

1. Statement executeUpdate()

It's only fair to share...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn