Updating Data with PreparedStatement

From InterBase
Jump to: navigation, search

Go Up to Modifying Data Using SQL Statements


The following code fragment shows an example of how to use PreparedStatement if you want to execute the update more than once:

//Define a PreparedStatement object type
java.sql.PreparedStatement preparedStatement;
//Create the Prepared_Statement object
preparedStatement = connection.prepareStatement(
"UPDATE emp_table SET last_name = ? WHERE emp_no = ?");
//input the last name and employee number
String lname;
String empno;
System.in.readln("Enter last name: ", + lname);
System.in.readln("Enter employee number: ", + empno);
int empNumber = Integer.parseInt(empno);
//pass in the last name and employee id to preparedStatement's ? //parameters
//where '1' is the 1st parameter, '2' is the 2nd, etc.
preparedStatement.setString(1,lname);
preparedStatement.setInt(2,empNumber);
//now update the table
int rowCount = preparedStatement.executeUpdate();