Java Code Examples for java.sql.DriverManager#registerDriver()

The following examples show how to use java.sql.DriverManager#registerDriver() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestValidation.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Before
public void setUp() throws SQLException {
    DriverManager.registerDriver(new MockDriver());

    // use our mock driver
    datasource.setDriverClassName(MockDriver.class.getName());
    datasource.setUrl(MockDriver.getUrlWithValidationOutcomes(ValidationOutcome.SUCCESS));

    // Required to trigger validation query's execution
    datasource.setInitialSize(1);
    datasource.setMinIdle(1);
    datasource.setMaxIdle(1);
    datasource.setMaxActive(2);
    // Validation interval is disabled by default to ensure validation occurs everytime
    datasource.setValidationInterval(-1);
    // No validation query by default
    datasource.setValidationQuery(null);
}
 
Example 2
Source File: DBCPServiceTest.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
/**
 * NB!!!! Prerequisite: file should be present in /var/tmp/mariadb-java-client-1.1.7.jar
 */
@Test
@Ignore("Intended only for local testing, not automated testing")
public void testURLClassLoader() throws ClassNotFoundException, MalformedURLException, SQLException, InstantiationException, IllegalAccessException {

    final URL url = new URL("file:///var/tmp/mariadb-java-client-1.1.7.jar");
    final URL[] urls = new URL[] { url };

    final ClassLoader parent = Thread.currentThread().getContextClassLoader();
    final URLClassLoader ucl = new URLClassLoader(urls, parent);

    final Class<?> clazz = Class.forName("org.mariadb.jdbc.Driver", true, ucl);
    assertNotNull(clazz);

    final Driver driver = (Driver) clazz.newInstance();
    final Driver shim = new DriverShim(driver);
    DriverManager.registerDriver(shim);

    final Driver driver2 = DriverManager.getDriver("jdbc:mariadb://localhost:3306/testdb");
    assertNotNull(driver2);
}
 
Example 3
Source File: UnpooledDataSource.java    From mybatis with Apache License 2.0 6 votes vote down vote up
private synchronized void initializeDriver() throws SQLException {
 //这里便是大家熟悉的初学JDBC时的那几句话了 Class.forName newInstance()
  if (!registeredDrivers.containsKey(driver)) {
    Class<?> driverType;
    try {
      if (driverClassLoader != null) {
        driverType = Class.forName(driver, true, driverClassLoader);
      } else {
        driverType = Resources.classForName(driver);
      }
      // DriverManager requires the driver to be loaded via the system ClassLoader.
      // http://www.kfu.com/~nsayer/Java/dyn-jdbc.html
      Driver driverInstance = (Driver)driverType.newInstance();
      DriverManager.registerDriver(new DriverProxy(driverInstance));
      registeredDrivers.put(driver, driverInstance);
    } catch (Exception e) {
      throw new SQLException("Error setting driver on UnpooledDataSource. Cause: " + e);
    }
  }
}
 
Example 4
Source File: TestJdbcDataSource.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testRetrieveFromDriverManager() throws Exception {
  // we're not (directly) using a Mockito based mock class here because it won't have a consistent class name
  // that will work with DriverManager's class bindings
  MockDriver mockDriver = new MockDriver(connection);
  DriverManager.registerDriver(mockDriver);
  try {
    props.put(JdbcDataSource.DRIVER, MockDriver.class.getName());
    props.put(JdbcDataSource.URL, MockDriver.MY_JDBC_URL);
    props.put("holdability", "HOLD_CURSORS_OVER_COMMIT");

    Connection conn = jdbcDataSource.createConnectionFactory(context, props).call();

    verify(connection).setAutoCommit(false);
    verify(connection).setHoldability(1);

    assertSame("connection", conn, connection);
  } catch(Exception e) {
    throw e;
  } finally {
    DriverManager.deregisterDriver(mockDriver);
  }
}
 
Example 5
Source File: DriverManagerTests.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Validate that a Connection object is returned when a valid URL is
 * specified to getConnection
 *
 */
