[BAEL-4497] jdbc url

This commit is contained in:
Kai Yuan 2020-12-13 17:22:09 +01:00
parent 282db45191
commit 7afdb4cba7
2 changed files with 120 additions and 1 deletions

View File

@ -20,10 +20,43 @@
<artifactId>h2</artifactId>
<version>${h2.version}</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>${postgresql.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.driver.version}</version>
</dependency>
<!--
==================================================================
This jar is not in maven central repo due to some license issues.
We need to manually download and install to maven repository.
==================================================================
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc14</artifactId>
<version>${oracle.driver.version}</version>
</dependency>
-->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>${mssql.driver.version}</version>
</dependency>
</dependencies>
<properties>
<h2.version>1.4.200</h2.version>
<postgresql.version>42.2.5.jre7</postgresql.version>
<mssql.driver.version>8.4.1.jre11</mssql.driver.version>
<oracle.driver.version>10.2.0.4.0</oracle.driver.version>
<mysql.driver.version>8.0.22</mysql.driver.version>
</properties>
</project>
</project>

View File

@ -0,0 +1,86 @@
package com.baeldung.jdbcurlformat;
import org.junit.Test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import static org.junit.Assert.assertNotNull;
public class JdbcUrlFormatLiveTest {
@Test
public void givenOracleSID_thenCreateConnectionObject() {
String oracleJdbcUrl = "jdbc:oracle:thin:@myoracle.db.server:1521:my_sid";
String username = "dbUser";
String password = "1234567";
try (Connection conn = DriverManager.getConnection(oracleJdbcUrl, username, password)) {
assertNotNull(conn);
} catch (SQLException e) {
System.err.format("SQL State: %s\n%s", e.getSQLState(), e.getMessage());
}
}
@Test
public void givenOracleServiceName_thenCreateConnectionObject() {
String oracleJdbcUrl = "jdbc:oracle:thin:@//myoracle.db.server:1521/my_servicename";
String username = "dbUser";
String password = "1234567";
try (Connection conn = DriverManager.getConnection(oracleJdbcUrl, username, password)) {
assertNotNull(conn);
} catch (SQLException e) {
System.err.format("SQL State: %s\n%s", e.getSQLState(), e.getMessage());
}
}
@Test
public void givenOracleTnsnames_thenCreateConnectionObject() {
String oracleJdbcUrl = "jdbc:oracle:thin:@" +
"(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)" +
"(HOST=myoracle.db.server)(PORT=1521))" +
"(CONNECT_DATA=(SERVICE_NAME=my_servicename)))";
String username = "dbUser";
String password = "1234567";
try (Connection conn = DriverManager.getConnection(oracleJdbcUrl, username, password)) {
assertNotNull(conn);
} catch (SQLException e) {
System.err.format("SQL State: %s\n%s", e.getSQLState(), e.getMessage());
}
}
@Test
public void givenMysqlDb_thenCreateConnectionObject() {
String jdbcUrl = "jdbc:mysql://mysql.db.server:3306/my_database?useSSL=false&serverTimezone=UTC";
String username = "dbUser";
String password = "1234567";
try (Connection conn = DriverManager.getConnection(jdbcUrl, username, password)) {
assertNotNull(conn);
} catch (SQLException e) {
System.err.format("SQL State: %s\n%s", e.getSQLState(), e.getMessage());
}
}
@Test
public void givenMssqlDb_thenCreateConnectionObject() {
String jdbcUrl = "jdbc:sqlserver://mssql.db.server\\mssql_instance;databaseName=my_database";
String username = "dbUser";
String password = "1234567";
try (Connection conn = DriverManager.getConnection(jdbcUrl, username, password)) {
assertNotNull(conn);
} catch (SQLException e) {
System.err.format("SQL State: %s\n%s", e.getSQLState(), e.getMessage());
}
}
@Test
public void givenPostgreSqlDb_thenCreateConnectionObject() {
String jdbcUrl = "jdbc:postgresql://postgresql.db.server:5430/my_database?ssl=true&loglevel=2";
String username = "dbUser";
String password = "1234567";
try (Connection conn = DriverManager.getConnection(jdbcUrl, username, password)) {
assertNotNull(conn);
} catch (SQLException e) {
System.err.format("SQL State: %s\n%s", e.getSQLState(), e.getMessage());
}
}
}