3.5. Use JDBC PreparedStatement and CallableStatement - Source Code

Code:

package c3.s5;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

/**
 * @author Mikalai Zaikin
 */
public class PreparedUpdate {

    public static final String URL = "jdbc:derby://localhost/d:/workspace/ocpjp7-upgrade-samples/sample";

    private void doIt() {
        try (Connection connect = DriverManager.getConnection(URL);
             PreparedStatement statement = connect.prepareStatement("UPDATE PERSON SET NAME = ? WHERE ID = ?");) {

            // Not needed with JDBC 4.0 and later
            // Class.forName("org.apache.derby.jdbc.ClientDriver");

            statement.setString(1, "Mikola Zaikin");
            statement.setInt(2, 1);
            statement.executeUpdate();

            statement.setString(1, "Mikalai Zaikin");
            statement.executeUpdate();

        } catch (Exception e) {
            System.out.println(e);
        }
    }

    public static void main(String[] args) throws Exception {
        PreparedUpdate test = new PreparedUpdate();
        test.doIt();
    }
}
					

Professional hosting         Free 'Oracle Certified Expert Web Services Developer 6' Guide     Free SCDJWS 5.0 Guide