@Test
public void test14() throws Exception {

    DriverManager.registerDriver(new StubDriver());
    assertTrue(
            DriverManager.getConnection(StubDriverURL) != null);
    assertTrue(DriverManager.getConnection(StubDriverURL,
            "LuckyDog", "tennisanyone") != null);
    Properties props = new Properties();
    props.put("user", "LuckyDog");
    props.put("password", "tennisanyone");
    assertTrue(
            DriverManager.getConnection(StubDriverURL,
                    props) != null);
}
 
Example 6
Source File: BaseTest.java    From phoenix with Apache License 2.0 5 votes vote down vote up
/**
 * Create a {@link PhoenixTestDriver} and register it.
 * @return an initialized and registered {@link PhoenixTestDriver} 
 */
protected static PhoenixTestDriver initAndRegisterDriver(String url, ReadOnlyProps props) throws Exception {
    PhoenixTestDriver newDriver = new PhoenixTestDriver(props);
    DriverManager.registerDriver(newDriver);
    Driver oldDriver = DriverManager.getDriver(url); 
    if (oldDriver != newDriver) {
        destroyDriver(oldDriver);
    }
    Connection conn = newDriver.connect(url, PropertiesUtil.deepCopy(TEST_PROPERTIES));
    conn.close();
    return newDriver;
}
 
Example 7
Source File: UnregisteredDriver.java    From calcite-avatica with Apache License 2.0 5 votes vote down vote up
/**
 * Registers this driver with the driver manager.
 */
protected void register() {
  try {
    DriverManager.registerDriver(this);
  } catch (SQLException e) {
    System.out.println(
        "Error occurred while registering JDBC driver "
        + this + ": " + e.toString());
  }
}
 
Example 8
Source File: CloudSpannerDriver.java    From spanner-jdbc with MIT License 5 votes vote down vote up
/**
 * Register the driver against {@link DriverManager}. This is done automatically when the class is
 * loaded. Dropping the driver from DriverManager's list is possible using {@link #deregister()}
 * method.
 *
 * @throws IllegalStateException if the driver is already registered
 * @throws SQLException if registering the driver fails
 */
public static void register() throws SQLException {
  if (isRegistered()) {
    throw new IllegalStateException(
        "Driver is already registered. It can only be registered once.");
  }
  CloudSpannerDriver registeredDriver = new CloudSpannerDriver();
  DriverManager.registerDriver(registeredDriver);
  CloudSpannerDriver.registeredDriver = registeredDriver;
}
 
Example 9
Source File: DriverManagerTests.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Register a driver and make sure you find it via its URL. Deregister the
 * driver and validate it is not longer registered
 *
 * @throws Exception
 */
@Test()
public void test15() throws Exception {
    DriverManager.registerDriver(new StubDriver());
    Driver d = DriverManager.getDriver(StubDriverURL);
    assertTrue(d != null);
    assertTrue(isDriverRegistered(d));
    DriverManager.deregisterDriver(d);
    assertFalse(isDriverRegistered(d));
}
 
Example 10
Source File: BasePhoenixMetricsIT.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static synchronized void doSetup() throws Exception {
    Map<String, String> props = Maps.newHashMapWithExpectedSize(3);
    // Disable system task handling
    props.put(QueryServices.TASK_HANDLING_INITIAL_DELAY_MS_ATTRIB, Long.toString(Long.MAX_VALUE));
    // Phoenix Global client metrics are enabled by default
    // Enable request metric collection at the driver level
    props.put(QueryServices.COLLECT_REQUEST_LEVEL_METRICS, String.valueOf(true));
    // disable renewing leases as this will force spooling to happen.
    props.put(QueryServices.RENEW_LEASE_ENABLED, String.valueOf(false));
    setUpTestDriver(new ReadOnlyProps(props.entrySet().iterator()));
    // need the non-test driver for some tests that check number of hconnections, etc.
    DriverManager.registerDriver(PhoenixDriver.INSTANCE);

}
 
