Skip to content
This repository has been archived by the owner on Aug 14, 2022. It is now read-only.

Commit

Permalink
#11, prototype 코드 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
parkyounghwan committed May 30, 2021
1 parent 31473a2 commit bde5487
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 0 deletions.
6 changes: 6 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -8,4 +10,13 @@ class BlackPostofficeApplication

fun main(args: Array<String>) {
runApplication<BlackPostofficeApplication>(*args)

val userDao = UserDao()
val user = User("dudrnxps", "박영환", "1234")

userDao.add(user)

val result = userDao.get("dudrnxps")

print(result.toString())
}
50 changes: 50 additions & 0 deletions src/main/kotlin/com/flabedu/blackpostoffice/dao/UserDao.kt
Original file line number Diff line number Diff line change
@@ -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
}
}
7 changes: 7 additions & 0 deletions src/main/kotlin/com/flabedu/blackpostoffice/domain/User.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.flabedu.blackpostoffice.domain

data class User (
var id: String = "",
var name: String = "",
var password: String = ""
)
5 changes: 5 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -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
23 changes: 23 additions & 0 deletions src/test/kotlin/com/flabedu/blackpostoffice/H2Test.kt
Original file line number Diff line number Diff line change
@@ -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)
}
}
Binary file added target/classes/META-INF/mainModule.kotlin_module
Binary file not shown.
Binary file added target/classes/MainKt.class
Binary file not shown.

0 comments on commit bde5487

Please sign in to comment.