JAVA구현 코드 보단 python 코드로 구현한 것을 보자

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
         try:
     
              conn = db_connPool(cfg)     #cfg는 configuration을 의미
              __LOG__.Trace("DB connected")
     
              #cursor = None
     
              #transaction
     
              conn.autocommit = False    #autocommit을 꺼준다.
              cursor = conn.cursor()
              strSql = cfg.get('DB','sql_insert') % params
              cursor.execute(strSql)
              __LOG__.Trace("Inserted!")
     
          except Exception as e:
              conn.rollback()            #Exception발생 시 rollback을 통해 되돌아간다.
              __LOG__Trace("insert Error : %s" % e)
     
          finally:
              conn.commit()                #정상적으로 insert가 이루어지면 commit하여 DB에 반영한다.
              if cursor:
                  cursor.close()
              if conn:
                  conn.close()
 
cs


오라클에서 insert문을 사용하는 메소드에서 int변수를 사용하지 않고 Integer변수를 사용하는 이유

★ 오라클에서 insert문을 사용하는 메소드에서 int변수를 사용하지 않고 Integer변수를 사용하는 이유
        --> insert문의 결과가 없거나 실패할 경우 null값을 리턴받기 위해. int변수를 사용하면 실패하는 경우 리턴되는 값이 없기 때문.



numbering된 sequence 값을 바로 받아 오는 법

Oracle에서 사용

preparedStatement의 생성자중 (sql, String columnName)사용
PreparedStatement ps = conn .prepareStatement(sql, new String[]{"bno" });
ResultSet rs = ps.getGeneratedKeys();
           if(rs .next()){
               bno = rs.getInt(1);
          }

자동 생성된 키를 return해라

Mysql(MsSQL Server)에서 사용
PreparedStatement ps = conn.prepareStatement(sql,PreparedStatement.RETURN_GENERATED_KEYS);


+ Recent posts