Example 11
Source File: TestJdbcWrapper.java    From javamelody with Apache License 2.0 5 votes vote down vote up
/** Test.
 * @throws SQLException e */
@Before
public void setUp() throws SQLException {
	Utils.initialize();
	Utils.setProperty(Parameter.SYSTEM_ACTIONS_ENABLED, Boolean.TRUE.toString());
	driver = new JdbcDriver();
	DriverManager.registerDriver(driver);
	jdbcWrapper = JdbcWrapper.SINGLETON;
}
 
Example 12
Source File: DriverManagerTests.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Validate that a non-null Driver is returned by getDriver when a valid URL
 * is specified
 */
@Test
public void test12() throws Exception {

    DriverManager.registerDriver(new StubDriver());
    assertTrue(DriverManager.getDriver(StubDriverURL) != null);
}
 
Example 13
Source File: DriverManagerTests.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Validate that SQLException is thrown when the URL is not valid for any of
 * the registered drivers
 */
@Test(expectedExceptions = SQLException.class)
public void test13() throws Exception {
    DriverManager.registerDriver(new StubDriver());
    DriverManager.getDriver(InvalidURL);
}
 
Example 14
Source File: TestValidityTools.java    From gsn with GNU General Public License v3.0 4 votes vote down vote up
@BeforeClass
public static void setUpBeforeClass() throws Exception {
	DriverManager.registerDriver( new org.h2.Driver( ) );
	sm = StorageManagerFactory.getInstance( "org.hsqldb.jdbcDriver","sa","" ,"jdbc:hsqldb:mem:.", Main.DEFAULT_MAX_DB_CONNECTIONS);
}
 
Example 15
Source File: TestVSensorLoader.java    From gsn with GNU General Public License v3.0 4 votes vote down vote up
@BeforeClass
public static void setUpBeforeClass() throws Exception {
	DriverManager.registerDriver( new org.h2.Driver( ) );
	sm = StorageManagerFactory.getInstance( "org.hsqldb.jdbcDriver","sa","" ,"jdbc:hsqldb:mem:.", Main.DEFAULT_MAX_DB_CONNECTIONS);
}
 
Example 16
Source File: DriverManagerTests.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Validate that NullPointerException is thrown when null is passed to
 * registerDriver
 */
@Test(expectedExceptions = NullPointerException.class)
public void test1() throws Exception {
    Driver d = null;
    DriverManager.registerDriver(d);
}
 
Example 17
Source File: DriverManagerTests.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Validate that SQLException is thrown when the URL is not valid for any of
 * the registered drivers
 */
@Test(expectedExceptions = SQLException.class)
public void test13() throws Exception {
    DriverManager.registerDriver(new StubDriver());
    DriverManager.getDriver(InvalidURL);
}
 
Example 18
Source File: MSSSqlConnectionIT.java    From pinpoint with Apache License 2.0 4 votes vote down vote up
@Before
public void registerDriver() throws Exception {
    Driver driver = driverClass.getDriver().newInstance();
    DriverManager.registerDriver(driver);
}
 
Example 19
Source File: DriverManagerTests.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Validate that NullPointerException is thrown when null is passed to
 * registerDriver
 */
@Test(expectedExceptions = NullPointerException.class)
public void test2() throws Exception {
    Driver d = null;
    DriverManager.registerDriver(d, null);
}
 
Example 20
Source File: Init.java    From bbs with GNU Affero General Public License v3.0 3 votes vote down vote up
/**
 * 获取数据库连接
 * @return
 */
private static Connection getConnection() throws Exception{
	Install install = getDatabaseParameter();

	//linux下5.7必须加这句,不然会报错java.sql.SQLException: No suitable driver found for
	DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());//注册驱动
	
	return DriverManager.getConnection(install.getDatabaseURL().trim(), install.getDatabaseUser().trim(), install.getDatabasePassword().trim());	
	
}