package hello.jbdc.connection
class ConnectionConst {
companion object{
val URL = "jdbc:h2:tcp://localhost/~/test"
val USERNAME = "sa"
val PASSWORD = ""
}
}
package hello.jbdc.connection
import hello.jbdc.Log
import java.sql.*
class DBConnectionUtil(
) {
companion object: Log {
fun getConnection(): Connection {
try {
val connection =
DriverManager.getConnection(ConnectionConst.URL, ConnectionConst.USERNAME, ConnectionConst.PASSWORD)
logger.info("get connection={}, class={}", connection, connection.javaClass)
return connection
} catch (e: SQLException) {
throw IllegalStateException(e)
}
}
}
}
*companion object?
https://lannstark.tistory.com/141
package hello.jbdc.connection
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
class DBConnectionUtilTest {
@Test
fun connection(){
val connection = DBConnectionUtil.getConnection()
assertThat(connection).isNotNull();
}
}
DBConnectionUtil - get connection=conn0: url=jdbc:h2:tcp://localhost/~/test
user=SA, class=class org.h2.jdbc.JdbcConnection
// H2 데이터베이스 드라이버 라이브러리 (Dependency)
runtimeOnly 'com.h2database:h2' //h2-x.x.xxx.jar
package hello.jbdc.domain
data class Member(
var memberId: String,
var money: Int
)
package hello.jbdc.repository
import hello.jbdc.Log
import hello.jbdc.connection.DBConnectionUtil
import hello.jbdc.domain.Member
import java.sql.*
class MemberRepositoryV0(
):Log {
fun save(member: Member): Member{
val sql: String = "insert into member(member_id, money) values (?, ?)"
lateinit var con: Connection
lateinit var pstmt: PreparedStatement
try{
con = getConnection()
pstmt = con.prepareStatement(sql)
pstmt.setString(1, member.memberId)
pstmt.setInt(2, member.money)
pstmt.executeUpdate()
return member
} catch(e: SQLException){
logger.error("db error", e)
e.printStackTrace()
throw e
} finally {
close(con, pstmt, null);
}
}
private fun getConnection(): Connection {
return DBConnectionUtil.getConnection()
}
private fun close(con: Connection?, stmt: Statement?, rs: ResultSet?) {
if (rs != null) {
try {
rs.close()
} catch (e: SQLException) {
logger.info("error", e)
}
}
if (stmt != null) {
try {
stmt.close()
} catch (e: SQLException) {
logger.info("error", e)
}
}
if (con != null) {
try {
con.close()
} catch (e: SQLException) {
logger.info("error", e)
}
}
}
}
fun findById(memberId: String): Member{
val sql = "select * from member where member_id = ?"
lateinit var con: Connection
lateinit var pstmt: PreparedStatement
lateinit var rs: ResultSet
try{
con = getConnection()
pstmt = con.prepareStatement(sql)
pstmt.setString(1, memberId)
rs = pstmt.executeQuery()
if (rs.next()){
val member = Member(
memberId = rs.getString("member_id"),
money = rs.getInt("money")
)
return member
}else{
throw NoSuchElementException("member not found memberId=" + memberId)
}
} catch (e: SQLException){
logger.error("db error", e)
throw e
} finally {
close(con, pstmt, rs);
}
}
fun update(memberId: String, money: Int){
val sql = "update member set money=? where member_id=?"
lateinit var con: Connection
lateinit var pstmt: PreparedStatement
try {
con = getConnection();
pstmt = con.prepareStatement(sql);
pstmt.setInt(1, money);
pstmt.setString(2, memberId);
val resultSize = pstmt.executeUpdate()
logger.info("resultSize={}", resultSize)
} catch (e: SQLException){
logger.error("db error", e)
throw e
} finally {
close(con, pstmt, null)
}
}
fun delete(memberId: String){
val sql = "delete from member where member_id=?"
lateinit var con: Connection
lateinit var pstmt: PreparedStatement
try {
con = getConnection();
pstmt = con.prepareStatement(sql);
pstmt.setString(1, memberId);
pstmt.executeUpdate()
} catch (e: SQLException){
logger.error("db error", e)
throw e
} finally {
close(con, pstmt, null)
}
}
package hello.jbdc.repository
import hello.jbdc.Log
import hello.jbdc.domain.Member
import org.assertj.core.api.Assertions.*
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test
internal class MemberRepositoryV0Test: Log{
private val memberRepositoryV0 = MemberRepositoryV0()
@Test
fun crud(){
// save
val member = Member("memberV0", 10000);
memberRepositoryV0.save(member)
// findById
val findMember = memberRepositoryV0.findById(member.memberId)
logger.info("findMember = {}", findMember)
assertThat(findMember).isEqualTo(member)
// update : money (10000 -> 20000)
memberRepositoryV0.update(member.memberId, 20000)
val updateMember = memberRepositoryV0.findById(member.memberId)
assertThat(updateMember.money).isEqualTo(20000)
// delete
memberRepositoryV0.delete(member.memberId)
assertThatThrownBy { memberRepositoryV0.findById(member.memberId) }
.isInstanceOf(NoSuchElementException::class.java)
}
}