import java.awt.*; import java.awt.event.*; import java.util.*; import java.sql.*; public class DynamicSQL extends Frame { Connection connection; PreparedStatement prepare; TextField endereco; TextArea text; public DynamicSQL () { super("DynamicSQL"); try { String driverName = "sun.jdbc.odbc.JdbcOdbcDriver"; Driver driver = (Driver)Class.forName(driverName).newInstance(); Properties p = new Properties(); p.put("user", "p8cp1"); p.put("password", "ismael"); String dbURL = "jdbc:odbc:fiaa"; connection = driver.connect(dbURL, p); System.out.println("--- Conection established ---"); // Create the SQL statement that we will use String sql = "select Nome from Pessoa where Endereco = ?"; prepare = connection.prepareCall(sql); } catch (SQLException e) {} catch (Exception e) {} // Add the text components endereco = new TextField(25); text = new TextArea(20,60); this.add("North", endereco); this.add("Center", text); // Resize window and display this.resize(300, 300); this.setVisible( true ); //Add ActionListener para Fechar Aplicacao this.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } } ); } public boolean handleEvent (Event evt) { // If the textfield caused the event, display items if (evt.target == endereco) { try { // Clear the parameters for the current statement prepare.clearParameters(); // Specify the endereco parameter prepare.setString(1, endereco.getText()); // Execute the statement and get the result set ResultSet results = prepare.executeQuery(); String stext = ""; // Loop through the returned items and display while (results.next()) { stext += results.getString(1) + "\n"; } // Set the text displayed for the textarea component text.setText(stext); } catch (SQLException e) {} } return super.handleEvent(evt); } public static void main (String args[]) { DynamicSQL app = new DynamicSQL(); } }