001: /*
002: ** $Id: ConnectionModel.java,v 1.10 2000/11/26 13:11:21 mrw Exp $
003: **
004: ** Mike Wilson, July 2000, mrw@whisperingwind.co.uk
005: **
006: ** (C) Copyright 2000, Mike Wilson, Reading, Berkshire, UK
007: **
008: ** This program is free software; you can redistribute it and/or modify
009: ** it under the terms of the GNU General Public License as published by
010: ** the Free Software Foundation; either version 2 of the License, or
011: ** (at your option) any later version.
012: **
013: ** This program is distributed in the hope that it will be useful,
014: ** but WITHOUT ANY WARRANTY; without even the implied warranty of
015: ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: ** GNU General Public License for more details.
017: **
018: ** You should have received a copy of the GNU Library General
019: ** Public License along with this library; if not, write to the
020: ** Free Software Foundation, Inc., 59 Temple Place - Suite 330,
021: ** Boston, MA 02111-1307 USA.
022: */
023:
024: package uk.co.whisperingwind.vienna;
025:
026: import java.sql.Connection;
027: import java.sql.SQLException;
028: import java.sql.DriverManager;
029: import uk.co.whisperingwind.framework.Dialogs;
030: import uk.co.whisperingwind.framework.Model;
031: import uk.co.whisperingwind.framework.SwingThread;
032:
033: /**
034: ** Model for the JDBC connection details. Has fields for the user
035: ** name, password, JDBC driver class and JDBC URL.
036: */
037:
038: class ConnectionModel extends Model {
039: private Connection connection = null;
040: private String connectionName = null;
041: private String userName = "";
042: private String password = "";
043: private String driverClass = null;
044: private String url = null;
045: private boolean updated = false;
046:
047: public ConnectionModel() {
048: super ();
049: }
050:
051: public ConnectionModel(final ConnectionModel copy) {
052: super ();
053: connectionName = new String(copy.connectionName);
054:
055: if (copy.userName != null)
056: userName = new String(copy.userName);
057:
058: if (copy.password != null)
059: password = new String(copy.password);
060:
061: driverClass = new String(copy.driverClass);
062: url = new String(copy.url);
063: }
064:
065: public void setName(String newName) {
066: connectionName = newName;
067: fireEvent("connectionName", connectionName);
068: }
069:
070: public String getName() {
071: return connectionName;
072: }
073:
074: public void setUserName(String newName) {
075: userName = newName;
076: fireEvent("userName", userName);
077: }
078:
079: public String getUserName() {
080: return userName;
081: }
082:
083: public void setPassword(String newPassword) {
084: password = newPassword;
085: fireEvent("password", password);
086: }
087:
088: public String getPassword() {
089: if (password == null)
090: return "";
091: else
092: return password;
093: }
094:
095: public void setDriverClass(String newClass) {
096: driverClass = newClass;
097: fireEvent("driverClass", driverClass);
098: }
099:
100: public String getDriverClass() {
101: return driverClass;
102: }
103:
104: public void setUrl(String newUrl) {
105: url = newUrl;
106: fireEvent("url", url);
107: }
108:
109: public String getUrl() {
110: return url;
111: }
112:
113: /**
114: ** Connect to the database URL. This is run in a separate thread
115: ** so the GUI is alive while I am connecting (which can take several
116: ** seconds).
117: */
118:
119: public void connect() {
120: ConnectWorker connectWorker = new ConnectWorker();
121: connectWorker.start();
122: }
123:
124: /*
125: ** Disconnect from the database.
126: */
127:
128: public void disconnect() {
129: if (isConnected()) {
130: connection = null;
131: setUserName(null);
132: setPassword(null);
133: setDriverClass(null);
134: setUrl(null);
135: updated = false;
136:
137: fireEvent("disconnect", null);
138: }
139: }
140:
141: public void commit() {
142: try {
143: connection.commit();
144: updated = false;
145: } catch (SQLException ex) {
146: Dialogs.showError("Commit failed", ex.getMessage());
147: }
148: }
149:
150: public void rollback() {
151: try {
152: connection.rollback();
153: updated = false;
154: } catch (SQLException ex) {
155: Dialogs.showError("Rollback failed", ex.getMessage());
156: }
157: }
158:
159: public void setUpdated() {
160: updated = true;
161: }
162:
163: public boolean isUpdated() {
164: return updated;
165: }
166:
167: /*
168: ** Returns true if I am connected to the database.
169: */
170:
171: public boolean isConnected() {
172: return connection != null;
173: }
174:
175: /*
176: ** Returns a Connection for use in queries. Will return null if I
177: ** am not connected.
178: */
179:
180: public Connection getConnection() {
181: return connection;
182: }
183:
184: /**
185: ** Worker class to make the connection with the database server.
186: ** This can run in a separate thread so the GUI remains responsive
187: ** while the connection is made.
188: */
189:
190: private class ConnectWorker extends SwingThread {
191: /**
192: ** Make the connection...
193: */
194:
195: public void construct() {
196: if (driverClass != null) {
197: try {
198: Class.forName(driverClass);
199: connection = DriverManager.getConnection(url,
200: userName, password);
201: connection.setAutoCommit(false);
202: } catch (SQLException ex) {
203: Dialogs.showError("Connect error", ex.getMessage());
204: } catch (java.lang.ClassNotFoundException ex) {
205: Dialogs.showError("Driver error", ex.getMessage());
206: }
207: }
208: }
209:
210: /**
211: ** This always runs in the event thread.
212: */
213:
214: public void finished() {
215: fireEvent("connect", null);
216: }
217: }
218: }
|