forked from slipp/jwp-basic
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
7 changed files
with
172 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package next.dao; | ||
|
||
import java.sql.Connection; | ||
import java.sql.PreparedStatement; | ||
import java.sql.SQLException; | ||
|
||
import core.jdbc.ConnectionManager; | ||
import next.model.User; | ||
|
||
public abstract class InsertJdbcTemplate { | ||
void insert(User user) throws SQLException { | ||
Connection con = null; | ||
PreparedStatement pstmt = null; | ||
try { | ||
con = ConnectionManager.getConnection(); | ||
pstmt = con.prepareStatement(createQueryForInsert()); | ||
setValuesForInsert(user, pstmt); | ||
pstmt.executeUpdate(); | ||
} finally { | ||
if (pstmt != null) { | ||
pstmt.close(); | ||
} | ||
|
||
if (con != null) { | ||
con.close(); | ||
} | ||
} | ||
} | ||
|
||
abstract void setValuesForInsert(User user, PreparedStatement pstmt) throws SQLException; | ||
abstract String createQueryForInsert(); | ||
} |
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,31 @@ | ||
package next.dao; | ||
|
||
import java.sql.Connection; | ||
import java.sql.PreparedStatement; | ||
import java.sql.SQLException; | ||
|
||
import core.jdbc.ConnectionManager; | ||
import next.model.User; | ||
|
||
public abstract class JdbcTemplate { | ||
void update(String query) throws SQLException { | ||
Connection con = null; | ||
PreparedStatement pstmt = null; | ||
try { | ||
con = ConnectionManager.getConnection(); | ||
pstmt = con.prepareStatement(query); | ||
setValues(pstmt); | ||
pstmt.executeUpdate(); | ||
} finally { | ||
if (pstmt != null) { | ||
pstmt.close(); | ||
} | ||
|
||
if (con != null) { | ||
con.close(); | ||
} | ||
} | ||
} | ||
|
||
abstract void setValues(PreparedStatement pstmt) throws SQLException; | ||
} |
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,32 @@ | ||
package next.dao; | ||
|
||
import java.sql.Connection; | ||
import java.sql.PreparedStatement; | ||
import java.sql.SQLException; | ||
|
||
import core.jdbc.ConnectionManager; | ||
import next.model.User; | ||
|
||
public abstract class UpdateJdbcTemplate { | ||
static void update(User user, UserDao userDao) throws SQLException{ | ||
Connection con = null; | ||
PreparedStatement pstmt = null; | ||
try { | ||
con = ConnectionManager.getConnection(); | ||
pstmt = con.prepareStatement(userDao.createQueryForUpdate()); | ||
userDao.setValuesForUpdate(user, pstmt); | ||
pstmt.executeUpdate(); | ||
} finally { | ||
if (pstmt != null) { | ||
pstmt.close(); | ||
} | ||
|
||
if (con != null) { | ||
con.close(); | ||
} | ||
} | ||
} | ||
|
||
abstract void setValuesForUpdate(User user, PreparedStatement pstmt); | ||
abstract String createQueryUpdate(); | ||
} |
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
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,6 +1,7 @@ | ||
package next.dao; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotEquals; | ||
|
||
import java.util.List; | ||
|
||
|
@@ -28,11 +29,15 @@ public void crud() throws Exception { | |
userDao.insert(expected); | ||
User actual = userDao.findByUserId(expected.getUserId()); | ||
assertEquals(expected, actual); | ||
|
||
expected.update(new User("userId", "password2", "name2", "[email protected]")); | ||
userDao.update(expected); | ||
actual = userDao.findByUserId(expected.getUserId()); | ||
assertEquals(expected, actual); | ||
//userDao.update(new User("changePW", "changeNM", "[email protected]", "userId")); | ||
//actual = userDao.findByUserId(expected.getUserId()); | ||
//assertEquals(expected, actual); | ||
//assertNotEquals(expected, actual); | ||
|
||
// expected.update(new User("userId", "password2", "name2", "[email protected]")); | ||
// userDao.update(expected); | ||
// actual = userDao.findByUserId(expected.getUserId()); | ||
// assertEquals(expected, actual); | ||
} | ||
|
||
@Test | ||
|