View Javadoc
1 /*
2 * Created by IntelliJ IDEA.
3 * User: jbirchfield
4 * Date: Aug 19, 2002
5 * Time: 6:14:29 PM
6 * To change template for new class use
7 * Code Style | Class Templates options (Tools | IDE Options).
8 */
9 package net.plugin.sql.gui;
10
11 import net.plugin.sql.beans.DataSource;
12 import net.plugin.sql.util.JDBCConnectionManager;
13 import net.plugin.sql.util.DataSourceException;
14 import net.plugin.sql.gui.table.SQLInfoTable;
15 import net.plugin.sql.gui.table.SQLDataTable;
16
17 import javax.swing.*;
18 import java.awt.*;
19 import java.sql.*;
20
21 import sun.jdbc.rowset.CachedRowSet;
22
23 public class SQLDataTablePanel extends JPanel {
24
25 private static final String SQLHEADER = " select * from ";
26
27 private String tableName = null;
28
29 private JTabbedPane tabbedPane = null;
30 private JScrollPane dataScrollPane = null;
31 private JScrollPane infoScrollPane = null;
32
33 private SQLDataTable dataTable = null;
34 private SQLInfoTable infoTable = null;
35
36 private CachedRowSet rowSet = null;
37
38 private DataSource dataSource = null;
39
40 public SQLDataTablePanel(DataSource dataSource) {
41 setLayout(new BorderLayout());
42 this.dataSource = dataSource;
43 tabbedPane = new JTabbedPane();
44 dataTable = new SQLDataTable();
45 infoTable = new SQLInfoTable();
46 dataScrollPane = new JScrollPane(dataTable);
47 infoScrollPane = new JScrollPane(infoTable);
48 tabbedPane.addTab("Data", dataScrollPane);
49 tabbedPane.addTab("Columns", infoScrollPane);
50 add(tabbedPane);
51 }
52
53 public void setTableName(String tableName) {
54 this.tableName = tableName;
55 updateTable();
56 }
57
58 public void setRowSet(CachedRowSet rowSet) {
59 this.rowSet = rowSet;
60 dataTable.setRowSet(rowSet);
61 infoTable.setRowSet(rowSet);
62 }
63
64
65 private void updateTable() {
66 System.out.println("SQLDataTable.updateTable");
67 Connection con = null;
68 Statement stmt = null;
69 ResultSet rs = null;
70
71 try {
72 JDBCConnectionManager manager = new JDBCConnectionManager(dataSource);
73 con = manager.getConnection();
74 stmt = con.createStatement();
75 rs = stmt.executeQuery(SQLHEADER + tableName);
76 rowSet = new CachedRowSet();
77 rowSet.populate(rs);
78 dataTable.setRowSet(rowSet);
79 infoTable.setRowSet(rowSet);
80 } catch (DataSourceException e) {
81 JOptionPane.showMessageDialog(null, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
82 } catch (SQLException e) {
83 JOptionPane.showMessageDialog(null, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
84 } finally {
85 try {
86 if (rs != null) {
87 rs.close();
88 }
89 if (stmt != null) {
90 stmt.close();
91 }
92 if (con != null) {
93 con.close();
94 }
95 } catch (SQLException e) {
96 e.printStackTrace();
97 }
98 }
99 }
100 }
This page was automatically generated by Maven