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

The following examples show how to use com.mysql.cj.jdbc.MysqlDataSource#setUser() . 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: MySqlHelper.java    From cassandana with Apache License 2.0 6 votes vote down vote up
public synchronized void tryConnecting() {

		MysqlDataSource dataSource = new MysqlDataSource();
		dataSource.setServerName(this.host);
		dataSource.setPort(port);
		dataSource.setUser(this.dbUsername);
		dataSource.setPassword(this.dbPassword);
		dataSource.setDatabaseName(this.dbName);

		try {
			dataSource.setCharacterEncoding("utf-8");
			connection = dataSource.getConnection();

			setConnected(true);
			onConnected();
			System.out.println("connected to mysql server: " + host + ":" + port);

		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
 
Example 2
Source File: TestUtil.java    From Mycat2 with GNU General Public License v3.0 6 votes vote down vote up
@SneakyThrows
public static Connection getMySQLConnection(int port) {
    String username = "root";
    String password = "123456";

    Properties properties = new Properties();
    properties.put("user", username);
    properties.put("password", password);
    properties.put("useBatchMultiSend", "false");
    properties.put("usePipelineAuth", "false");

    String url = "jdbc:mysql://0.0.0.0:" +
            port +
            "/db1?useServerPrepStmts=false&useCursorFetch=false&serverTimezone=UTC&allowMultiQueries=false&useBatchMultiSend=false&characterEncoding=utf8";

    MysqlDataSource mysqlDataSource = new MysqlDataSource();
    mysqlDataSource.setUrl(url);
    mysqlDataSource.setUser(username);
    mysqlDataSource.setPassword(password);

    return mysqlDataSource.getConnection();
}
 
Example 3
Source File: TestUtil.java    From Mycat2 with GNU General Public License v3.0 6 votes vote down vote up
@SneakyThrows
public static Connection getMySQLConnection() {
    String username = "root";
    String password = "123456";

    Properties properties = new Properties();
    properties.put("user", username);
    properties.put("password", password);
    properties.put("useBatchMultiSend", "false");
    properties.put("usePipelineAuth", "false");

    String url = "jdbc:mysql://0.0.0.0:8066/db1?useServerPrepStmts=false&useCursorFetch=false&serverTimezone=UTC&allowMultiQueries=false&useBatchMultiSend=false&characterEncoding=utf8";

    MysqlDataSource mysqlDataSource = new MysqlDataSource();
    mysqlDataSource.setUrl(url);
    mysqlDataSource.setUser(username);
    mysqlDataSource.setPassword(password);

    return mysqlDataSource.getConnection();
}
 
Example 4
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 5
Source File: CatalogMultitenancyTest.java    From high-performance-java-persistence with Apache License 2.0 6 votes vote down vote up
private void addTenantConnectionProvider(String tenantId) {
    DataSourceProvider dataSourceProvider = database().dataSourceProvider();

    Properties properties = properties();

    MysqlDataSource tenantDataSource = new MysqlDataSource();
    tenantDataSource.setDatabaseName(tenantId);
    tenantDataSource.setUser(dataSourceProvider.username());
    tenantDataSource.setPassword(dataSourceProvider.password());

    properties.put(
            Environment.DATASOURCE,
            dataSourceProxyType().dataSource(tenantDataSource)
    );

    addTenantConnectionProvider(tenantId, tenantDataSource, properties);
}
 
Example 6
Source File: ITTracingStatementInterceptor.java    From brave with Apache License 2.0 6 votes vote down vote up
@Before public void init() throws SQLException {
  StringBuilder url = new StringBuilder("jdbc:mysql://");
  url.append(envOr("MYSQL_HOST", "127.0.0.1"));
  url.append(":").append(envOr("MYSQL_TCP_PORT", 3306));
  String db = envOr("MYSQL_DB", null);
  if (db != null) url.append("/").append(db);
  url.append("?statementInterceptors=").append(TracingStatementInterceptor.class.getName());
  url.append("&zipkinServiceName=").append("myservice");
  url.append("&serverTimezone=").append("UTC");

  MysqlDataSource dataSource = new MysqlDataSource();
  dataSource.setUrl(url.toString());

  dataSource.setUser(System.getenv("MYSQL_USER"));
  assumeTrue("Minimally, the environment variable MYSQL_USER must be set",
    dataSource.getUser() != null);
  dataSource.setPassword(envOr("MYSQL_PASS", ""));
  connection = dataSource.getConnection();
  spans.clear();
}
 
Example 7
Source File: ITTracingQueryInterceptor.java    From brave with Apache License 2.0 6 votes vote down vote up
@Before public void init() throws SQLException {
  StringBuilder url = new StringBuilder("jdbc:mysql://");
  url.append(envOr("MYSQL_HOST", "127.0.0.1"));
  url.append(":").append(envOr("MYSQL_TCP_PORT", 3306));
  String db = envOr("MYSQL_DB", null);
  if (db != null) url.append("/").append(db);
  url.append("?queryInterceptors=").append(TracingQueryInterceptor.class.getName());
  if (exceptionsTraced) {
    url.append("&exceptionInterceptors=").append(TracingExceptionInterceptor.class.getName());
  }
  url.append("&zipkinServiceName=").append("myservice");
  url.append("&serverTimezone=").append("UTC");

  MysqlDataSource dataSource = new MysqlDataSource();
  dataSource.setUrl(url.toString());

  dataSource.setUser(System.getenv("MYSQL_USER"));
  assumeTrue("Minimally, the environment variable MYSQL_USER must be set",
    dataSource.getUser() != null);
  dataSource.setPassword(envOr("MYSQL_PASS", ""));
  connection = dataSource.getConnection();
  spans.clear();
}
 
Example 8
Source File: MySqlDataSourceFactory.java    From nifi-registry with Apache License 2.0 5 votes vote down vote up
@Override
protected DataSource createDataSource() {
    final MySQLContainer container = mysqlContainer();
    final MysqlDataSource dataSource = new MysqlDataSource();
    dataSource.setUrl(container.getJdbcUrl());
    dataSource.setUser(container.getUsername());
    dataSource.setPassword(container.getPassword());
    dataSource.setDatabaseName(container.getDatabaseName());
    return dataSource;
}
 
Example 9
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 10
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 11
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 12
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 13
Source File: MySQLVariableObjectMapWrapper.java    From Mycat2 with GNU General Public License v3.0 4 votes vote down vote up
@SneakyThrows
public static void main(String[] args) {
    String username = "root";
    String password = "123456";

    Properties properties = new Properties();
    properties.put("user", username);
    properties.put("password", password);
    properties.put("useBatchMultiSend", "false");
    properties.put("usePipelineAuth", "false");

    String url = "jdbc:mysql://0.0.0.0:3306/db1?useServerPrepStmts=false&useCursorFetch=true&serverTimezone=UTC&allowMultiQueries=false&useBatchMultiSend=false&characterEncoding=utf8";

    MysqlDataSource mysqlDataSource = new MysqlDataSource();
    mysqlDataSource.setUrl(url);
    mysqlDataSource.setUser(username);
    mysqlDataSource.setPassword(password);

    MySQLVariableObjectMapWrapper mapWrapper = new MySQLVariableObjectMapWrapper();
    try (Connection connection = DriverManager.getConnection(url, properties)) {
        try (Statement statement = connection.createStatement()) {
            for (MySQLVariablesEnum value : MySQLVariablesEnum.values()) {
                String[] columnNames = value.getColumnNames();

                try {
                    for (String columnName : columnNames) {
                        ResultSet resultSet = statement.executeQuery("select " + columnName);
                        while (resultSet.next()) {
                            ResultSetMetaData metaData = resultSet.getMetaData();
                            JDBCType jdbcType = JDBCType.valueOf(metaData.getColumnType(1));
                            Object object = resultSet.getObject(1);
                            Class<?> aClass = null;
                            if (object != null) {
                                aClass = object.getClass();
                            }
                            MySQLVariableObject mySQLVariableObject = new MySQLVariableObject(value, columnName, jdbcType, aClass, object);
                            mapWrapper.map.put(mySQLVariableObject.getColumnName(), mySQLVariableObject);
                            System.out.println(mySQLVariableObject);
                        }
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                    System.err.println("error:" + columnNames);
                }
            }


        }
    }
    String s = JsonUtil.toJson(mapWrapper);
    MySQLVariableObjectMapWrapper from = JsonUtil.from(s, MySQLVariableObjectMapWrapper.class);
    System.out.println(s);
}
 
Example 14
Source File: MetaDataConfig.java    From Mycat2 with GNU General Public License v3.0 4 votes vote down vote up
@SneakyThrows
public static void main(String[] args) {
    String username = "root";
    String password = "123456";

    Properties properties = new Properties();
    properties.put("user", username);
    properties.put("password", password);
    properties.put("useBatchMultiSend", "false");
    properties.put("usePipelineAuth", "false");

    String url = "jdbc:mysql://0.0.0.0:3306/db1?useServerPrepStmts=false&useCursorFetch=true&serverTimezone=UTC&allowMultiQueries=false&useBatchMultiSend=false&characterEncoding=utf8";

    MysqlDataSource mysqlDataSource = new MysqlDataSource();
    mysqlDataSource.setUrl(url);
    mysqlDataSource.setUser(username);
    mysqlDataSource.setPassword(password);

    try (Connection connection = DriverManager.getConnection(url, properties)) {
        try (Statement statement = connection.createStatement()) {
            ResultSet resultSet = statement.executeQuery("SELECT * FROM information_schema.COLUMNS  ");
                ResultSetMetaData metaData = resultSet.getMetaData();
                int columnCount = metaData.getColumnCount();
            resultSet.next();
            for (int i = 1; i <=columnCount ; i++) {
                String columnName = metaData.getColumnName(i);
                JDBCType jdbcType = JDBCType.valueOf(metaData.getColumnType(i));
                Object object = null;
                try {
                    object  = resultSet.getObject(i);
                }catch (Exception e){

                }
                Class<?> aClass = null;
                if (object != null) {
                    aClass = object.getClass();
                }
                System.out.println(""+ Optional.ofNullable(aClass).map(j->j.getSimpleName()).orElse(jdbcType.getName())+" "+columnName+" ;");
            }
        }
    }
}
 
Example 15
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();
}