티스토리 뷰
Spring/Toby의 스프링3 따라하기
Toby의 Spring3 따라하기-제3편 connectionMaker interface 이용해서 분리하기
KyeongRok Kim 2015. 9. 8. 16:56Toby의 Spring3 따라하기-제3편 connectionMaker interface 이용해서 분리하기
UserDao.java
import java.sql.*;
/**
* Created by krkim on 2015-09-08.
*/
public class UserDao {
ConnectionMaker connectionMaker;
public UserDao(ConnectionMaker connectionMaker) {
this.connectionMaker = connectionMaker;
}
public void add(User user) throws SQLException {
Connection c = null;
try {
c = connectionMaker.getConnection();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
PreparedStatement ps = c.prepareStatement("insert into users(id, password, name) values(?,?,?)");
ps.setString(1, user.getId());
ps.setString(2, user.getPassword());
ps.setString(3, user.getName());
ps.executeUpdate();
ps.close();
c.close();
}
public User get(String id) throws SQLException {
Connection c = null;
try {
c = connectionMaker.getConnection();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
PreparedStatement ps = c.prepareStatement("select * from users where id=?");
ps.setString(1, id);
ResultSet rs = ps.executeQuery();
rs.next();
User user = new User(rs.getString("id"), rs.getString("password"), rs.getString("name") );
rs.close();
ps.close();
c.close();
return user;
}
}
ConnectionMaker.java(interface;인터페이스)
import java.sql.Connection;
import java.sql.SQLException;
/**
* Created by krkim on 2015-09-08.
*/
public interface ConnectionMaker {
public Connection getConnection() throws ClassNotFoundException, SQLException;
}
KConnectionMaker.java(인터페이스 구현체)
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
* Created by krkim on 2015-09-08.
*/
public class KConnectionMaker implements ConnectionMaker{
@Override
public Connection getConnection() throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
Connection c = DriverManager.getConnection(
"jdbc:mysql://localhost/krkdb",
"root",
"111111"
);
return c;
}
}
end.
728x90
'Spring > Toby의 스프링3 따라하기' 카테고리의 다른 글
토비의 스프링 5장 'Service 추상화'부터 시작하기 위한 준비 (0) | 2016.01.23 |
---|---|
Toby의 Spring3 따라하기-제4편 AnnotationContext를 이용한 Spring 사용 (1) | 2015.09.28 |
Toby의 Spring3 따라하기-제2편 UserDao.class 만들기 (0) | 2015.09.08 |
Toby의 Spring3 따라하기-제2편 Spring MVC Project 생성하기 (2) | 2015.02.05 |
토비의 스프링3 리팩토링하기 정리 - 제3장 템플릿과 콜백 (0) | 2015.01.07 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 도커티슈박스
- 개발자
- docker container tissue box
- Sh
- shellscript
- 도커티슈케이스
- 도커각티슈박스
- docker container
- docker container whale
- vim
- 이직
- 싱가폴
- 도커컨테이너
- docker container case
- 도커각티슈케이스
- Linux
- 2017 티스토리 결산
- docker container tissue
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
글 보관함