public final class ConnectionManager extends Object
The list of connections can be retrieved using the ConnectionManager.getConnections()
method. A connection can be also retrieved by name using the
ConnectionManager.getConnection(java.lang.String)
method.
New connections can be added to the Connection Manager using the
ConnectionManager.addConnection(org.netbeans.api.db.explorer.DatabaseConnection)
method (new connections can be created using the
DatabaseConnection.create(org.netbeans.api.db.explorer.JDBCDriver, java.lang.String, java.lang.String, java.lang.String, java.lang.String, boolean)
method.
It is also possible to display the New Database Connection dialog to let the
user create a new database connection using the ConnectionManager.showAddConnectionDialog(org.netbeans.api.db.explorer.JDBCDriver)
.
Connections can be realized using the ConnectionManager.showConnectionDialog(org.netbeans.api.db.explorer.DatabaseConnection)
method.
Clients can be informed of changes to the ConnectionManager by registering
a ConnectionListener
using the ConnectionManager.addConnectionListener(org.netbeans.api.db.explorer.ConnectionListener)
method.
DatabaseConnection
Constructor and Description |
---|
ConnectionManager() |
Modifier and Type | Method and Description |
---|---|
void |
addConnection(DatabaseConnection dbconn)
Adds a new connection to Database Explorer.
|
void |
addConnectionListener(ConnectionListener listener)
Registers a ConnectionListener.
|
boolean |
connect(DatabaseConnection dbconn)
Connects this connection to the database without opening any
dialog.
|
void |
disconnect(DatabaseConnection dbconn)
Disconnects this connection from the database.
|
DatabaseConnection |
getConnection(String name)
Returns the connection with the specified name.
|
DatabaseConnection[] |
getConnections()
Returns the list of connections in the Database Explorer.
|
static ConnectionManager |
getDefault()
Gets the ConnectionManager singleton instance.
|
DatabaseConnection |
getPreferredConnection(boolean fallback)
Returns the preferred connection instance.
|
void |
refreshConnectionInExplorer(DatabaseConnection dbconn)
Refresh the node corresponding to the specified connection in the
Runtime tab.
|
void |
removeConnection(DatabaseConnection dbconn)
Remove an existing connection from the Database Explorer.
|
void |
removeConnectionListener(ConnectionListener listener)
Unregisters the specified connection listener.
|
void |
selectConnectionInExplorer(DatabaseConnection dbconn)
Selects the node corresponding to the specified connection in the
Runtime tab.
|
void |
setPreferredConnection(DatabaseConnection conn)
Sets the preferred DB connection for the IDE.
|
void |
showAddConnectionDialog(JDBCDriver driver)
Shows the dialog for adding a new connection.
|
void |
showAddConnectionDialog(JDBCDriver driver,
String databaseUrl)
Shows the dialog for adding a new connection with the specified database URL.
|
void |
showAddConnectionDialog(JDBCDriver driver,
String databaseUrl,
String user,
String password)
Shows the dialog for adding a new connection with the specified database URL, user and password
The specified driver be filled as the single element of the
Driver combo box of the New Database Connection dialog box.
|
DatabaseConnection |
showAddConnectionDialogFromEventThread(JDBCDriver driver)
The counterpart of
ConnectionManager.showAddConnectionDialog(JDBCDriver) which returns
the newly created database connection, but must be called from the event dispatching
thread. |
DatabaseConnection |
showAddConnectionDialogFromEventThread(JDBCDriver driver,
String databaseUrl)
The counterpart of
ConnectionManager.showAddConnectionDialog(JDBCDriver, String) which returns
the newly created database connection, but must be called from the event dispatching
thread. |
DatabaseConnection |
showAddConnectionDialogFromEventThread(JDBCDriver driver,
String databaseUrl,
String user,
String password)
The counterpart of
ConnectionManager.showAddConnectionDialog(JDBCDriver, String, String, String)
which returns the newly created database connection, but must be called
from the event dispatching thread. |
void |
showConnectionDialog(DatabaseConnection dbconn)
Shows the Connect dialog for the specified connection if not all data
needed to connect, such as the user name or password,
are known), or displays a modal progress dialog and attempts
to connect to the database immediately.
|
public static ConnectionManager getDefault()
public DatabaseConnection[] getConnections()
public DatabaseConnection getConnection(String name)
name
- the connection nameNullPointerException
- if the specified database name is null.public void addConnection(DatabaseConnection dbconn) throws DatabaseException
dbconn
- the connection to be added; must not be null.NullPointerException
- if dbconn is null.DatabaseException
- if an error occurs while adding the connection.public boolean connect(DatabaseConnection dbconn) throws DatabaseException
false
.
The connection is made synchronously in the calling thread, which must not be the AWT event dispatching thread.
dbconn
- the database connection to be connected.NullPointerException
- if the dbconn parameter is null.DatabaseException
- if an error occurs while connecting.IllegalStateException
- if this connection is not added to the
ConnectionManager or the calling thread is the AWT event dispatching thread.public void removeConnection(DatabaseConnection dbconn) throws DatabaseException
dbconn
- the connection to be removedDatabaseException
public void showAddConnectionDialog(JDBCDriver driver)
driver
- the JDBC driver; can be null.public void showAddConnectionDialog(JDBCDriver driver, String databaseUrl)
driver
- the JDBC driver; can be null.databaseUrl
- the database URL; can be null.public void showAddConnectionDialog(JDBCDriver driver, String databaseUrl, String user, String password)
driver
- the JDBC driver; can be null.databaseUrl
- the database URL; can be null.user
- the database user; can be null.password
- user's password; can be null.public DatabaseConnection showAddConnectionDialogFromEventThread(JDBCDriver driver)
ConnectionManager.showAddConnectionDialog(JDBCDriver)
which returns
the newly created database connection, but must be called from the event dispatching
thread.driver
- the JDBC driver; can be null.IllegalStateException
- if the calling thread is not the event
dispatching thread.public DatabaseConnection showAddConnectionDialogFromEventThread(JDBCDriver driver, String databaseUrl)
ConnectionManager.showAddConnectionDialog(JDBCDriver, String)
which returns
the newly created database connection, but must be called from the event dispatching
thread.driver
- the JDBC driver; can be null.databaseUrl
- the database URL; can be null.IllegalStateException
- if the calling thread is not the event
dispatching thread.public DatabaseConnection showAddConnectionDialogFromEventThread(JDBCDriver driver, String databaseUrl, String user, String password)
ConnectionManager.showAddConnectionDialog(JDBCDriver, String, String, String)
which returns the newly created database connection, but must be called
from the event dispatching thread.driver
- the JDBC driver; can be null.databaseUrl
- the database URL; can be null.user
- the database user; can be null.password
- user's password; can be null.IllegalStateException
- if the calling thread is not the event
dispatching thread.public void showConnectionDialog(DatabaseConnection dbconn)
dbconn
- the database connection to be connectedNullPointerException
- if the dbconn parameter is nullIllegalStateException
- if this connection is not added to the
ConnectionManager.public void disconnect(DatabaseConnection dbconn)
dbconn
- the database connection to be connectedNullPointerException
- if the dbconn parameter is nullIllegalStateException
- if this connection is not added to the
ConnectionManager.public void selectConnectionInExplorer(DatabaseConnection dbconn)
dbconn
- the connection to selectNullPointerException
- if the dbconn parameter is nullIllegalStateException
- if this connection is not added to the
ConnectionManager.public void refreshConnectionInExplorer(DatabaseConnection dbconn)
dbconn
- the connection to selectNullPointerException
- if the dbconn parameter is nullIllegalStateException
- if this connection is not added to the
ConnectionManager.public void addConnectionListener(ConnectionListener listener)
public void removeConnectionListener(ConnectionListener listener)
public DatabaseConnection getPreferredConnection(boolean fallback)
ConnectionManager.setPreferredConnection(org.netbeans.api.db.explorer.DatabaseConnection)
. If none was defined,
the preferred connection is the first one defined. The method will return null
if no connection
is specified (with fallback = false), or no connection is defined. Usually, fallback = true is the good
choice, except connection management scenarios.fallback
- if false, returns null
if no connection is explicitly defined.null
.public void setPreferredConnection(DatabaseConnection conn)
null
will return the 1st connection as the preferred.conn
- the preferred connection