Running SQL statements with Statement objectsUse 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 interfaceUse 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 ] |