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 32 public class DBQueryTest extends JFrame { 33 private Connection connection; 34 private Statement statement; 35 private ResultSet resultSet; 36 private ResultSetMetaData rsMetaData; 37 38 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; 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 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 ResultSetMetaData rsmd = rs.getMetaData(); 106 107 for (int i=1;i<=rsmd.getColumnCount();++i) { 108 columnHeads.addElement(rsmd.getColumnName(i) ); 109 } 110 do 112 { 113 rows.addElement(getNextRow(rs, rsmd)); 114 } while (rs.next() ); 115 116 table = new JTable(rows, columnHeads); 118 JScrollPane scroller = new JScrollPane(table); 119 Container c = getContentPane(); 120 c.remove(1 ); 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 (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 |