jdbc : connection
This commit is contained in:
@@ -0,0 +1,7 @@
|
|||||||
|
package com.example.jdbc.connection;
|
||||||
|
|
||||||
|
public abstract class ConnectionConst {
|
||||||
|
public static final String URL = "jdbc:h2:tcp://localhost/~/test";
|
||||||
|
public static final String USERNAME = "sa";
|
||||||
|
public static final String PASSWORD = "";
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package com.example.jdbc.connection;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.DriverManager;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
import static com.example.jdbc.connection.ConnectionConst.*;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
public class DBConnectionUtil {
|
||||||
|
|
||||||
|
public static Connection getConnection() {
|
||||||
|
try {
|
||||||
|
Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
|
||||||
|
log.info("get connection={}, class={}", connection, connection.getClass());
|
||||||
|
return connection;
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new IllegalStateException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
package com.example.jdbc.connection;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
class DBConnectionUtilTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void connection() {
|
||||||
|
Connection connection = DBConnectionUtil.getConnection();
|
||||||
|
assertThat(connection).isNotNull();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user