List of usage examples for java.sql Driver connect
Connection connect(String url, java.util.Properties info) throws SQLException;
From source file:org.codehaus.mojo.dbupgrade.sqlexec.DefaultSQLExec.java
private Connection createConnection(Driver driverInstance, Properties driverProperties) throws SQLException { Connection connection = null; for (int i = 0; i < config.getConnectionRetries(); ++i) { try {// w w w .jav a2 s . co m connection = driverInstance.connect(config.getUrl(), driverProperties); if (connection == null) { // Driver doesn't understand the URL throw new RuntimeException("No suitable Driver for " + config.getUrl()); } break; } catch (SQLException e) { if (i < config.getConnectionRetries()) { try { Thread.sleep(config.getConnectionRetryDelay()); } catch (Exception iex) { throw new SQLException("Unable to connect to " + config.getUrl(), iex); } continue; } throw new SQLException("Unable to connect to " + config.getUrl(), e); } } return connection; }
From source file:org.codehaus.mojo.sql.SqlExecMojo.java
/** * Creates a new Connection as using the driver, url, userid and password * specified./* w w w . jav a 2 s .c o m*/ * * The calling method is responsible for closing the connection. * * @return Connection the newly created connection. * @throws MojoExecutionException if the UserId/Password/Url is not set or there * is no suitable driver or the driver fails to load. * @throws SQLException if there is problem getting connection with valid url * */ private Connection getConnection() throws MojoExecutionException, SQLException { getLog().debug("connecting to " + getUrl()); Properties info = new Properties(); info.put("user", getUsername()); if (!enableAnonymousPassword) { info.put("password", getPassword()); } info.putAll(this.getDriverProperties()); Driver driverInstance = null; try { Class dc = Class.forName(getDriver()); driverInstance = (Driver) dc.newInstance(); } catch (ClassNotFoundException e) { throw new MojoExecutionException("Driver class not found: " + getDriver(), e); } catch (Exception e) { throw new MojoExecutionException("Failure loading driver: " + getDriver(), e); } Connection conn = driverInstance.connect(getUrl(), info); if (conn == null) { // Driver doesn't understand the URL throw new SQLException("No suitable Driver for " + getUrl()); } conn.setAutoCommit(autocommit); return conn; }
From source file:org.dbflute.intro.app.logic.dfprop.TestConnectionLogic.java
public void testConnection(String dbfluteVersion, OptionalThing<String> jdbcDriverJarPath, DatabaseInfoMap databaseInfoMap) { final ProxySelector proxySelector = ProxySelector.getDefault(); ProxySelector.setDefault(null); Connection connection = null; try {/*from w ww . j av a 2 s .c o m*/ final Driver driver = prepareJdbcDriver(dbfluteVersion, jdbcDriverJarPath, databaseInfoMap); final Properties info = new Properties(); final DbConnectionBox dbConnectionBox = databaseInfoMap.getDbConnectionBox(); final String user = dbConnectionBox.getUser(); if (DfStringUtil.is_NotNull_and_NotEmpty(user)) { info.put("user", user); } final String password = dbConnectionBox.getPassword(); if (DfStringUtil.is_NotNull_and_NotEmpty(password)) { info.put("password", password); } logger.debug("...Connecting the database: " + databaseInfoMap); connection = driver.connect(dbConnectionBox.getUrl(), info); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | MalformedURLException | SQLException e) { final String failureHint = e.getClass().getName() + " :: " + e.getMessage(); throw new DatabaseConnectionException("Failed to test the connection: " + databaseInfoMap, failureHint, e); } finally { ProxySelector.setDefault(proxySelector); if (connection != null) { try { connection.close(); } catch (SQLException ignored) { } } } }
From source file:org.eclipse.emf.teneo.test.stores.BaseTestDatabaseAdapter.java
public void createDatabase() { try {//from www. jav a 2s. com log.info("Creating database: " + logInfo); final Driver driver = (Driver) Class.forName(dbDriver).newInstance(); Properties info = new Properties(); info.put("user", dbUser); info.put("password", dbPwd); Connection conn = null; Statement stmt = null; try { conn = driver.connect(dbUrl, info); stmt = conn.createStatement(); conn.setAutoCommit(true); stmt.execute("CREATE DATABASE " + dbName + ";"); } finally { if (stmt != null) { stmt.close(); } if (conn != null) { conn.close(); } } } catch (Exception e) { throw new StoreTestException("Exception while creating database: " + logInfo, e); } }
From source file:org.eclipse.emf.teneo.test.stores.BaseTestDatabaseAdapter.java
public void dropDatabase() { try {// w w w. j ava 2s .c o m final Driver driver = (Driver) Class.forName(dbDriver).newInstance(); Properties info = new Properties(); info.put("user", dbUser); info.put("password", dbPwd); Connection conn = null; Statement stmt = null; try { conn = driver.connect(dbUrl, info); final ResultSet rs = conn.getMetaData().getCatalogs(); boolean exists = false; while (rs.next()) { final String existingDb = rs.getString(1); exists |= existingDb.compareToIgnoreCase(dbName) == 0; } rs.close(); if (exists) { stmt = conn.createStatement(); conn.setAutoCommit(true); log.info("Dropping database: " + logInfo); stmt.execute("DROP DATABASE " + dbName + ";"); } else { log.info("Database does not exist, not dropped, " + logInfo); } } finally { if (stmt != null) { stmt.close(); } if (conn != null) { conn.close(); } } } catch (Exception e) { throw new StoreTestException("Exception while dropping database: " + logInfo, e); } }
From source file:org.eclipse.emf.teneo.test.stores.BaseTestDatabaseAdapter.java
/** Gets a database connection using the adapters connection info */ public Connection getConnection() { try {/*www. j av a2 s.co m*/ final Driver driver = (Driver) Class.forName(dbDriver).newInstance(); Properties info = new Properties(); info.put("user", dbUser); info.put("password", dbPwd); return driver.connect(getDbUrl(), info); } catch (Exception e) { throw new StoreTestException("Exception while creating database: " + logInfo, e); } }
From source file:org.eclipse.emf.teneo.test.stores.PgsqlTestDatabaseAdapter.java
/** Drops the database and creates a new one */ @Override/*ww w . j a v a2 s . com*/ public void createDatabase() { try { log.info("Creating database: " + logInfo); final Driver driver = (Driver) Class.forName(dbDriver).newInstance(); Properties info = new Properties(); info.put("user", dbUser); info.put("password", dbPwd); Connection conn = null; Statement stmt = null; try { final String useUrl = dbUrl + "template1"; conn = driver.connect(useUrl, info); if (!databaseExists(conn)) { int tries = 0; while (true) { try { tries++; stmt = conn.createStatement(); stmt.execute("CREATE DATABASE " + dbName + ";"); // conn.commit(); return; } catch (Exception e) { Thread.sleep(500); log.warn("Exception (" + e.getMessage() + ") while creating database (" + getDbName() + "), num of tries: " + tries + ", the create database gives up at: " + NO_TRIES); // log.warn("Waiting for " + WAIT_TIME + // " milli seconds on postgresql to release connections"); // wait(WAIT_TIME); if (tries == NO_TRIES) { throw e; } } finally { if (stmt != null) { stmt.close(); } if (conn != null) { conn.close(); } conn = driver.connect(useUrl, info); } } } } finally { if (stmt != null) { stmt.close(); } if (conn != null) { conn.close(); } } } catch (Exception e) { throw new StoreTestException("Exception while creating database: " + logInfo, e); } }
From source file:org.eclipse.emf.teneo.test.stores.PgsqlTestDatabaseAdapter.java
/** * Drops the database, uses the conn.getMetaData().getCatalogs() method to determine if the * database already exists. This should be supported by the database driver. *//*from www .j a va 2 s . com*/ @Override public void dropDatabase() { try { final Driver driver = (Driver) Class.forName(dbDriver).newInstance(); Properties info = new Properties(); info.put("user", dbUser); info.put("password", dbPwd); Connection conn = null; Statement stmt = null; try { final String useUrl = dbUrl + "template1"; conn = driver.connect(useUrl, info); // check if the database exists stmt = conn.createStatement(); log.info("Dropping database: " + logInfo); ResultSet rs = stmt.executeQuery("SELECT datname FROM pg_database"); boolean exists = false; while (rs.next()) { exists |= dbName.compareTo(rs.getString("datname")) == 0; if (exists) { break; } } rs.close(); stmt.close(); if (exists) { int tries = 0; while (true) { try { tries++; stmt = conn.createStatement(); stmt.execute("DROP DATABASE " + dbName + ";"); // conn.commit(); return; } catch (Exception e) { Thread.sleep(500); log.warn("Exception (" + e.getMessage() + ") while creating database (" + getDbName() + "), num of tries: " + tries + ", the create database gives up at: " + NO_TRIES); // log.warn("Waiting for " + WAIT_TIME + // " milli seconds on postgresql to release connections"); // wait(WAIT_TIME); if (tries == NO_TRIES) { throw e; } } finally { if (stmt != null) { stmt.close(); } if (conn != null) { conn.close(); } conn = driver.connect(useUrl, info); } } } // conn.commit(); return; } finally { if (stmt != null) { stmt.close(); } if (conn != null) { conn.close(); } } } catch (Exception e) { throw new StoreTestException("Exception while dropping database: " + logInfo, e); } }
From source file:org.fastcatsearch.datasource.reader.DBReader.java
private Connection getConnection(JDBCSourceInfo jdbcSourceInfo) throws IRException, SQLException { Connection con = null;/*from ww w .j a v a 2s . c o m*/ logger.debug(">>>>>>>>>>>>>> jdbcSourceInfo > {}", jdbcSourceInfo); if (jdbcSourceInfo.getDriver() != null && jdbcSourceInfo.getDriver().length() > 0) { Object object = DynamicClassLoader.loadObject(jdbcSourceInfo.getDriver()); if (object == null) { throw new IRException("Cannot find sql driver = " + jdbcSourceInfo.getDriver()); } else { Driver driver = (Driver) object; DriverManager.registerDriver(driver); Properties info = new Properties(); info.put("user", jdbcSourceInfo.getUser()); info.put("password", jdbcSourceInfo.getPassword()); info.put("connectTimeout", "300000"); con = driver.connect(jdbcSourceInfo.getUrl(), info); con.setAutoCommit(true); } } else { throw new IRException("JDBC driver is empty!"); } return con; }
From source file:org.hyperic.hq.plugin.mysql_stats.MySqlServerDetector.java
private static final Connection getConnection(String url, String user, String pass,//from w w w . ja va 2s . c om ConfigResponse config) throws SQLException { final String d = MySqlStatsMeasurementPlugin.DEFAULT_DRIVER; try { Driver driver = (Driver) Class.forName(d).newInstance(); final Properties props = new Properties(); pass = (pass == null) ? "" : pass; props.put("user", user); props.put("password", pass); return driver.connect(url, props); } catch (InstantiationException e) { throw new SQLException(e.getMessage()); } catch (IllegalAccessException e) { throw new SQLException(e.getMessage()); } catch (ClassNotFoundException e) { throw new SQLException(e.getMessage()); } }