PreparedStatement interface

You can use a PreparedStatement object when an SQL statement is going to be run many times. An SQL statement can be precompiled. A "prepared." statement is an SQL statement that has been precompiled. This approach is more efficient than running the same statement multiple times using a Statement object, which compiles the statement each time it is run. In addition, the SQL statement contained in a PreparedStatement object may have one or more IN parameters. Use Connection.prepareStatement() to create PreparedStatement objects.

Start changeYou can use a batch update facility to associate a single PreparedStatement object with multiple sets of input parameter values. This unit then can be sent to the database for processing as a single entity. You may get better performance with batch updates because it is usually faster to process a group of update operations than one update operation at a time. If you want to use the batch update facility, you need JDBC 2.0 and JDK 1.2.End change

The following example shows how to use the PreparedStatement interface.

                       // Connect to the AS/400.
     Connection c = DriverManager.getConnection("jdbc:as400://mySystem");

                       // Create the PreparedStatement
                       // object. It precompiles the
                       // specified SQL statement. The
                       // question marks indicate where
                       // parameters must be set before the
                       // statement is run.
     PreparedStatement ps = c.prepareStatement("INSERT INTO MYLIBRARY.MYTABLE (NAME, ID) VALUES (?, ?)");

                       // Set parameters and run the
                       // statement.
     ps.setString(1, "JOSH");
     ps.setInt(2, 789);
     ps.executeUpdate();

                       // Set parameters and run the
                       // statement again.
     ps.setString(1, "DAVE");
     ps.setInt(2, 456);
     ps.executeUpdate();

                       // Close PreparedStatement and the
                       // Connection.
     ps.close();
     c.close();


[ Legal | AS/400 Glossary ]