Mp3dings 
20100427

net.sourceforge.mp3dings.export
Class DatabaseConnection

java.lang.Object
  extended by net.sourceforge.mp3dings.export.DatabaseConnection
Direct Known Subclasses:
DatabaseExporter, DatabaseImporter

public class DatabaseConnection
extends Object

Manages and establishes a connection to a JDBC database.

Version:
$Revision: 143 $ ($Date: 2008-05-14 09:18:25 +0200 (Mi, 14 Mai 2008) $)
Author:
Moritz Ringler

Field Summary
static String P_CLASSPATH
          The name of the property holding the file path of the jar archive with the JDBC driver.
static String P_DRIVER
          The name of the property holding the name of the JDBC driver class.
static String P_FIELD_PREFIX
          The prefix for property names that designate an mp3dings field.
static String P_PASSWORD
          The name of the property holding the database password.
static String P_TABLE
          The name of the property holding the name of the database table to interchange data with.
static String P_URL
          The name of the property holding the jdbc: URL of the database.
static String P_USER
          The name of the property holding the database user name.
static String PREF_EXPORT
          The preferences key at which the database export configuration is saved.
static String PREF_IMPORT
          The preferences key at which the database import configuration is saved.
 
Constructor Summary
DatabaseConnection(Properties config, ClassLoaderProvider clp)
          Establishes a new DatabaseConnection using the specified configuration.
 
Method Summary
 void close()
          Closes the Database connection.
static Properties editConfig(Component parent, Properties config, Field[] availableFields, ClassLoaderProvider clp, boolean allowViews)
          Displays a wizard to interactively create or modify a JDBC configuration that can be used to establish a database connection.
protected  Connection getConnection()
          Gets the jdbc connection established at instantiation.
 boolean isAlive()
          Returns whether the database connection established at instantiation is still alive.
static Properties loadConfig(String prefkey)
          Tries to load a previously saved JDBC configuration.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

P_DRIVER

public static final String P_DRIVER
The name of the property holding the name of the JDBC driver class.

See Also:
Constant Field Values

P_CLASSPATH

public static final String P_CLASSPATH
The name of the property holding the file path of the jar archive with the JDBC driver.

See Also:
Constant Field Values

P_USER

public static final String P_USER
The name of the property holding the database user name.

See Also:
Constant Field Values

P_PASSWORD

public static final String P_PASSWORD
The name of the property holding the database password.

See Also:
Constant Field Values

P_URL

public static final String P_URL
The name of the property holding the jdbc: URL of the database.

See Also:
Constant Field Values

P_TABLE

public static final String P_TABLE
The name of the property holding the name of the database table to interchange data with.

See Also:
Constant Field Values

P_FIELD_PREFIX

public static final String P_FIELD_PREFIX
The prefix for property names that designate an mp3dings field. These properties hold the name of the corresponding column of the database table.

See Also:
Constant Field Values

PREF_EXPORT

public static final String PREF_EXPORT
The preferences key at which the database export configuration is saved.

See Also:
Constant Field Values

PREF_IMPORT

public static final String PREF_IMPORT
The preferences key at which the database import configuration is saved.

See Also:
Constant Field Values
Constructor Detail

DatabaseConnection

public DatabaseConnection(Properties config,
                          ClassLoaderProvider clp)
                   throws ExportException,
                          SQLException
Establishes a new DatabaseConnection using the specified configuration. Such a configuration can be created interactively with the editConfig method.

Parameters:
config - the jdbc configuration
clp - a classloader provider that manages the dynamic class path for loading the JDBC driver, may be null
Throws:
ExportException - if a required property is missing in config, or if the JDBC driver cannot be loaded
SQLException - if an SQL/JDBC error occurs
Method Detail

loadConfig

public static Properties loadConfig(String prefkey)
                             throws IOException
Tries to load a previously saved JDBC configuration.

Parameters:
prefkey - the preferences key at which the configuration was stored, usually this will be PREF_EXPORT or PREF_IMPORT
Returns:
a previously stored JDBC configuration or null if no such configuration exists
Throws:
IOException - if the JDBC configuration cannot be read from the properties file whose location was stored in the preferences

editConfig

public static Properties editConfig(Component parent,
                                    Properties config,
                                    Field[] availableFields,
                                    ClassLoaderProvider clp,
                                    boolean allowViews)
                             throws SQLException
Displays a wizard to interactively create or modify a JDBC configuration that can be used to establish a database connection.

Parameters:
parent - the GUI component to use as a dialog owner
config - the previous configuration if any, may be null
availableFields - the fields that are available in the mp3dings table that will exchange data with the database
clp - a classloader provider that manages the dynamic class path for loading the JDBC driver, may be null
allowViews - whether to allow data exchange with views as well as database tables. This can be true for an import configuration, but should usually be false for an export configuration.
Throws:
SQLException
See Also:
Mp3dingsTableModel.getFields()

getConnection

protected Connection getConnection()
Gets the jdbc connection established at instantiation.

Returns:
a jdbc connection

isAlive

public boolean isAlive()
Returns whether the database connection established at instantiation is still alive.

Returns:
whether the jdbc connection is still alive
See Also:
Connection.isClosed()

close

public void close()
           throws SQLException
Closes the Database connection.

Throws:
SQLException

Mp3dings 
20100427