Oracle


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int staffNo = 0//생성된 행의 키를 받을 변수
String[] keyCol ={"no"}; //키값이 생성되는 컬럼 명
 
            staffPstmt = conn.prepareStatement //쿼리문 세팅
                    ("INSERT INTO STAFF (no,name,sn,graduateday,schoolno,religionno) VALUES"
                            + "(STAFF_SEQ.nextval,?,?,?,?,?)",keyCol);
            staffPstmt.setString(1,staff.getName());
            staffPstmt.setString(2, staff.getSn());
            staffPstmt.setString(3, staff.getGraduateday());
            staffPstmt.setInt(4, staff.getSchool().getNo());
            staffPstmt.setInt(5, staff.getReligion().getNo());
 
            staffRow = staffPstmt.executeUpdate(); //쿼리실행
            rs=staffPstmt.getGeneratedKeys(); //ResultSet 타입으로 결과 받아옴
            if(rs.next()){
                staffNo = rs.getInt(1); 
                //이부분에서 rs.getInt("컬럼명"); 이렇게하면 키값을 불러올수없다.
                //절대로 rs.getInt(1); 이렇게 해야한다.
            }
cs
 


MySql


1
2
3
4
5
6
pstmt = con.prepareStatement("insert into ......", Statement.RETURN_GENERATED_KEYS);
pstmt.executeUpdate();
rs = pstmt.getGeneratedKeys();
if(rs.next()){
    out.println(rs.getInt(1));
}
cs


'JSP&Servlet' 카테고리의 다른 글

jstl 문자열 자르기 조건문  (0) 2017.03.14
커넥션 풀  (0) 2017.02.26
이벤트 리스너(event listener)  (0) 2017.02.26
ServletContext / ServletConfig init-param 차이점  (0) 2017.02.26
필터  (0) 2017.02.26

+ Recent posts