Java Code Examples for com.mysql.cj.jdbc.MysqlDataSource#setURL()

The following examples show how to use com.mysql.cj.jdbc.MysqlDataSource#setURL() . 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: MySQLDatabase.java    From modernmt with Apache License 2.0 6 votes vote down vote up
public MySQLDatabase(String host, int port, String name, String user, String password) {
    super(null);
    this.name = name;

    String params = "useUnicode=true"
            + "&useJDBCCompliantTimezoneShift=true"
            + "&useLegacyDatetimeCode=false"
            + "&serverTimezone=UTC";

    MysqlDataSource mysqlDS = new MysqlDataSource();
    mysqlDS.setURL("jdbc:mysql://" + host + ":" + port + "/" + name + "?" + params);
    mysqlDS.setDatabaseName(name);
    mysqlDS.setUser(user);
    mysqlDS.setPassword(password);
    this.dataSource = mysqlDS;
}
 
Example 2
Source File: DataSourceFactory.java    From extract with MIT License 5 votes vote down vote up
private DataSource createSingle()  {
	final MysqlDataSource dataSource = new MysqlDataSource();

	dataSource.setURL(createURL());
	dataSource.setUser(null == user ? "extract" : user);
	dataSource.setPassword(password);

	dataSource.initializeProperties(createProperties());

	return dataSource;
}
 
Example 3
Source File: Installer.java    From CodeDefenders with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Sets up the {@link InitialContext} for a given configuration.
 * <p>
 * Also adds the database {@link DataSource} to the initial context.
 *
 * @param configurations the configuration used to set the initial context.
 * @throws NamingException when setting the initial context fails.
 */
private static void setupInitialContext(Properties configurations) throws NamingException {
    System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory");
    System.setProperty(Context.URL_PKG_PREFIXES, "org.apache.naming");

    InitialContext ic = new InitialContext();

    ic.createSubcontext("java:");
    ic.createSubcontext("java:comp");
    ic.createSubcontext("java:comp/env");
    ic.createSubcontext("java:comp/env/codedefenders");

    // Alessio: Maybe there a better way to do it...
    for (String propName : configurations.stringPropertyNames()) {
        logger.info("Setting java:comp/env/codedefenders/" + propName + " = " + configurations.get(propName));
        ic.bind("java:comp/env/codedefenders/" + propName, configurations.get(propName));
    }

    ic.createSubcontext("java:comp/env/jdbc");

    MysqlDataSource dataSource = new MysqlDataSource();
    dataSource.setURL(configurations.getProperty("db.url"));
    dataSource.setUser(configurations.getProperty("db.username"));
    dataSource.setPassword(configurations.getProperty("db.password"));

    ic.bind("java:comp/env/jdbc/codedefenders", dataSource);

    // Maybe there's a way to provide the beans definition directly here...
    Weld weld = new Weld();
    WeldContainer container = weld.initialize();
    // Manually load the dependencies and set the backend in the context, this is only because I cannot inject BeanManager
    BackendExecutorService backend = container.instance().select(BackendExecutorService.class).get();
    ic.bind("java:comp/env/codedefenders/backend", backend);
    // Weld will be automatically closed at system.exit
}
 
Example 4
Source File: JpaEntityManagerFactory.java    From tutorials with MIT License 5 votes vote down vote up
protected DataSource getMysqlDataSource() {
MysqlDataSource mysqlDataSource = new MysqlDataSource();
       mysqlDataSource.setURL(DB_URL);
mysqlDataSource.setUser(DB_USER_NAME);
       mysqlDataSource.setPassword(DB_PASSWORD);
return mysqlDataSource;
   }
 
Example 5
Source File: JpaEntityManagerFactory.java    From tutorials with MIT License 5 votes vote down vote up
protected DataSource getMysqlDataSource() {
MysqlDataSource mysqlDataSource = new MysqlDataSource();
       mysqlDataSource.setURL(DB_URL);
mysqlDataSource.setUser(DB_USER_NAME);
       mysqlDataSource.setPassword(DB_PASSWORD);
return mysqlDataSource;
   }
 
Example 6
Source File: ApiConnectionImpl.java    From JspMyAdmin2 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * To open connection by providing all of the properties.
 *
 * @param host String
 * @param port String
 * @param user String
 * @param pass String
 * @return Connection
 * @throws SQLException e
 */
private Connection _openConnection(String host, String port, String user, String pass) throws SQLException {

    MysqlDataSource dataSource = new MysqlDataSource();
    dataSource.setUseSSL(false);
    dataSource.setURL(_URL + host + Constants.SYMBOL_COLON + port + Constants.SYMBOL_BACK_SLASH);
    dataSource.setUser(user);
    dataSource.setPassword(pass);
    return dataSource.getConnection();
}