Java/Spring,Servlet
H2 데이터베이스 ] 실행 시 자동 켜기 설정
zenna
2022. 8. 22. 17:38
728x90
h2database 사이트의 tutorial에 복사 가능한 코드 있음
Tutorial
Tutorial Starting and Using the H2 Console Special H2 Console Syntax Settings of the H2 Console Connecting to a Database using JDBC Creating New Databases Using the Server Using Hibernate Using TopLink and Glassfish Using EclipseLink Using Apache Active
www.h2database.com
- Starting the TCP Server within an Applcation 부분 코드 확인
import org.h2.tools.Server;
...
// start the TCP Server
Server server = Server.createTcpServer(args).start();
...
// stop the TCP Server
server.stop();
- 위 코드를 Connection받아오는 .java에 붙여넣기
package org.comstudy21.myweb.dbcp;
...
import org.h2.tools.Server;
public class JdbcUtil {
//================h2실행 여부에 따라 자동 실행===========================
private static Server server = null;
static {
try {
if (server == null) {
// start the TCP Server
server = Server.createTcpServer(null).start();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
//================================================================
연결 확인하기
junit에서
package org.comstudy21.myapp.dbcp;
import static org.junit.jupiter.api.Assertions.*;
import java.sql.Connection;
import java.util.function.Supplier;
import org.comstudy21.myweb.dbcp.JdbcUtil;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
class JdbcUtilTest {
Connection conn = null;
@Test
@DisplayName("connection 객체 생성 Test")
void testGetConnection() {
assertNotNull(conn, "conn은 null이 아니어야 합니다!");
}
}
작성 후 실행하면 에러 없이 연결!
728x90