当前位置: 首页>>代码示例>>Java>>正文


Java Driver.connect方法代码示例

本文整理汇总了Java中java.sql.Driver.connect方法的典型用法代码示例。如果您正苦于以下问题:Java Driver.connect方法的具体用法?Java Driver.connect怎么用?Java Driver.connect使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.sql.Driver的用法示例。


在下文中一共展示了Driver.connect方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: 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

示例2: 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:bandaotixi,项目名称:generator_mybatis,代码行数:25,代码来源:JDBCConnectionFactory.java

示例3: connectToDB

import java.sql.Driver; //导入方法依赖的package包/类
public static void connectToDB() throws Exception {
    if (connection == null) {
        info = new Properties();
        info.load(new FileInputStream("src/test/resources/liquibase.properties"));

        url = info.getProperty("url");
        driver = (Driver) Class.forName(DatabaseFactory.getInstance().findDefaultDriver(url), true,
                Thread.currentThread().getContextClassLoader()).newInstance();

        connection = driver.connect(url, info);

        if (connection == null) {
            throw new DatabaseException("Connection could not be created to " + url + " with driver "
                    + driver.getClass().getName() + ".  Possibly the wrong driver for the given database URL");
        }

        jdbcConnection = new JdbcConnection(connection);
    }
}
 
开发者ID:CDKGlobal,项目名称:liquibase-snowflake,代码行数:20,代码来源:BaseTestCase.java

示例4: 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

示例5: connect

import java.sql.Driver; //导入方法依赖的package包/类
@Override
public Connection connect(String url, Properties info) throws SQLException {
  // if there is no url, we have problems
  if (url == null) {
    throw new SQLException("url is required");
  }

  if (!acceptsURL(url)) {
    return null;
  }

  String realUrl = extractRealUrl(url);
  String dbType = extractDbType(realUrl);
  String dbUser = info.getProperty("user");

  // find the real driver for the URL
  Driver wrappedDriver = findDriver(realUrl);
  Connection connection = wrappedDriver.connect(realUrl, info);

  return new TracingConnection(connection, dbType, dbUser, url.contains(WITH_ACTIVE_SPAN_ONLY));
}
 
开发者ID:opentracing-contrib,项目名称:java-jdbc,代码行数:22,代码来源:TracingDriver.java

示例6: connect

import java.sql.Driver; //导入方法依赖的package包/类
/**
 * Connects to the HSQLDB database in the given directory.
 *
 * @param databaseDirectory HSQLDB database directory.
 * @return new JDBC connection
 * @throws IOException if there are errors while connection
 */
public Connection connect(final File databaseDirectory) throws IOException {

  // Create driver
  final Driver driver = createHSQLDBDriver();

  // Save the datbase in the format acceptable for the upgrade.
  final String connURL = "jdbc:hsqldb:" + IoUtils.getCanonicalPathHard(databaseDirectory);
  final Properties connProps = new Properties();
  connProps.setProperty("user", PersistanceConstants.DATABASE_USER_NAME);
  connProps.setProperty("password", PersistanceConstants.DATABASE_PASSWORD);
  try {
    return driver.connect(connURL, connProps);
  } catch (SQLException e) {
    throw IoUtils.createIOException(e);
  }
}
 
开发者ID:parabuild-ci,项目名称:parabuild-ci,代码行数:24,代码来源:HSQLDBConnector.java

示例7: makeJDBCConnection

import java.sql.Driver; //导入方法依赖的package包/类
private static Connection makeJDBCConnection() {
  try {
    final String catalinaBase = System.getProperty("catalina.base");
    final String databaseHome = (new File(catalinaBase, "data/parabuild")).getAbsolutePath();
    final Properties props = new Properties();
    props.setProperty("user", PersistanceUtils.DATABASE_USER_NAME);
    props.setProperty("password", PersistanceUtils.DATABASE_PASSWORD);
    final Driver driver = (Driver) Class.forName("org.hsqldb.jdbcDriver").newInstance();
    final Connection connection = driver.connect("jdbc:hsqldb:" + databaseHome, props);
    connection.setAutoCommit(false);
    return connection;
  } catch (Exception e) {
    final IllegalStateException ise = new IllegalStateException(StringUtils.toString(e));
    ise.initCause(e);
    throw ise;
  }
}
 
开发者ID:parabuild-ci,项目名称:parabuild-ci,代码行数:18,代码来源:ServersideTestCase.java

示例8: 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

示例9: getConnection

