當前位置: 首頁>>代碼示例>>Java>>正文


Java Driver類代碼示例

本文整理匯總了Java中java.sql.Driver的典型用法代碼示例。如果您正苦於以下問題:Java Driver類的具體用法?Java Driver怎麽用?Java Driver使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


Driver類屬於java.sql包,在下文中一共展示了Driver類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: getConnection

import java.sql.Driver; //導入依賴的package包/類
public static Connection getConnection() {
    try {
        DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());

        String url = "jdbc:mysql://" +       //db type
                "localhost:" +               //host name
                "3306/" +                    //port
                "db_example?" +              //db name
                "useSSL=false&" +            //do not use ssl
                "user=tully&" +              //login
                "password=tully";            //password

        return DriverManager.getConnection(url);
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}
 
開發者ID:vitaly-chibrikov,項目名稱:otus_java_2017_04,代碼行數:18,代碼來源:ConnectionHelper.java

示例2: loadDrivers

import java.sql.Driver; //導入依賴的package包/類
/**
 * 裝載和注冊所有JDBC 驅動程序
 *
 * @param props 屬性
 */
private void loadDrivers(Vector driverBeans) {
	logger.debug("----------------------->");
	Iterator iterator = driverBeans.iterator();
	while (iterator.hasNext()) {
		DSConfigBean dsConfigBean = (DSConfigBean)iterator.next();
		try {
			if (dsConfigBean.getDriver() != null && !"".equals(dsConfigBean.getDriver())){
				Driver driver = (Driver) Class.forName(dsConfigBean.getDriver())
						.newInstance();
				DriverManager.registerDriver(driver);
				drivers.addElement(driver);
				logger.debug("成功注冊JDBC 驅動程序" + dsConfigBean.getDriver());
			}
		} catch (Exception e) {
			logger.error("注冊驅動程序出錯,",e);
		}
	}
}
 
開發者ID:langxianwei,項目名稱:iot-plat,代碼行數:24,代碼來源:DBConnectionManager.java

示例3: getDriverClass

