KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > data > io > sql > ConnectionFactory


1 package prefuse.data.io.sql;
2
3 import java.sql.Connection JavaDoc;
4 import java.sql.DriverManager JavaDoc;
5 import java.sql.SQLException JavaDoc;
6
7 /**
8  * @author <a HREF="http://jheer.org">jeffrey heer</a>
9  */

10 public class ConnectionFactory {
11
12     /** String constant for the commonly used MySQL JDBC driver */
13     public static final String JavaDoc DRIVER_MYSQL = "com.mysql.jdbc.Driver";
14     /** String constant for the JDBC/ODBC bridge driver */
15     public static final String JavaDoc DRIVER_JDBC_OBDC = "sun.jdbc.odbc.JdbcOdbcDriver";
16     
17     /** Protocol prefix for JDBC URLs */
18     public static final String JavaDoc PROTOCOL_JDBC = "jdbc:";
19     /** Sub-protocol prefix for MySQL connections */
20     public static final String JavaDoc SUBPROTOCOL_MYSQL = "mysql:";
21     /** Sub-protocol prefix for JDBC/ODBC bridge connections */
22     public static final String JavaDoc SUBPROTOCOL_JDBC_ODBC = "odbc:";
23     
24     // ------------------------------------------------------------------------
25

26     /**
27      * Get an instance of the default SQL data handler.
28      * @return an instance of the default SQL data handler
29      */

30     public static SQLDataHandler getDefaultHandler() {
31         return new DefaultSQLDataHandler();
32     }
33     
34     // ------------------------------------------------------------------------
35
// Generic Connection Methods
36

37     /**
38      * Get a new database connection.
39      * @param conn the Connection object to the database
40      * @param handler the data handler to use
41      * @return a DatabaseDataSource for interacting with the database
42      * @throws SQLException if an SQL error occurs
43      */

44     public static DatabaseDataSource getDatabaseConnection(
45             Connection JavaDoc conn, SQLDataHandler handler)
46         throws SQLException JavaDoc
47     {
48         return new DatabaseDataSource(conn, handler);
49     }
50     
51     /**
52      * Get a new database connection, using a default handler.
53      * @param conn the Connection object to the database
54      * @return a DatabaseDataSource for interacting with the database
55      * @throws SQLException if an SQL error occurs
56      */

57     public static DatabaseDataSource getDatabaseConnection(Connection JavaDoc conn)
58         throws SQLException JavaDoc
59     {
60         return getDatabaseConnection(conn, getDefaultHandler());
61     }
62
63     /**
64      * Get a new database connection.
65      * @param driver the database driver to use, must resolve to a valid Java
66      * class on the current classpath.
67      * @param url the url for the database, of the form
68      * "jdbc:<database_sub_protocol>://&lt;hostname&gt;/&lt;database_name&gt;
69      * @param user the database username
70      * @param password the database password
71      * @param handler the sql data handler to use
72      * @return a DatabaseDataSource for interacting with the database
73      * @throws SQLException
74      * @throws ClassNotFoundException
75      */

76     public static DatabaseDataSource getDatabaseConnection(String JavaDoc driver,
77             String JavaDoc url, String JavaDoc user, String JavaDoc password, SQLDataHandler handler)
78         throws SQLException JavaDoc, ClassNotFoundException JavaDoc
79     {
80         Class.forName(driver);
81         Connection JavaDoc conn = DriverManager.getConnection(url, user, password);
82         return getDatabaseConnection(conn, handler);
83     }
84     
85     /**
86      * Get a new database connection, using a default handler.
87      * @param driver the database driver to use, must resolve to a valid Java
88      * class on the current classpath.
89      * @param url the url for the database, of the form
90      * "jdbc:<database_sub_protocol>://&lt;hostname&gt;/&lt;database_name&gt;
91      * @param user the database username
92      * @param password the database password
93      * @return a DatabaseDataSource for interacting with the database
94      * @throws SQLException
95      * @throws ClassNotFoundException
96      */

97     public static DatabaseDataSource getDatabaseConnection(String JavaDoc driver,
98             String JavaDoc url, String JavaDoc user, String JavaDoc password)
99         throws SQLException JavaDoc, ClassNotFoundException JavaDoc
100     {
101         return getDatabaseConnection(driver, url, user, password,
102                 getDefaultHandler());
103     }
104     
105     // ------------------------------------------------------------------------
106
// Driver Specific Methods
107

108     // -- MySQL ---------------------------------------------------------------
109

110     /**
111      * Get a new database connection to a MySQL database.
112      * @param host the ip address or host name of the database server
113      * @param database the name of the particular database to use
114      * @param user the database username
115      * @param password the database password
116      * @param handler the sql data handler to use
117      * @return a DatabaseDataSource for interacting with the database
118      * @throws SQLException
119      * @throws ClassNotFoundException
120      */

121     public static DatabaseDataSource getMySQLConnection(
122             String JavaDoc host, String JavaDoc database, String JavaDoc user, String JavaDoc password,
123             SQLDataHandler handler)
124         throws SQLException JavaDoc, ClassNotFoundException JavaDoc
125     {
126         String JavaDoc url = PROTOCOL_JDBC + SUBPROTOCOL_MYSQL
127                    + "//" + host + "/" + database;
128         return getDatabaseConnection(DRIVER_MYSQL,url,user,password,handler);
129     }
130
131     /**
132      * Get a new database connection to a MySQL database, using a default
133      * handler.
134      * @param host the ip address or host name of the database server
135      * @param database the name of the particular database to use
136      * @param user the database username
137      * @param password the database password
138      * @return a DatabaseDataSource for interacting with the database
139      * @throws SQLException
140      * @throws ClassNotFoundException
141      */

142     public static DatabaseDataSource getMySQLConnection(
143             String JavaDoc host, String JavaDoc database, String JavaDoc user, String JavaDoc password)
144         throws SQLException JavaDoc, ClassNotFoundException JavaDoc
145     {
146         return getMySQLConnection(host, database, user, password,
147                 getDefaultHandler());
148     }
149     
150 } // end of class ConnectionFactory
151
Popular Tags