섹션 5. 연결
23강 JDBC
23-1 기본 SQL
테이블 생성 및 삭제
<!--(member 테이블 생성)-->
CREATE TABLE member (
memId VARCHAR2(10) CONSTRAINT memId_pk PRIMARY KEY,
memPw VARCHAR2(10),
memMail VARCHAR2(15),
memPurcNum NUMBER(3) DEFAULT 0 CONSTRAINT memPurNum_ck CHECK (memPurcNum < 3)
);
<!--(member 테이블에 테스트 차원에서 ‘b’계정을 삽입한다.)-->
INSERT INTO member (memId, memPw, memMail)
values ('b', 'bb', 'bbb@gmail.com');
<!--(member 테이블에서 ‘memId’가 ‘b’인 회원을 삭제한다.)-->
DELETE FROM member WHERE memId = 'b';
<!--(member 테이블의 모든 회원정보를 출력한다.)-->
SELECT * FROM member;
<!--(member 테이블을 삭제한다.)-->
DROP TABLE member;
23-2 JDBC
드라이버 로딩 ➡ DB 연결 ➡ SQL 작성 및 전송 ➡ 자원해제
@Repository
public class MemberDao implements IMemberDao {
private String driver = "oracle.jdbc.driver.OracleDriver";
private String url = "jdbc:oracle:thin:@localhost:1521:xe";
private String userid = "scott";
private String userpw = "tiger";
private Connection conn = null;
private PreparedStatement pstmt = null;
private ResultSet rs = null;
@Override
public int memberInsert(Member member) {
int result = 0;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, userid, userpw);
String sql = "INSERT INTO member (memId, memPw, memMail) values (?,?,?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, member.getMemId());
pstmt.setString(2, member.getMemPw());
pstmt.setString(3, member.getMemMail());
result = pstmt.executeUpdate();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if(pstmt != null) pstmt.close();
if(conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return result;
}
...
}