The java.util PreparedStatement setDate method is used to set a date value in a prepared statement. This method takes two parameters, an integer parameter that specifies the parameter index and a java.sql.Date object that represents the date value.
Example 1:
PreparedStatement ps = conn.prepareStatement("INSERT INTO employee(empID, empName, empDOB) VALUES(?,?,?)"); ps.setInt(1, 101); ps.setString(2, "John"); ps.setDate(3, new java.sql.Date(new Date().getTime()));
In this example, we are inserting a new employee record into the employee table. The prepared statement has three parameters - empID, empName and empDOB. We are setting the empID and empName parameters using the setInt and setString methods respectively. The empDOB parameter is set using the setDate method, where we are creating a new java.sql.Date object and passing it as the second parameter.
Example 2:
java.sql.Date dob = new java.sql.Date(90, 5, 15); PreparedStatement ps = conn.prepareStatement("SELECT * FROM employee WHERE empDOB < ?"); ps.setDate(1, dob);
In this example, we are retrieving all employee records whose date of birth (empDOB) is before June 15, 1990. The prepared statement has one parameter - empDOB. We are setting this parameter using the setDate method, where we are creating a new java.sql.Date object with the year, month and day values passed as parameters.
The java.util PreparedStatement setDate method belongs to the java.sql package.
Java PreparedStatement.setDate - 16 examples found. These are the top rated real world Java examples of java.util.PreparedStatement.setDate extracted from open source projects. You can rate examples to help us improve the quality of examples.