import java.sql.Driver; //导入方法依赖的package包/类
private Connection getConnection() throws Exception {
    Driver driver = getDriver();
    Properties props = new Properties();
    props.put("user", user == null ? "" : user);
    if (password != null) {
        props.put("password", password);
    }

    Connection connection = driver.connect(url, props);
    assertNotNull(connection);

    return connection;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:14,代码来源:ColumnElementTest.java

示例10: connect

import java.sql.Driver; //导入方法依赖的package包/类
public static Connection connect(String url, String driverClassName, String user, String password, boolean readOnly) throws ConnectFailedException {
	try {
		if (driverClassName == null)
			throw new ConfigurationError("No JDBC driver class name provided");
		
		// Instantiate driver
           Class<Driver> driverClass = BeanUtil.forName(driverClassName);
           Driver driver = driverClass.newInstance();
           
           // Wrap connection properties
        java.util.Properties info = new java.util.Properties();
		if (user != null)
		    info.put("user", user);
		if (password != null)
		    info.put("password", password);
		
		// connect
           JDBC_LOGGER.debug("opening connection to " + url);
           Connection connection = driver.connect(url, info);
           if (connection == null)
           	throw new ConnectFailedException("Connecting the database failed silently - " +
           			"probably due to wrong driver (" + driverClassName + ") or wrong URL format (" + url + ")");
           connection = wrapWithPooledConnection(connection, readOnly);
		return connection;
       } catch (Exception e) {
		throw new ConnectFailedException("Connecting " + url + " failed: ", e);
	}
}
 
开发者ID:aravindc,项目名称:jdbacl,代码行数:29,代码来源:DBUtil.java

示例11: initSql

import java.sql.Driver; //导入方法依赖的package包/类
private void initSql() {
	String path = "db.sql";
	try(InputStream script = new FileInputStream(path)){
		String sql = IOUtils.toString(script, StandardCharsets.UTF_8);
        Properties info = new Properties();
        info.put("user", postgres.getUsername());
        info.put("password", postgres.getPassword());
        Driver driver = new org.postgresql.Driver();
        try(Connection connection = driver.connect(postgres.getJdbcUrl(), info)){
        	ScriptUtils.executeSqlScript(connection, path, sql);
		}
	} catch (IOException | SQLException | ScriptException e) {
		throw new RuntimeException(e);
	}
}
 
开发者ID:FroMage,项目名称:redpipe,代码行数:16,代码来源:ApiTest.java

示例12: getConnection

import java.sql.Driver; //导入方法依赖的package包/类
public static Connection getConnection(String dbType, String url, String username, String password) throws Exception {
	String driverLocation = Activator.getDefault().getPreferenceStore().getString(dbType);
	DbDialect dbDialect = DbDialectManager.getDbDialect(dbType);
	Driver driver = (Driver) ClassUtils.loadJdbcDriverClass(dbDialect.getJdbcDriverName(), driverLocation).newInstance();
	Properties info = new Properties();
	info.put("remarksReporting", "true");
	info.setProperty("user", username);
	info.setProperty("password", password);
	Connection con = driver.connect(url, info);
	return con;
}
 
开发者ID:bsteker,项目名称:bdf2,代码行数:12,代码来源:DbJdbcUtils.java

示例13: connect

import java.sql.Driver; //导入方法依赖的package包/类
public Connection connect(String url, Properties info) throws SQLException
{
	Driver d = getDriverLoading(url);
	if(d == null)
	{
		return null;
	}
	lastUnderlyingDriverRequested = d;
	Connection c = d.connect(url, info);
	if(c == null)
	{
		throw new SQLException("invalid or unknown driver url: " + url);
	}
	if(log.isJdbcLoggingEnabled())
	{
		ConnectionSpy cspy = new ConnectionSpy(c);
		RdbmsSpecifics r = null;
		String dclass = d.getClass().getName();
		if(dclass != null && dclass.length() > 0)
		{
			r = (RdbmsSpecifics) rdbmsSpecifics.get(dclass);
		}
		if(r == null)
		{
			r = defaultRdbmsSpecifics;
		}
		cspy.setRdbmsSpecifics(r);
		return cspy;
	}
	else
	{
		return c;
	}
}
 
开发者ID:skeychen,项目名称:dswork,代码行数:35,代码来源:DriverSpy.java

示例14: createDatabase

import java.sql.Driver; //导入方法依赖的package包/类
/**
 * Creates a new empty database in the Derby system and registers
 * it in the Database Explorer. A <code>DatabaseException</code> is thrown
 * if a database with the given name already exists.
 *
 * <p>This method requires at least the Derby network driver to be registered.
 * Otherwise it will throw an IllegalStateException.</p>
 *
 * <p>This method might take a long time to perform. It is advised that
 * clients do not call this method from the event dispatching thread,
 * where it would block the UI.</p>
 *
 * @param  databaseName the name of the database to created; cannot be nul.
 * @param  user the user to set up authentication for. No authentication
 *         will be set up if <code>user</code> is null or an empty string.
 * @param  password the password for authentication.
 *
 * @throws NullPointerException if <code>databaseName</code> is null.
 * @throws IllegalStateException if the Derby network driver is not registered.
 * @throws DatabaseException if an error occurs while creating the database
 *         or registering it in the Database Explorer.
 * @throws IOException if the Derby system home directory does not exist
 *         and it cannot be created.
 */
public  DatabaseConnection createDatabase(String databaseName, String user, String password) throws DatabaseException, IOException, IllegalStateException {
    if (databaseName == null) {
        throw new NullPointerException("The databaseName parameter cannot be null"); // NOI18N
    }

    ensureSystemHome();
    if (!RegisterDerby.getDefault().ensureStarted(true)) {
        throw new DatabaseException("The Derby server did not start"); // NOI18N
    }

    Driver driver = loadDerbyNetDriver();
    Properties props = new Properties();
    boolean setupAuthentication = (user != null && user.length() >= 0);

    try {
        String url = "jdbc:derby://localhost:" + RegisterDerby.getDefault().getPort() + "/" + databaseName; // NOI18N
        String urlForCreation = url + ";create=true"; // NOI18N
        Connection connection = driver.connect(urlForCreation, props);


        try {
            if (setupAuthentication) {
                setupDatabaseAuthentication(connection, user, password);
            }
        } finally {
            connection.close();
        }

        if (setupAuthentication) {
            // we have to reboot the database for the authentication properties
            // to take effect
            try {
                connection = driver.connect(url + ";shutdown=true", props); // NOI18N
            } catch (SQLException e) {
                // OK, will always occur
            }
        }
    } catch (SQLException sqle) {
        throw new DatabaseException(sqle);
    }

    return registerDatabase(databaseName, user,
            setupAuthentication ? user.toUpperCase() : "APP", // NOI18N
            setupAuthentication ? password : null, setupAuthentication);
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:70,代码来源:DerbyDatabasesImpl.java

示例15: testGetConnection

import java.sql.Driver; //导入方法依赖的package包/类
/**
 * Tests the driver priority if there are multiple drivers which accept the same URL.
 * Also tests the getSameDriverConnection() method.
 */
public void testGetConnection() throws Exception {
    // register a driver to DriverManager
    Driver ddm = new DriverImpl(DriverImpl.DEFAULT_URL);
    DriverManager.registerDriver(ddm);
    try {
        // register a driver to DbDriverManager
        Driver dreg = new DriverImpl(DriverImpl.DEFAULT_URL);
        DbDriverManager.getDefault().registerDriver(dreg);
        try {
            // create a JDBC driver
            JDBCDriver drv = createJDBCDriver();

            Connection conn, newConn;

            // the drivers registered with DbDriverManager have the greatest priority
            conn = DbDriverManager.getDefault().getConnection(DriverImpl.DEFAULT_URL, new Properties(), drv);
            assertSame(dreg, ((ConnectionEx)conn).getDriver());
            // also test the getSameDriverConnection() method
            newConn = DbDriverManager.getDefault().getSameDriverConnection(conn, DriverImpl.DEFAULT_URL, new Properties());
            assertSame(((ConnectionEx)conn).getDriver(), ((ConnectionEx)newConn).getDriver());

            // if nothing registered, try to load a driver from the JDBCDriver URLs 
            DbDriverManager.getDefault().deregisterDriver(dreg);
            conn = DbDriverManager.getDefault().getConnection(DriverImpl.DEFAULT_URL, new Properties(), drv);
            assertNotSame(dreg, ((ConnectionEx)conn).getDriver());
            assertNotSame(ddm, ((ConnectionEx)conn).getDriver());
            // also test the getSameDriverConnection() method
            newConn = DbDriverManager.getDefault().getSameDriverConnection(conn, DriverImpl.DEFAULT_URL, new Properties());
            assertSame(((ConnectionEx)conn).getDriver(), ((ConnectionEx)newConn).getDriver());

            // if no JDBCDriver, try DriverManager
            conn = DbDriverManager.getDefault().getConnection(DriverImpl.DEFAULT_URL, new Properties(), null);
            assertSame(ddm, ((ConnectionEx)conn).getDriver());
            // also test the getSameDriverConnection() method
            newConn = DbDriverManager.getDefault().getSameDriverConnection(conn, DriverImpl.DEFAULT_URL, new Properties());
            assertSame(((ConnectionEx)conn).getDriver(), ((ConnectionEx)newConn).getDriver());
            
            // test if getSameDriverConnection() throws IAE when passed a conn not obtained from DbDriverManager
            conn = dreg.connect(DriverImpl.DEFAULT_URL, new Properties());
            try {
                DbDriverManager.getDefault().getSameDriverConnection(conn, DriverImpl.DEFAULT_URL, new Properties());
                fail();
            } catch (IllegalArgumentException e) {
                // ok
            }
        } finally {
            DbDriverManager.getDefault().deregisterDriver(dreg);
        }
    } finally {
        DriverManager.deregisterDriver(ddm);
    }
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:57,代码来源:DbDriverManagerTest.java


注:本文中的java.sql.Driver.connect方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。