001: package prefuse.data.io.sql;
002:
003: import java.sql.Connection;
004: import java.sql.DriverManager;
005: import java.sql.SQLException;
006:
007: /**
008: * @author <a href="http://jheer.org">jeffrey heer</a>
009: */
010: public class ConnectionFactory {
011:
012: /** String constant for the commonly used MySQL JDBC driver */
013: public static final String DRIVER_MYSQL = "com.mysql.jdbc.Driver";
014: /** String constant for the JDBC/ODBC bridge driver */
015: public static final String DRIVER_JDBC_OBDC = "sun.jdbc.odbc.JdbcOdbcDriver";
016:
017: /** Protocol prefix for JDBC URLs */
018: public static final String PROTOCOL_JDBC = "jdbc:";
019: /** Sub-protocol prefix for MySQL connections */
020: public static final String SUBPROTOCOL_MYSQL = "mysql:";
021: /** Sub-protocol prefix for JDBC/ODBC bridge connections */
022: public static final String SUBPROTOCOL_JDBC_ODBC = "odbc:";
023:
024: // ------------------------------------------------------------------------
025:
026: /**
027: * Get an instance of the default SQL data handler.
028: * @return an instance of the default SQL data handler
029: */
030: public static SQLDataHandler getDefaultHandler() {
031: return new DefaultSQLDataHandler();
032: }
033:
034: // ------------------------------------------------------------------------
035: // Generic Connection Methods
036:
037: /**
038: * Get a new database connection.
039: * @param conn the Connection object to the database
040: * @param handler the data handler to use
041: * @return a DatabaseDataSource for interacting with the database
042: * @throws SQLException if an SQL error occurs
043: */
044: public static DatabaseDataSource getDatabaseConnection(
045: Connection conn, SQLDataHandler handler)
046: throws SQLException {
047: return new DatabaseDataSource(conn, handler);
048: }
049:
050: /**
051: * Get a new database connection, using a default handler.
052: * @param conn the Connection object to the database
053: * @return a DatabaseDataSource for interacting with the database
054: * @throws SQLException if an SQL error occurs
055: */
056: public static DatabaseDataSource getDatabaseConnection(
057: Connection conn) throws SQLException {
058: return getDatabaseConnection(conn, getDefaultHandler());
059: }
060:
061: /**
062: * Get a new database connection.
063: * @param driver the database driver to use, must resolve to a valid Java
064: * class on the current classpath.
065: * @param url the url for the database, of the form
066: * "jdbc:<database_sub_protocol>://<hostname>/<database_name>
067: * @param user the database username
068: * @param password the database password
069: * @param handler the sql data handler to use
070: * @return a DatabaseDataSource for interacting with the database
071: * @throws SQLException
072: * @throws ClassNotFoundException
073: */
074: public static DatabaseDataSource getDatabaseConnection(
075: String driver, String url, String user, String password,
076: SQLDataHandler handler) throws SQLException,
077: ClassNotFoundException {
078: Class.forName(driver);
079: Connection conn = DriverManager.getConnection(url, user,
080: password);
081: return getDatabaseConnection(conn, handler);
082: }
083:
084: /**
085: * Get a new database connection, using a default handler.
086: * @param driver the database driver to use, must resolve to a valid Java
087: * class on the current classpath.
088: * @param url the url for the database, of the form
089: * "jdbc:<database_sub_protocol>://<hostname>/<database_name>
090: * @param user the database username
091: * @param password the database password
092: * @return a DatabaseDataSource for interacting with the database
093: * @throws SQLException
094: * @throws ClassNotFoundException
095: */
096: public static DatabaseDataSource getDatabaseConnection(
097: String driver, String url, String user, String password)
098: throws SQLException, ClassNotFoundException {
099: return getDatabaseConnection(driver, url, user, password,
100: getDefaultHandler());
101: }
102:
103: // ------------------------------------------------------------------------
104: // Driver Specific Methods
105:
106: // -- MySQL ---------------------------------------------------------------
107:
108: /**
109: * Get a new database connection to a MySQL database.
110: * @param host the ip address or host name of the database server
111: * @param database the name of the particular database to use
112: * @param user the database username
113: * @param password the database password
114: * @param handler the sql data handler to use
115: * @return a DatabaseDataSource for interacting with the database
116: * @throws SQLException
117: * @throws ClassNotFoundException
118: */
119: public static DatabaseDataSource getMySQLConnection(String host,
120: String database, String user, String password,
121: SQLDataHandler handler) throws SQLException,
122: ClassNotFoundException {
123: String url = PROTOCOL_JDBC + SUBPROTOCOL_MYSQL + "//" + host
124: + "/" + database;
125: return getDatabaseConnection(DRIVER_MYSQL, url, user, password,
126: handler);
127: }
128:
129: /**
130: * Get a new database connection to a MySQL database, using a default
131: * handler.
132: * @param host the ip address or host name of the database server
133: * @param database the name of the particular database to use
134: * @param user the database username
135: * @param password the database password
136: * @return a DatabaseDataSource for interacting with the database
137: * @throws SQLException
138: * @throws ClassNotFoundException
139: */
140: public static DatabaseDataSource getMySQLConnection(String host,
141: String database, String user, String password)
142: throws SQLException, ClassNotFoundException {
143: return getMySQLConnection(host, database, user, password,
144: getDefaultHandler());
145: }
146:
147: } // end of class ConnectionFactory
|