Running SQL statements with Statement objects

Use a Statement object to run an SQL statement and optionally obtain the ResultSet produced by it.

PreparedStatement inherits from Statement, and CallableStatement inherits from PreparedStatement. Use the following Statement objects to run different SQL statements:

  • Statement - to run a simple SQL statement that has no parameters.
  • PreparedStatement - to run a precompiled SQL statement that may or may not have IN parameters.
  • CallableStatement - to run a call to a database stored procedure. A CallableStatement may or may not have IN, OUT, and INOUT parameters.

Statement interface

Use Connection.createStatement() to create new Statement objects.

The following example shows how to use a Statement object.

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

                       // Create a Statement object.
     Statement s = c.createStatement();

                       // Run an SQL statement that creates
                       // a table in the database.
     s.executeUpdate("CREATE TABLE MYLIBRARY.MYTABLE (NAME VARCHAR(20), ID INTEGER)");

                       // Run an SQL statement that inserts
                       // a record into the table.
     s.executeUpdate("INSERT INTO MYLIBRARY.MYTABLE (NAME, ID) VALUES ('DAVE', 123)");

                       // Run an SQL statement that inserts
                       // a record into the table.
     s.executeUpdate("INSERT INTO MYLIBRARY.MYTABLE (NAME, ID) VALUES ('CINDY', 456)");

                       // Run an SQL query on the table.
     ResultSet rs = s.executeQuery("SELECT * FROM MYLIBRARY.MYTABLE");

                       // Close the Statement and the
                       // Connection.
     s.close();
     c.close();


[ Legal | AS/400 Glossary ]