diff --git a/build.gradle.kts b/build.gradle.kts index 09c1542..ce01f3a 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -20,7 +20,13 @@ dependencies { implementation("org.springframework.boot:spring-boot-starter-web") implementation("org.jetbrains.kotlin:kotlin-reflect") implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8") + implementation("junit:junit:4.12") + implementation("org.springframework.boot:spring-boot-starter-jdbc") + + implementation("com.h2database:h2") + providedRuntime("org.springframework.boot:spring-boot-starter-tomcat") + testImplementation("org.springframework.boot:spring-boot-starter-test") } diff --git a/src/main/kotlin/com/flabedu/blackpostoffice/BlackPostofficeApplication.kt b/src/main/kotlin/com/flabedu/blackpostoffice/BlackPostofficeApplication.kt index 64932ce..e82273a 100644 --- a/src/main/kotlin/com/flabedu/blackpostoffice/BlackPostofficeApplication.kt +++ b/src/main/kotlin/com/flabedu/blackpostoffice/BlackPostofficeApplication.kt @@ -1,5 +1,7 @@ package com.flabedu.blackpostoffice +import com.flabedu.blackpostoffice.dao.UserDao +import com.flabedu.blackpostoffice.domain.User import org.springframework.boot.autoconfigure.SpringBootApplication import org.springframework.boot.runApplication @@ -8,4 +10,13 @@ class BlackPostofficeApplication fun main(args: Array) { runApplication(*args) + + val userDao = UserDao() + val user = User("dudrnxps", "박영환", "1234") + + userDao.add(user) + + val result = userDao.get("dudrnxps") + + print(result.toString()) } diff --git a/src/main/kotlin/com/flabedu/blackpostoffice/dao/UserDao.kt b/src/main/kotlin/com/flabedu/blackpostoffice/dao/UserDao.kt new file mode 100644 index 0000000..1c16216 --- /dev/null +++ b/src/main/kotlin/com/flabedu/blackpostoffice/dao/UserDao.kt @@ -0,0 +1,50 @@ +package com.flabedu.blackpostoffice.dao + +import com.flabedu.blackpostoffice.domain.User +import java.sql.* +import kotlin.jvm.Throws + +class UserDao { + + @Throws + fun add(user: User): Unit { + Class.forName("org.h2.Driver") + + val conn = DriverManager.getConnection("jdbc:h2:mem:test", "SA", "") + var ps: PreparedStatement = conn.prepareStatement("CREATE TABLE USERS(id varchar(10) primary key, name varchar(20) not null, password varchar(10) not null)") + ps.executeUpdate() + + ps = conn.prepareStatement("INSERT INTO USERS(id, name, password) VALUES(?, ?, ?)") + ps.setString(1, user.id) + ps.setString(2, user.name) + ps.setString(3, user.password) + + ps.executeUpdate() + + ps.close() + conn.close() + } + + @Throws + fun get(id: String): User { + Class.forName("org.h2.Driver") + + val c: Connection = DriverManager.getConnection("jdbc:h2:mem:test", "SA", "") + val ps: PreparedStatement = c.prepareStatement("SELECT * FROM USERS WHERE id = ?") + ps.setString(1, id) + + val rs: ResultSet = ps.executeQuery() + rs.next() + + val user = User() + user.id = rs.getString("id") + user.name = rs.getString("name") + user.password = rs.getString("password") + + rs.close() + ps.close() + c.close() + + return user + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/flabedu/blackpostoffice/domain/User.kt b/src/main/kotlin/com/flabedu/blackpostoffice/domain/User.kt new file mode 100644 index 0000000..24e074c --- /dev/null +++ b/src/main/kotlin/com/flabedu/blackpostoffice/domain/User.kt @@ -0,0 +1,7 @@ +package com.flabedu.blackpostoffice.domain + +data class User ( + var id: String = "", + var name: String = "", + var password: String = "" +) \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 8b13789..fe1e595 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1 +1,6 @@ +spring.datasource.url=jdbc:h2:mem:test +spring.datasource.username=sa +spring.datasource.password= +spring.datasource.driver-class-name=org.h2.Driver +spring.h2.console.enabled=true \ No newline at end of file diff --git a/src/test/kotlin/com/flabedu/blackpostoffice/H2Test.kt b/src/test/kotlin/com/flabedu/blackpostoffice/H2Test.kt new file mode 100644 index 0000000..5908cb7 --- /dev/null +++ b/src/test/kotlin/com/flabedu/blackpostoffice/H2Test.kt @@ -0,0 +1,23 @@ +package com.flabedu.blackpostoffice + +import junit.framework.Assert.assertEquals +import org.junit.jupiter.api.Test +import org.junit.runner.RunWith +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner +import java.sql.* + +@RunWith(SpringJUnit4ClassRunner::class) +class H2Test { + + @Test + fun `H2Database 연결 확인`() { + Class.forName("org.h2.Driver") + + val c: Connection = DriverManager.getConnection("jdbc:h2:mem:test", "SA", "") + assertEquals(c.isClosed, false) + + c.close() + + assertEquals(c.isClosed, true) + } +} \ No newline at end of file diff --git a/target/classes/META-INF/mainModule.kotlin_module b/target/classes/META-INF/mainModule.kotlin_module new file mode 100644 index 0000000..842528e Binary files /dev/null and b/target/classes/META-INF/mainModule.kotlin_module differ diff --git a/target/classes/MainKt.class b/target/classes/MainKt.class new file mode 100644 index 0000000..11baf84 Binary files /dev/null and b/target/classes/MainKt.class differ