implementation 'org.springframework.boot:spring-boot-starter-jdbc'
resources/application.properties
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/spring?useSSL=false&useUnicode=true&serverTimezone=Asia/Seoul
spring.datasource.username=root
spring.datasource.password=12345678
private final DataSource dataSource;
public JdbcMemberRepository(DataSource dataSource) {
this.dataSource = dataSource;
}
public Member save(Member member) {
String sql = "insert into member(name) values(?)";
Connection conn = dataSource.getConnection();
// DB Connection 을 가지고 온다.
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, member.getName());
pstmt.executeUpdate();
INSERT INTO member VALUES (?, ?);
PreparedStatement pstmt = connection.prepareStatement(SQL);
pstmt.setString(1, "Lee");
pstmt.setString(2, "1234");
?
에 들어갈 값 지정executeXXX()
executeUpdate()
int r = pstmt.executeUpdate();
executeQuery()
int r = pstmt.excuteQuery();
ResultSet rs = pstmt.executeQuery();
if(rs.next()) {
Member member = new Member(
rs.getString("email"),
rs.getString("password"),
rs.getString("name")
);