001: /*
002: * Copyright Javelin Software, All rights reserved.
003: */
004:
005: package com.javelin.swinglets.table;
006:
007: import java.util.*;
008: import java.sql.*;
009:
010: import javax.swing.table.*;
011:
012: /**
013: * A SQLTableModel
014: *
015: * @author Dino Fancellu, Robin Sharp
016: */
017:
018: public class SQLTableModel extends DefaultTableModel {
019:
020: public SQLTableModel() {
021: dataVector = new Vector();
022: columnIdentifiers = new Vector();
023: }
024:
025: /**
026: * Set the URL.
027: */
028: public void setURL(String url) {
029: this .url = url;
030: }
031:
032: /**
033: * Get the Source.
034: */
035: public String getURL() {
036: return url;
037: }
038:
039: /**
040: * Set the User.
041: */
042: public void setUser(String user) {
043: this .user = user;
044: }
045:
046: /**
047: * Get the Source.
048: */
049: public String getUser() {
050: return user;
051: }
052:
053: /**
054: * Set the Password.
055: */
056: public void setPassword(String password) {
057: this .password = password;
058: }
059:
060: /**
061: * Get the Source.
062: */
063: public String getPassword() {
064: return password;
065: }
066:
067: /**
068: * Set the SQL String to execute.
069: */
070: public void setSql(String sql) {
071: this .sql = sql;
072: }
073:
074: /**
075: * Get the SQL String to execute.
076: */
077: public String getSql() {
078: return sql;
079: }
080:
081: public void execute() {
082: dataVector.removeAllElements();
083: columnIdentifiers.removeAllElements();
084:
085: Connection connection = null;
086: Statement statement = null;
087: try {
088: connection = DriverManager.getConnection(url,
089: user == null ? "" : user, password == null ? ""
090: : password);
091:
092: statement = connection.createStatement();
093: ResultSet resultSet = statement.executeQuery(sql);
094: final ResultSetMetaData md = resultSet.getMetaData();
095:
096: final int columnCount = md.getColumnCount();
097: String columnName = md.getColumnName(1);
098:
099: for (int columnIndex = 0; columnIndex < columnCount; columnIndex++) {
100: try {
101: columnIdentifiers.addElement(md
102: .getColumnLabel(columnIndex + 1));
103: } catch (SQLException ex) {
104: columnIdentifiers.addElement("" + columnIndex + 1);
105: }
106: }
107:
108: Object blankobject = " ";
109: while (resultSet.next()) {
110: Vector row = new Vector();
111: for (int columnIndex = 0; columnIndex < columnCount; columnIndex++) {
112: Object o = resultSet.getObject(columnIndex + 1);
113: if (o == null) {
114: o = blankobject;
115: }
116: //System.out.println( o );
117: row.addElement(o);
118: }
119: dataVector.addElement(row);
120: }
121: } catch (Exception e) {
122: System.out.println(e);
123: } finally {
124: if (statement != null) {
125: try {
126: statement.close();
127: } catch (Exception e) {
128: System.out.println(e);
129: }
130: }
131: if (connection != null) {
132: try {
133: connection.close();
134: } catch (Exception e) {
135: System.out.println(e);
136: }
137: }
138: fireTableDataChanged();
139: }
140:
141: }
142:
143: protected String url;
144: protected String user;
145: protected String password;
146: protected String sql;
147:
148: static {
149: System.out.println("Preloading database drivers.");
150: try {
151: Class.forName("com.ms.jdbc.odbc.JdbcOdbcDriver");
152: } catch (Exception e) {
153: try {
154: Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
155: } catch (Exception e2) {
156: System.out.println("No available ODBC driver.");
157: }
158: }
159: }
160:
161: }
|