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

The following examples show how to use com.mysql.cj.jdbc.MysqlDataSource#getConnection() . 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: 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 5
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 6
Source File: DataSourceRegressionTest.java    From FoxTelem with GNU General Public License v3.0 5 votes vote down vote up
public void testBug42267() throws Exception {
    MysqlDataSource ds = new MysqlDataSource();
    ds.setUrl(dbUrl);
    Connection c = ds.getConnection();
    String query = "select 1,2,345";
    PreparedStatement ps = c.prepareStatement(query);
    String psString = ps.toString();
    assertTrue("String representation of wrapped ps should contain query string", psString.endsWith(": " + query));
    ps.close();
    ps.toString();
    c.close();
}
 
Example 7
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();
}