public class DisplayQueryResults extends JFrame
1: // Fig. 25.31: DisplayQueryResults.java
2: // Display the contents of the Authors table in the
3: // Books database.
4: import java.awt.BorderLayout;
5: import java.awt.event.ActionListener;
6: import java.awt.event.ActionEvent;
7: import java.awt.event.WindowAdapter;
8: import java.awt.event.WindowEvent;
9: import java.sql.SQLException;
10: import javax.swing.JFrame;
11: import javax.swing.JTextArea;
12: import javax.swing.JScrollPane;
13: import javax.swing.ScrollPaneConstants;
14: import javax.swing.JTable;
15: import javax.swing.JOptionPane;
16: import javax.swing.JButton;
17: import javax.swing.Box;
18:
19: public class DisplayQueryResults extends JFrame
20: {
21: // JDBC driver, database URL, username and password
22: static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
23: static final String DATABASE_URL = "jdbc:mysql://localhost/books";
24: static final String USERNAME= "jhtp6";
25: static final String PASSWORD= "jhtp6";
26:
27: // default query retrieves all data from authors table
28: static final String DEFAULT_QUERY = "SELECT * FROM authors";
29:
30: private ResultSetTableModel tableModel;
31: private JTextArea queryArea;
32:
33: // create ResultSetTableModel and GUI
34: public DisplayQueryResults()
35: {
36: super( "Displaying Query Results" );
37:
38: // create ResultSetTableModel and display database table
39: try
40: {
41: // create TableModel for results of query SELECT * FROM authors
42: tableModel = new ResultSetTableModel( JDBC_DRIVER, DATABASE_URL,
43: USERNAME, PASSWORD, DEFAULT_QUERY );
44:
45: // set up JTextArea in which user types queries
46: queryArea = new JTextArea( DEFAULT_QUERY, 3, 100 );
47: queryArea.setWrapStyleWord( true );
48: queryArea.setLineWrap( true );
49:
50: JScrollPane scrollPane = new JScrollPane( queryArea,
51: ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
52: ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER );
53:
54: // set up JButton for submitting queries
55: JButton submitButton = new JButton( "Submit Query" );
56:
57: // create Box to manage placement of queryArea and
58: // submitButton in GUI
59: Box box = Box.createHorizontalBox();
60: box.add( scrollPane );
61: box.add( submitButton );
62:
63: // create JTable delegate for tableModel
64: JTable resultTable = new JTable( tableModel );
65:
66: // place GUI components on content pane
67: add( box, BorderLayout.NORTH );
68: add( new JScrollPane( resultTable ), BorderLayout.CENTER );
69:
70: // create event listener for submitButton
71: submitButton.addActionListener(
72:
73: new ActionListener()
74: {
75: // pass query to table model
76: public void actionPerformed( ActionEvent event )
77: {
78: // perform a new query
79: try
80: {
81: tableModel.setQuery( queryArea.getText() );
82: } // end try
83: catch ( SQLException sqlException )
84: {
85: JOptionPane.showMessageDialog( null,
86: sqlException.getMessage(), "Database error",
87: JOptionPane.ERROR_MESSAGE );
88:
89: // try to recover from invalid user query
90: // by executing default query
91: try
92: {
93: tableModel.setQuery( DEFAULT_QUERY );
94: queryArea.setText( DEFAULT_QUERY );
95: } // end try
96: catch ( SQLException sqlException2 )
97: {
98: JOptionPane.showMessageDialog( null,
99: sqlException2.getMessage(), "Database error",
100: JOptionPane.ERROR_MESSAGE );
101:
102: // ensure database connection is closed
103: tableModel.disconnectFromDatabase();
104:
105: System.exit( 1 ); // terminate application
106: } // end inner catch
107: } // end outer catch
108: } // end actionPerformed
109: } // end ActionListener inner class
110: ); // end call to addActionListener
111:
112: setSize( 500, 250 ); // set window size
113: setVisible( true ); // display window
114: } // end try
115: catch ( ClassNotFoundException classNotFound )
116: {
117: JOptionPane.showMessageDialog( null,
118: "MySQL driver not found", "Driver not found",
119: JOptionPane.ERROR_MESSAGE );
120:
121: System.exit( 1 ); // terminate application
122: } // end catch
123: catch ( SQLException sqlException )
124: {
125: JOptionPane.showMessageDialog( null, sqlException.getMessage(),
126: "Database error", JOptionPane.ERROR_MESSAGE );
127:
128: // ensure database connection is closed
129: tableModel.disconnectFromDatabase();
130:
131: System.exit( 1 ); // terminate application
132: } // end catch
133:
134: // dispose of window when user quits application (this overrides
135: // the default of HIDE_ON_CLOSE)
136: setDefaultCloseOperation( DISPOSE_ON_CLOSE );
137:
138: // ensure database connection is closed when user quits application
139: addWindowListener(
140:
141: new WindowAdapter()
142: {
143: // disconnect from database and exit when window has closed
144: public void windowClosed( WindowEvent event )
145: {
146: tableModel.disconnectFromDatabase();
147: System.exit( 0 );
148: } // end method windowClosed
149: } // end WindowAdapter inner class
150: ); // end call to addWindowListener
151: } // end DisplayQueryResults constructor
152:
153: // execute application
154: public static void main( String args[] )
155: {
156: new DisplayQueryResults();
157: } // end main
158: } // end class DisplayQueryResults
159:
160:
161:
162: /**************************************************************************
163: * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
164: * Pearson Education, Inc. All Rights Reserved. *
165: * *
166: * DISCLAIMER: The authors and publisher of this book have used their *
167: * best efforts in preparing the book. These efforts include the *
168: * development, research, and testing of the theories and programs *
169: * to determine their effectiveness. The authors and publisher make *
170: * no warranty of any kind, expressed or implied, with regard to these *
171: * programs or to the documentation contained in these books. The authors *
172: * and publisher shall not be liable in any event for incidental or *
173: * consequential damages in connection with, or arising out of, the *
174: * furnishing, performance, or use of these programs. *
175: *************************************************************************/