本文整理匯總了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;
}
示例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;
}
示例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);
}
}
示例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;
}
示例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));
}
示例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);
}
}
示例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;
}
}
示例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;
}
示例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;
}
示例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);
}
}
示例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);
}
}
示例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;
}
示例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;
}
}
示例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);
}
示例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);
}
}