List of usage examples for java.sql DriverManager getDriver
@CallerSensitive public static Driver getDriver(String url) throws SQLException
From source file:PoolingDriverExample.java
public static void printDriverStats() throws Exception { PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:"); ObjectPool<? extends Connection> connectionPool = driver.getConnectionPool("example"); System.out.println("NumActive: " + connectionPool.getNumActive()); System.out.println("NumIdle: " + connectionPool.getNumIdle()); }
From source file:com.jt.dbcp.example.ManualPoolingDriverExample.java
public static void setupDriver(String connectURI) throws Exception { ///* ww w . jav a2 s .co m*/ // First, we'll need a ObjectPool that serves as the // actual pool of connections. // // We'll use a GenericObjectPool instance, although // any ObjectPool implementation will suffice. // ObjectPool connectionPool = new GenericObjectPool(null); // // Next, we'll create a ConnectionFactory that the // pool will use to create Connections. // We'll use the DriverManagerConnectionFactory, // using the connect string passed in the command line // arguments. // ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI, "root", "root"); // // Now we'll create the PoolableConnectionFactory, which wraps // the "real" Connections created by the ConnectionFactory with // the classes that implement the pooling functionality. // PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, connectionPool, null, null, false, true); // // Finally, we create the PoolingDriver itself... // Class.forName("org.apache.commons.dbcp.PoolingDriver"); PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:"); // // ...and register our pool with it. // driver.registerPool("example", connectionPool); // // Now we can just use the connect string "jdbc:apache:commons:dbcp:example" // to access our pool of Connections. // }
From source file:de.uniwue.info6.webapp.misc.InitVariables.java
/** * @param event//from w w w . j av a 2s .co m */ @Override public void contextDestroyed(final ServletContextEvent event) { // ------------------------------------------------ // ConnectionTools.inst().cleanUp(); // ------------------------------------------------ // C3P0Registry.getNumPooledDataSources(); PooledDataSource dataSource = null; @SuppressWarnings({ "unchecked", "rawtypes" }) Iterator<Set> it = C3P0Registry.getPooledDataSources().iterator(); while (it.hasNext()) { try { dataSource = (PooledDataSource) it.next(); dataSource.close(); } catch (Exception e) { LOGGER.error("Error deregistering driver %s", dataSource, e); } } // ------------------------------------------------ // final HashMap<Scenario, ComboPooledDataSource> pools = ConnectionManager.instance().getPools(); for (Scenario scenario : pools.keySet()) { final String dbHost = scenario.getDbHost(); final String dbPort = scenario.getDbPort(); final String url = ConnectionManager.URL_PREFIX + dbHost + ":" + dbPort + "/"; Driver mariaDBDriver = null; try { final ComboPooledDataSource cpds = pools.get(scenario); mariaDBDriver = DriverManager.getDriver(url); if (mariaDBDriver != null) { DriverManager.deregisterDriver(mariaDBDriver); } DataSources.destroy(cpds); } catch (SQLException e) { } catch (Exception e) { LOGGER.error(String.format("Error deregistering driver %s", mariaDBDriver), e); } } // ------------------------------------------------ // Enumeration<Driver> drivers = DriverManager.getDrivers(); while (drivers.hasMoreElements()) { Driver driver = drivers.nextElement(); try { DriverManager.deregisterDriver(driver); LOGGER.info(String.format("deregistering jdbc driver: %s", driver)); } catch (SQLException e) { LOGGER.error(String.format("Error deregistering driver %s", driver), e); } } // ------------------------------------------------ // // for (Thread thread : Thread.getAllStackTraces().keySet()) { // } // ------------------------------------------------ // }
From source file:ManualPoolingDriverExample.java
public static void printDriverStats() throws Exception { PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:"); ObjectPool connectionPool = driver.getConnectionPool("example"); System.out.println("NumActive: " + connectionPool.getNumActive()); System.out.println("NumIdle: " + connectionPool.getNumIdle()); }
From source file:PoolingDriverExample.java
public static void shutdownDriver() throws Exception { PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:"); driver.closePool("example"); }
From source file:com.iver.utiles.connections.ConnectionDB.java
/** * Registers in the driverpool a new connection * // w w w . java 2s . co m * @param ct * Data connection * @param driver * Driver * * @throws ConnectionException */ public void setupDriver(ConnectionTrans ct) throws ConnectionException { String url = ct.getConnBeginning() + "//" + ct.getHost() + ":" + ct.getPort() + "/" + ct.getDb(); String user = ct.getUser(); String password = ct.getPassword(); String name = ct.getHost() + "_" + ct.getName(); ObjectPool connectionPool = new GenericObjectPool(); ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(url, user, password); try { PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, connectionPool, null, null, false, true); } catch (Exception e) { throw new ConnectionException(JDBCManager.getTranslation("fallo_crear_pool"), e); } try { Class.forName("org.apache.commons.dbcp.PoolingDriver"); } catch (ClassNotFoundException e) { throw new ConnectionException("Clase : " + "org.apache.commons.dbcp.PoolingDriver", e); } PoolingDriver driverPool; try { driverPool = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:"); } catch (SQLException e) { throw new ConnectionException(JDBCManager.getTranslation("fallo_registrar_conexion"), e); } driverPool.registerPool(name, connectionPool); ct.setConnected(true); }
From source file:edu.education.ucsb.muster.MusterServlet.java
private Driver registerDriver(String driver, String url) { try {//from w ww .j a v a 2 s .c o m DriverManager .registerDriver((Driver) Class.forName(driver).getConstructor().newInstance((Object[]) null)); return DriverManager.getDriver(url); } catch (Exception e) { addException(e, "Could not load driver `" + driver + "` for url `" + url + "`"); } return null; }
From source file:gemlite.core.internal.db.DBSynchronizer.java
protected synchronized void initConnection() { String maskedPasswordDbUrl = null; if (this.dbUrl != null) { maskedPasswordDbUrl = maskPassword(this.dbUrl); }//from w ww. j a v a2s. c o m try { Class.forName(this.driverClass).newInstance(); this.driver = DriverManager.getDriver(this.dbUrl); } catch (Exception e) { throw helper.newRuntimeException( String.format(DB_SYNCHRONIZER__6, this.driverClass, maskedPasswordDbUrl), e); } this.instantiateConnection(); if (this.logger.isInfoEnabled()) { this.helper.logFormat(this.logger, Level.INFO, null, DB_SYNCHRONIZER__13, maskedPasswordDbUrl, this.driverClass); } this.shutDown = false; }
From source file:com.jaspersoft.jasperserver.war.action.DataSourceAction.java
public Event testJdbcDataSource(RequestContext context) throws Exception { TestJdbcConnectionResponseBuilder response = new TestJdbcConnectionResponseBuilder().failed(); ReportDataSourceWrapper wrapper = (ReportDataSourceWrapper) getFormObject(context); JdbcReportDataSource ds = (JdbcReportDataSource) wrapper.getReportDataSource(); Connection conn = null;/*from w ww . j a v a2 s .c o m*/ try { jdbcDriverService.register(ds.getDriverClass()); // On edit datasource we set the passwordSubstitution to the passwords form fields // If we get the substitution from UI then set the password from original datasource (if it exists) if (ds.getPassword().equals(passwordSubstitution)) { JdbcReportDataSource existingDs = (JdbcReportDataSource) repository.getResource(null, ds.getURIString()); if (existingDs != null) { ds.setPassword(existingDs.getPassword()); } } Properties properties = new Properties(); properties.put("user", ds.getUsername()); properties.put("password", ds.getPassword()); Driver driver = DriverManager.getDriver(ds.getConnectionUrl()); conn = driver.connect(ds.getConnectionUrl(), properties); if (conn != null) { response.passed(); } } catch (Exception e) { logger.error("exception testing jdbc data source", e); response.failed(e); } finally { if (conn != null) conn.close(); } context.getRequestScope().put(AJAX_RESPONSE_MODEL, response.buildJson()); return success(); }
From source file:com.frameworkset.commons.dbcp2.BasicDataSource.java
/** * Creates a JDBC connection factory for this datasource. The JDBC driver * is loaded using the following algorithm: * <ol>/* ww w. j a va 2s . c o m*/ * <li>If a Driver instance has been specified via * {@link #setDriver(Driver)} use it</li> * <li>If no Driver instance was specified and {@link #driverClassName} is * specified that class is loaded using the {@link ClassLoader} of this * class or, if {@link #driverClassLoader} is set, {@link #driverClassName} * is loaded with the specified {@link ClassLoader}.</li> * <li>If {@link #driverClassName} is specified and the previous attempt * fails, the class is loaded using the context class loader of the current * thread.</li> * <li>If a driver still isn't loaded one is loaded via the * {@link DriverManager} using the specified {@link #url}. * </ol> * This method exists so subclasses can replace the implementation class. */ protected ConnectionFactory createConnectionFactory() throws SQLException { // Load the JDBC driver class Driver driverToUse = this.driver; if (driverToUse == null) { Class<?> driverFromCCL = null; if (driverClassName != null) { try { try { if (driverClassLoader == null) { driverFromCCL = Class.forName(driverClassName); } else { driverFromCCL = Class.forName(driverClassName, true, driverClassLoader); } } catch (ClassNotFoundException cnfe) { driverFromCCL = Thread.currentThread().getContextClassLoader().loadClass(driverClassName); } } catch (Exception t) { String message = "Cannot load JDBC driver class '" + driverClassName + "'"; logWriter.println(message); t.printStackTrace(logWriter); throw new SQLException(message, t); } } try { if (driverFromCCL == null) { driverToUse = DriverManager.getDriver(url); } else { // Usage of DriverManager is not possible, as it does not // respect the ContextClassLoader // N.B. This cast may cause ClassCastException which is handled below driverToUse = (Driver) driverFromCCL.newInstance(); if (!driverToUse.acceptsURL(url)) { throw new SQLException("No suitable driver", "08001"); } } } catch (Exception t) { String message = "Cannot create JDBC driver of class '" + (driverClassName != null ? driverClassName : "") + "' for connect URL '" + url + "'"; logWriter.println(message); t.printStackTrace(logWriter); throw new SQLException(message, t); } } // Set up the driver connection factory we will use String user = username; if (user != null) { connectionProperties.put("user", user); } else { log("DBCP DataSource configured without a 'username'"); } String pwd = password; if (pwd != null) { connectionProperties.put("password", pwd); } else { log("DBCP DataSource configured without a 'password'"); } ConnectionFactory driverConnectionFactory = new DriverConnectionFactory(driverToUse, url, connectionProperties); return driverConnectionFactory; }