The java.util PreparedStatement setInt() method is a part of the Java JDBC API which allows a developer to set the specified parameter to the given Java int value. This method is used to set a value for the placeholder '?' in the SQL query.
Syntax:
setInt(int parameterIndex, int x) throws SQLException
Parameter(s):
parameterIndex – an int value that represents the index of the parameter to be set. It starts at 1.
x – an int value that represents the desired value to be set to the specified parameter.
Examples:
1. Setting value to the first parameter of a PreparedStatement.
PreparedStatement pstmt = con.prepareStatement("SELECT * FROM table WHERE id = ?"); pstmt.setInt(1, 10);
In this example, we are setting the value of the first parameter in the SQL query with the integer value of 10.
2. Setting multiple values to PreparedStatement
PreparedStatement pstmt = con.prepareStatement("UPDATE table SET name = ?, age = ?, gender = ? WHERE id = ?"); pstmt.setString(1, "John"); pstmt.setInt(2, 30); pstmt.setString(3, "Male"); pstmt.setInt(4, 101);
In this example, we are setting four parameters in the SQL query with their corresponding values.
Package Library:
The PreparedStatement setInt() method is a part of the Java JDBC API, which is included in the Java SE Development Kit (JDK) within the java.sql and javax.sql packages.
Java PreparedStatement.setInt - 30 examples found. These are the top rated real world Java examples of java.util.PreparedStatement.setInt extracted from open source projects. You can rate examples to help us improve the quality of examples.