This repository has been archived by the owner on Aug 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
31473a2
commit bde5487
Showing
8 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/main/kotlin/com/flabedu/blackpostoffice/dao/UserDao.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = "" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
Binary file not shown.