KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > piratepete > util > db > DBQueryTest


1 package com.piratepete.util.db;
2
3 import java.sql.*;
4 import javax.swing.*;
5 import java.awt.*;
6 import java.awt.event.*;
7 import java.util.*;
8
9 /**
10  * DBQueryTest Class
11  * Copyright (C) 2003 David L. Whitehurst<p>
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.<p>
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.<p>
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.<p>
26  *
27  * <a HREF="http://www.piratepetesoftware.com">piratepetesoftware.com</a><br>
28  * <a HREF="mailto:dlwhitehurst@comcast.net">dlwhitehurst@comcast.net</a>
29  *
30  * @version 1.0a
31  */

32 public class DBQueryTest extends JFrame {
33     private Connection connection;
34     private Statement statement;
35     private ResultSet resultSet;
36     private ResultSetMetaData rsMetaData;
37     
38     //gui stuff
39
private JTable table;
40     private JTextArea inputQuery;
41     private JButton submitQuery;
42     
43     public DBQueryTest(Connection conn) {
44         super ("Enter query. Click submit to test");
45         
46         connection = conn; // set connection used
47

48         inputQuery = new JTextArea();
49         submitQuery = new JButton ("Submit Query");
50         submitQuery.addActionListener (
51                 new ActionListener() {
52                     public void actionPerformed (ActionEvent e) {
53                         if (e.getSource() == submitQuery )
54                             getTable();
55                     }
56                 }
57         );
58         
59         JPanel topPanel = new JPanel();
60         topPanel.setLayout (new BorderLayout() );
61         topPanel.add (new JScrollPane (inputQuery), BorderLayout.CENTER );
62         topPanel.add ( submitQuery, BorderLayout.SOUTH );
63         
64         table = new JTable();
65         
66         Container c = getContentPane();
67         c.setLayout( new BorderLayout() );
68         c.add (topPanel, BorderLayout.NORTH );
69         c.add (table, BorderLayout.CENTER );
70         
71         getTable();
72         
73         setSize (1000, 500);
74         show();
75     }
76
77     private void getTable() {
78         try {
79             String JavaDoc query = inputQuery.getText();
80             
81             statement = connection.createStatement();
82             resultSet = statement.executeQuery( query );
83             displayResultSet(resultSet);
84             
85         }
86         catch (SQLException sqlex) {
87             sqlex.printStackTrace();
88         }
89     }
90     
91     private void displayResultSet( ResultSet rs) throws SQLException {
92         boolean moreRecords = rs.next();
93         
94         if (!moreRecords) {
95             JOptionPane.showMessageDialog(this, "ResultSet contained no records");
96             setTitle("No records to Display");
97             return;
98         }
99         
100         Vector columnHeads = new Vector();
101         Vector rows = new Vector();
102         
103         try {
104             // get columnheads
105
ResultSetMetaData rsmd = rs.getMetaData();
106             
107             for (int i=1;i<=rsmd.getColumnCount();++i) {
108                 columnHeads.addElement(rsmd.getColumnName(i) );
109             }
110             // get row data
111
do
112             {
113                 rows.addElement(getNextRow(rs, rsmd));
114             } while (rs.next() );
115                 
116             // display table with contents
117
table = new JTable(rows, columnHeads);
118             JScrollPane scroller = new JScrollPane(table);
119             Container c = getContentPane();
120             c.remove(1 ); // delete original scrollpane and table
121
c.add(scroller, BorderLayout.CENTER);
122             c.validate();
123         } catch (SQLException sqlex ) {
124                 sqlex.printStackTrace();
125         }
126     }
127     
128     private Vector getNextRow (ResultSet rs, ResultSetMetaData rsmd) throws SQLException {
129         Vector currentRow = new Vector();
130         
131         for (int i=1; i<=rsmd.getColumnCount(); ++i) {
132             switch (rsmd.getColumnType(i)) {
133                 case Types.VARCHAR:
134                 case Types.LONGVARCHAR:
135                     currentRow.addElement(rs.getString(i));
136                     break;
137                 case Types.INTEGER:
138                     currentRow.addElement(new Long JavaDoc(rs.getLong(i)));
139                     break;
140                 default:
141                     System.out.println ("Type was: " + rsmd.getColumnTypeName(i));
142                     break;
143             }
144             
145         }
146         return currentRow;
147     }
148     
149     
150 }
151
Popular Tags