import java.sql.Driver; //導入依賴的package包/類
@Override
public Class<? extends Driver> getDriverClass(DbEnvironment env) {
    if (env.getDbServer() != null) {
        try {
            if (isSqlAnywhereDriverAvailable()) {
                return (Class<? extends Driver>) Class.forName(SQLANYWHERE_DRIVER_CLASS_NAME);
            } else if (isIanywhereDriverAvailable()) {
                return (Class<? extends Driver>) Class.forName(IANYWHERE_DRIVER_CLASS_NAME);
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    return super.getDriverClass(env);
}
 
開發者ID:goldmansachs,項目名稱:obevo,代碼行數:17,代碼來源:IqDbPlatform.java

示例4: getConnection

import java.sql.Driver; //導入依賴的package包/類
public Connection getConnection()
        throws SQLException {
    Driver driver = getDriver();

    Properties props = new Properties();

    if (stringHasValue(userId)) {
        props.setProperty("user", userId); //$NON-NLS-1$
    }

    if (stringHasValue(password)) {
        props.setProperty("password", password); //$NON-NLS-1$
    }

    props.putAll(otherProperties);

    Connection conn = driver.connect(connectionURL, props);

    if (conn == null) {
        throw new SQLException(getString("RuntimeError.7")); //$NON-NLS-1$
    }

    return conn;
}
 
開發者ID:DomKing,項目名稱:springbootWeb,代碼行數:25,代碼來源:JDBCConnectionFactory.java

示例5: driverPropertyInfoWithoutValues

import java.sql.Driver; //導入依賴的package包/類
@Test
public void driverPropertyInfoWithoutValues() throws SQLException
{
	Driver driver = getDriver();
	DriverPropertyInfo[] properties = driver.getPropertyInfo("jdbc:cloudspanner://localhost", null);
	assertEquals(12, properties.length);
	for (DriverPropertyInfo property : properties)
	{
		if (property.name.equals("AllowExtendedMode") || property.name.equals("AsyncDdlOperations")
				|| property.name.equals("AutoBatchDdlOperations"))
			assertEquals("false", property.value);
		else if (property.name.equals("ReportDefaultSchemaAsNull"))
			assertEquals("true", property.value);
		else
			assertNull(property.value);
	}
}
 
開發者ID:olavloite,項目名稱:spanner-jdbc,代碼行數:18,代碼來源:CloudSpannerDriverTest.java

示例6: isDriverClass

import java.sql.Driver; //導入依賴的package包/類
private boolean isDriverClass(URLClassLoader jarloader, String className) {
    Class<?> clazz;

    try {
        clazz = jarloader.loadClass(className);
    } catch ( Throwable t ) {
        LOGGER.log(Level.FINE, null, t);
        
        LOGGER.log(Level.INFO, 
             "Got an exception trying to load class " +
             className + " during search for JDBC drivers in " +
             " driver jar(s): " + t.getClass().getName() + ": "
             + t.getMessage() + ".  Skipping this class..."); // NOI18N

        return false;         
    }

    if ( Driver.class.isAssignableFrom(clazz) ) {
        return true;
    }
    
    return false;
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:24,代碼來源:AddDriverDialog.java

示例7: getConnection

import java.sql.Driver; //導入依賴的package包/類
public Connection getConnection(ISqlConfig config) {
	if(conn == null){
		IConnectionParam param = config.getConnectionParam();
		 try {
			Driver d = (Driver)Class.forName
			 ("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
			if(param.getUsername().trim().length()>0){
				// USERNAME & PASSWORD is configured, let's use it for connection
				conn = DriverManager.getConnection("jdbc:odbc:"+param.getURL(),param.getUsername(),param.getPassword());	
			} else {
				conn = DriverManager.getConnection("jdbc:odbc:"+param.getURL());	
			}
	
		 } catch (Exception e) {
			throw new RuntimeException(e);
		 }
	}
	return conn;
}
 
開發者ID:manikmagar,項目名稱:adobe-air-db-connector,代碼行數:20,代碼來源:DnsConnectionManager.java

示例8: getDriver

import java.sql.Driver; //導入依賴的package包/類
/**
 * Get the driver for a JDBCDriver.  It only tries to load it using Class.forName() -
 * there is no URL to work with
 */
public Driver getDriver(JDBCDriver jdbcDriver) throws SQLException {
    ClassLoader l = getClassLoader(jdbcDriver);
    Object driver;
    try {
        driver = Class.forName(jdbcDriver.getClassName(), true, l).newInstance();
    } catch (Exception e) {
        SQLException sqlex = createDriverNotFoundException();
        sqlex.initCause(e);
        throw sqlex;
    }
    if (driver instanceof Driver) {
        return (Driver) driver;
    } else {
        throw new SQLException(driver.getClass().getName()
                + " is not a driver");                              //NOI18N
    }
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:22,代碼來源:DbDriverManager.java

示例9: createConnection

import java.sql.Driver; //導入依賴的package包/類
public static Connection createConnection(Properties p,File[] f) throws Exception{
    String driver_name=p.getProperty(DRIVER_CLASS_NAME);
    String url=p.getProperty(URL);
    String user=p.getProperty(USER);
    String passwd=p.getProperty(PASSWORD);
    ArrayList list=new java.util.ArrayList();
    for(int i=0;i<f.length;i++){
        list.add(f[i].toURI().toURL());
    }
    URL[] driverURLs=(URL[])list.toArray(new URL[0]);
    URLClassLoader l = new URLClassLoader(driverURLs);
    Class c = Class.forName(driver_name, true, l);
    Driver driver=(Driver)c.newInstance();
    Connection con=driver.connect(url,p);
    return con;
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:17,代碼來源:DbUtil.java

示例10: createFromJdbcUrl

import java.sql.Driver; //導入依賴的package包/類
private static DataSource createFromJdbcUrl(Class<? extends Driver> driverClass, String url,
        Credential credential, int numThreads, ImmutableList<String> initSqls, Properties extraConnectionProperties) {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName(driverClass.getName());
    dataSource.setUrl(url);
    dataSource.setUsername(credential.getUsername());
    dataSource.setPassword(credential.getPassword());

    // connection pool settings
    dataSource.setInitialSize(numThreads);
    dataSource.setMaxActive(numThreads);
    // keep the connections open if possible; only close them via the removeAbandonedTimeout feature
    dataSource.setMaxIdle(numThreads);
    dataSource.setMinIdle(0);
    dataSource.setRemoveAbandonedTimeout(300);

    dataSource.setConnectionInitSqls(initSqls.castToList());
    if (extraConnectionProperties != null) {
        for (String key : extraConnectionProperties.stringPropertyNames()) {
            dataSource.addConnectionProperty(key, extraConnectionProperties.getProperty(key));
        }
    }

    return dataSource;
}
 
開發者ID:goldmansachs,項目名稱:obevo,代碼行數:26,代碼來源:JdbcDataSourceFactory.java

示例11: getDriverLoaderByName

import java.sql.Driver; //導入依賴的package包/類
/**
 * 加載對應路徑jar包裏的對應驅動
 *
 * @param fname 對應路徑  如: lib4/ojdbc14.jar
 * @param dname 驅動名  如: oracle.jdbc.driver.OracleDriver
 * @return 加載到的驅動    java.sql.Driver
 * @throws Exception
 * @author tangxr
 */
public static Driver getDriverLoaderByName(String fname, String dname) throws Exception {
    if (null == fname || "".equals(fname)) {
        LOG.error("對應的驅動路徑不存在,請確認.");
        return null;
    }
    if (null == dname || "".equals(dname)) {
        LOG.error("對應的驅動類的名字不存在.");
        return null;
    }
    File file = new File(fname);
    if (!file.exists()) {
        LOG.error("對應的驅動jar不存在.");
        return null;
    }
    URLClassLoader loader = new URLClassLoader(new URL[]{file.toURI().toURL()});
    loader.clearAssertionStatus();

    return (Driver) loader.loadClass(dname).newInstance();

}
 
開發者ID:DevOpsStudio,項目名稱:Re-Collector,代碼行數:30,代碼來源:DriverLoader.java

示例12: deregisterJdbcDrivers

import java.sql.Driver; //導入依賴的package包/類
private void deregisterJdbcDrivers() {
  ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
  Enumeration<Driver> drivers = DriverManager.getDrivers();

  while (drivers.hasMoreElements()) {
    LOGGER.debug("Deregistering JDBC Drivers:");
    Driver driver = drivers.nextElement();
    if (driver.getClass().getClassLoader() == classLoader) {
      try {
        LOGGER.debug("  {} v{}.{}",
            driver.getClass().getName(),
            driver.getMajorVersion(),
            driver.getMinorVersion()
        );

        DriverManager.deregisterDriver(driver);
      } catch (SQLException e) {
        LOGGER.error("Failed to deregister JDBC driver: ", e);
      }
    }
  }
}
 
開發者ID:durimkryeziu,項目名稱:jersey-2.x-webapp-for-servlet-container,代碼行數:23,代碼來源:SampleApplicationDestroyListener.java

示例13: getConnection

import java.sql.Driver; //導入依賴的package包/類
@Override
public Connection getConnection()
        throws SQLException {
    Driver driver = getDriver();

    Properties props = new Properties();

    if (stringHasValue(userId)) {
        props.setProperty("user", userId); //$NON-NLS-1$
    }

    if (stringHasValue(password)) {
        props.setProperty("password", password); //$NON-NLS-1$
    }

    props.putAll(otherProperties);

    Connection conn = driver.connect(connectionURL, props);

    if (conn == null) {
        throw new SQLException(getString("RuntimeError.7")); //$NON-NLS-1$
    }

    return conn;
}
 
開發者ID:nextyu,項目名稱:summer-mybatis-generator,代碼行數:26,代碼來源:JDBCConnectionFactory.java

示例14: setDriverClassName

import java.sql.Driver; //導入依賴的package包/類
/**
 * Set JDBC Driver class name to use
 * @param driverClassName JDBC Driver class name
 */
@SuppressWarnings("unchecked")
public void setDriverClassName(String driverClassName) {
	ObjectUtils.argumentNotNull(driverClassName, "Driver class must be not null");
	try {
		Class<?> driverClass = Class.forName(driverClassName.trim(), true, getClass().getClassLoader());

		if (!Driver.class.isAssignableFrom(driverClass)) {
			throw new IllegalStateException("Class: " + driverClassName + " is not a valid JDBC Driver class");
		}

		setDriverClass((Class<? extends Driver>) driverClass);

	} catch (ClassNotFoundException e) {
		throw new IllegalStateException("Failed to load JDBC driver class: " + driverClassName, e);
	}
}
 
開發者ID:holon-platform,項目名稱:holon-jdbc,代碼行數:21,代碼來源:DefaultBasicDataSource.java

示例15: getConnection

import java.sql.Driver; //導入依賴的package包/類
public Connection getConnection(JDBCConnectionConfiguration config)
		throws SQLException {
	Driver driver = getDriver(config);

	Properties props = new Properties();

	if (StringUtility.stringHasValue(config.getUserId())) {
		props.setProperty("user", config.getUserId()); //$NON-NLS-1$
	}

	if (StringUtility.stringHasValue(config.getPassword())) {
		props.setProperty("password", config.getPassword()); //$NON-NLS-1$
	}

	props.putAll(config.getProperties());

	Connection conn = driver.connect(config.getConnectionURL(), props);

	if (conn == null) {
		throw new SQLException(Messages.getString("RuntimeError.7")); //$NON-NLS-1$
	}

	return conn;
}
 
開發者ID:ajtdnyy,項目名稱:PackagePlugin,代碼行數:25,代碼來源:ConnectionFactory.java


注:本文中的java.sql.Driver類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。