The SQLStatementDocument class is an implementation of the Java™ Foundation Classes (JFC) Document interface. It can be used directly with any JFC graphical text component. Several text components, such as single line fields (JTextField) and multiple line text areas (JTextArea), are available in JFC. SQLStatementDocument objects associate the contents of text components with SQLConnection objects. The Java program can run the SQL statement contained in the document contents at any time and then process the results, if any.
To use an SQLStatementDocument, you must set the connection property. Set this property by using the constructor or the setConnection() method. The SQLStatementDocument object is then "plugged" into the text component, typically using the text component's constructor or setDocument() method. Use execute() at any time to run the SQL statement contained in the document.
The following example creates an SQLStatementDocument in a JTextField:
// Create an SQLStatementDocument // object. Assume that "connection" // is an SQLConnection object that is // created and initialized elsewhere. // The text of the document is // initialized to a generic query. SQLStatementDocument document = new SQLStatementDocument (connection, "SELECT * FROM QIWS.QCUSTCDT"); // Create a text field to present the // document. JTextField textField = new JTextField (); textField.setDocument (document); // Add the text field to a frame. // Assume that "frame" is a JFrame // created elsewhere. frame.getContentPane ().add (textField); // Run the SQL statement that is in // the text field. document.execute ();
After the SQL statement is issued, use getResultSet(), getMoreResults(), getUpdateCount(), or getWarnings() to retrieve the results.