KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > db > explorer > DatabaseConnection


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.api.db.explorer;
21
22 import java.sql.Connection JavaDoc;
23 import org.netbeans.modules.db.explorer.ConnectionList;
24 import org.netbeans.modules.db.explorer.DatabaseConnectionAccessor;
25
26 /**
27  * Encapsulates a database connection. Each DatabaseConnection instance
28  * represents a connection to a database in the Database Explorer.
29  *
30  * <p>This class provides access to the properties of a database connection,
31  * such as the connection name, database URL, user or default schema. New connections
32  * can be created using the {@link #create} method (these connections can be
33  * added to the Database Explorer using the
34  * {@link ConnectionManager#addConnection} method.</p>
35  *
36  * <p>It is also possible to retrieve the JDBC {@link java.sql.Connection}
37  * using the {@link #getJDBCConnection} method (the connection can be connected
38  * or disconnected using the {@link ConnectionManager#showConnectionDialog}
39  * and {@link ConnectionManager#disconnect} methods.</p>
40  *
41  * @author Andrei Badea
42  *
43  * @see ConnectionManager
44  */

45 public final class DatabaseConnection {
46     
47     private org.netbeans.modules.db.explorer.DatabaseConnection delegate;
48
49     /*
50      * DatabaseConnection's methods delegate to
51      * org.netbeans.modules.db.explorer.DatabaseConnection. Each instance of
52      * org.netbeans.modules.db.explorer.DatabaseConnection
53      * creates and maintains an instance of
54      * DatabaseConnection. Since the constructor of
55      * DatabaseConnection is package-protected, an accessor is needed
56      * to create instances of DatabaseConnection from
57      * org.netbeans.modules.db.explorer.DatabaseConnection.
58      *
59      * See org.netbeans.modules.db.explorer.DatabaseConnectionAccessor
60      */

61     
62     static {
63         DatabaseConnectionAccessor.DEFAULT = new DatabaseConnectionAccessor() {
64             public DatabaseConnection createDatabaseConnection(org.netbeans.modules.db.explorer.DatabaseConnection conn) {
65                 return new DatabaseConnection(conn);
66             }
67         };
68     }
69     
70     /**
71      * Package-protected constructor.
72      */

73     DatabaseConnection(org.netbeans.modules.db.explorer.DatabaseConnection delegate) {
74         assert delegate != null;
75         this.delegate = delegate;
76     }
77
78     /**
79      * Returns the org.netbeans.modules.db.explorer.DatabaseConnection which this instance delegates to.
80      */

81     org.netbeans.modules.db.explorer.DatabaseConnection getDelegate() {
82         return delegate;
83     }
84     
85     /**
86      * Creates a new DatabaseConnection instance.
87      *
88      * @param driver the JDBC driver the new connection uses; cannot be null.
89      * @param databaseURL the URL of the database to connect to; cannot be null.
90      * @param user the username.
91      * @param password the password.
92      * @param rememberPassword whether to remeber the password for the current session.
93      *
94      * @return the new instance.
95      *
96      * @throws NullPointerException if driver or database are null.
97      */

98     public static DatabaseConnection create(JDBCDriver driver, String JavaDoc databaseURL,
99             String JavaDoc user, String JavaDoc schema, String JavaDoc password, boolean rememberPassword) {
100         if (driver == null || databaseURL == null) {
101             throw new NullPointerException JavaDoc();
102         }
103         org.netbeans.modules.db.explorer.DatabaseConnection conn = new org.netbeans.modules.db.explorer.DatabaseConnection();
104         conn.setDriverName(driver.getName());
105         conn.setDriver(driver.getClassName());
106         conn.setDatabase(databaseURL);
107         conn.setUser(user);
108         conn.setSchema(schema);
109         conn.setPassword(password);
110         conn.setRememberPassword(rememberPassword);
111         
112         return conn.getDatabaseConnection();
113     }
114     
115     /**
116      * Returns the JDBC driver class that this connection uses.
117      *
118      * @return the JDBC driver class
119      */

120     public String JavaDoc getDriverClass() {
121         return delegate.getDriver();
122     }
123
124     /**
125      * Returns this connection's database URL.
126      *
127      * @return the connection's database URL
128      */

129     public String JavaDoc getDatabaseURL() {
130         return delegate.getDatabase();
131     }
132     
133     /**
134      * Returns this connection's default schema.
135      *
136      * @return the schema
137      */

138     public String JavaDoc getSchema() {
139         return delegate.getSchema();
140     }
141     
142     /**
143      * Returns the user name used to connect to the database.
144      *
145      * @return the user name
146      */

147     public String JavaDoc getUser() {
148         return delegate.getUser();
149     }
150     
151     /**
152      * Returns the password used to connect to the database.
153      *
154      * @return the password
155      */

156     public String JavaDoc getPassword() {
157         return delegate.getPassword();
158     }
159     
160     /**
161      * Returns the programmatic name of this connection in the Database Explorer.
162      *
163      * @return the programmatic name
164      */

165     public String JavaDoc getName() {
166         return delegate.getName();
167     }
168     
169     /**
170      * Returns the name used to display this connection in the UI.
171      *
172      * @return the display name
173      */

174     public String JavaDoc getDisplayName() {
175         return delegate.getName();
176     }
177     
178     /**
179      * Returns the {@link java.sql.Connection} instance which encapsulates
180      * the physical connection to the database if this database connection
181      * is connected. Note that "connected" here means "connected using the
182      * Database Explorer". There is no check if {@link java.sql.Connection#close}
183      * has been called on the returned connection. However,
184      * clients should not call <code>Connection.close()</code> on the returned
185      * connection, therefore this method should always return a non-closed
186      * connection or <code>null</code>.
187      *
188      * <p><strong>Calling {@link java.sql.Connection#close} on the connection
189      * returned by this method is illegal. Use
190      * {@link ConnectionManager#disconnect}
191      * to close the connection.</strong></p>
192      *
193      * @return the physical connection or null if not connected.
194      *
195      * @throws IllegalStateException if this connection is not added to the
196      * ConnectionManager.
197      */

198     public Connection JavaDoc getJDBCConnection() {
199         if (!ConnectionList.getDefault().contains(delegate)) {
200             throw new IllegalStateException JavaDoc("This connection is not added to the ConnectionManager."); // NOI18N
201
}
202         return delegate.getJDBCConnection();
203     }
204
205     /**
206      * Returns a string representation of the database connection.
207      */

208     public String JavaDoc toString() {
209         return "DatabaseConnection[name='" + getName() + "']"; // NOI18N
210
}
211 }
212
Popular Tags