1 package prefuse.data.io.sql; 2 3 import java.sql.Connection ; 4 import java.sql.DriverManager ; 5 import java.sql.SQLException ; 6 7 10 public class ConnectionFactory { 11 12 13 public static final String DRIVER_MYSQL = "com.mysql.jdbc.Driver"; 14 15 public static final String DRIVER_JDBC_OBDC = "sun.jdbc.odbc.JdbcOdbcDriver"; 16 17 18 public static final String PROTOCOL_JDBC = "jdbc:"; 19 20 public static final String SUBPROTOCOL_MYSQL = "mysql:"; 21 22 public static final String SUBPROTOCOL_JDBC_ODBC = "odbc:"; 23 24 26 30 public static SQLDataHandler getDefaultHandler() { 31 return new DefaultSQLDataHandler(); 32 } 33 34 37 44 public static DatabaseDataSource getDatabaseConnection( 45 Connection conn, SQLDataHandler handler) 46 throws SQLException 47 { 48 return new DatabaseDataSource(conn, handler); 49 } 50 51 57 public static DatabaseDataSource getDatabaseConnection(Connection conn) 58 throws SQLException 59 { 60 return getDatabaseConnection(conn, getDefaultHandler()); 61 } 62 63 76 public static DatabaseDataSource getDatabaseConnection(String driver, 77 String url, String user, String password, SQLDataHandler handler) 78 throws SQLException , ClassNotFoundException 79 { 80 Class.forName(driver); 81 Connection conn = DriverManager.getConnection(url, user, password); 82 return getDatabaseConnection(conn, handler); 83 } 84 85 97 public static DatabaseDataSource getDatabaseConnection(String driver, 98 String url, String user, String password) 99 throws SQLException , ClassNotFoundException 100 { 101 return getDatabaseConnection(driver, url, user, password, 102 getDefaultHandler()); 103 } 104 105 108 110 121 public static DatabaseDataSource getMySQLConnection( 122 String host, String database, String user, String password, 123 SQLDataHandler handler) 124 throws SQLException , ClassNotFoundException 125 { 126 String url = PROTOCOL_JDBC + SUBPROTOCOL_MYSQL 127 + "//" + host + "/" + database; 128 return getDatabaseConnection(DRIVER_MYSQL,url,user,password,handler); 129 } 130 131 142 public static DatabaseDataSource getMySQLConnection( 143 String host, String database, String user, String password) 144 throws SQLException , ClassNotFoundException 145 { 146 return getMySQLConnection(host, database, user, password, 147 getDefaultHandler()); 148 } 149 150 } | Popular Tags |