SimpleJdbcInsert 문제

· β˜• 2 min read · πŸ‘€... views

이전 jdbcTemplate λ₯Ό μ„€λͺ…ν•˜λ©΄μ„œ simpleJdbcInsert 에 λŒ€ν•œ μΉ­μ°¬(?)을 ν–ˆμ—ˆλ‹€.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
@Override
public Long saveLoginUI(final LoginVO loginVO) {
    KeyHolder keyHolder = new GeneratedKeyHolder();
    jdbcTemplate.update(new PreparedStatementCreator() {
         @Override
         public PreparedStatement createPreparedStatement(final Connection connection) throws SQLException {
                PreparedStatement statement = connection.prepareStatement("insert into login_ui " +
                        "(ui_name, img_path, use_yn, reg_id, reg_dt, reg_ip) " +
                        "values (?,?,?,?,?,?)", new String[]{"id"});
                statement.setString(1, loginVO.getName());
                statement.setString(2, loginVO.imgPath());
                statement.setString(3, loginVO.isUse() == true ? "Y" : "N");
                statement.setLong(4, loginVO.getRegInfo().getId());
                statement.setTimestamp(5, Timestamp.valueOf(loginVO.getRegInfo().getDate()));
                statement.setLong(6, inetConverter.convertToDatabaseColumn(loginUIData.getRegInfo().getIp()));

                return statement;
            }
        }, keyHolder);
        loginUIData.setId(keyHolder.getKey().longValue());
        return loginUIData.getId();
 }

μ΄λ ‡κ²Œ μž‘μ„±ν–ˆλ˜ μ½”λ“œλ₯Ό

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
@Override
public Long saveLoginUI(final LoginVO loginVO) {

    SqlParameterSource sqlParameterSource = new MapSqlParameterSource()
            .addValue("ui_name", loginVO.getName())
            .addValue("img_path", loginVO.getImgPath())
            .addValue("use_yn", loginVO.isUse() == true ? "Y" : "N")
            .addValue("reg_id", loginVO.getRegInfo().getId())
            .addValue("reg_dt", loginVO.getRegInfo().getDate())
            .addValue("reg_ip", inetConverter.convertToDatabaseColumn(loginVO.getRegInfo().getIp()));

    Number id = simpleJdbcInsert.executeAndReturnKey(sqlParameterSource);
    loginUIData.setId(id.longValue());
    return loginUIData.getId();
}

μ΄λŸ°μ‹μœΌλ‘œ κ°„νŽΈν•˜κ²Œ λ§Œλ“€μ–΄ μ€«μ—ˆμœΌλ‹ˆ 말이닀.

ν…ŒμŠ€νŠΈ μ½”λ“œμ—μ„œλ„ 문제 μ—†κ³ , λ‘œμ»¬μ—μ„œλ„ 잘 λ™μž‘ν•˜κΈΈλž˜ 개발 μ„œλ²„μ— μ˜¬λ Έλ”λ‹ˆ…

였늘 ν•œ λ°© λ¨Ήμ—ˆλ‹€…

Caused by: java.sql.SQLException: Out of range value for column 'reg_ip' at row 1
Query is: INSERT INTO login_ui (ui_name, img_path, use_yn, reg_id, reg_dt, reg_ip, upd_id, upd_dt, upd_ip) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?), parameters ['test','img_path','N',1522
909,'2019-12-09 15:40:04.143',-1218290558,<null>,<null>,<null>]
    at org.mariadb.jdbc.internal.util.LogQueryTool.exceptionWithQuery(LogQueryTool.java:153)
    at org.mariadb.jdbc.internal.protocol.AbstractQueryProtocol.executeQuery(AbstractQueryProtocol.java:254)
    at org.mariadb.jdbc.MariaDbPreparedStatementClient.executeInternal(MariaDbPreparedStatementClient.java:209)
    ... 85 common frames omitted

?????

reg_ip 에 μ™œ -1218290558 λΌλŠ” 값이…?

Long 으둜 λ„˜μ–΄κ°€λŠ” 건 ν™•μΈν–ˆλ‹€. 그런데 μ™œ - 값이 λ‚˜μ˜€λŠ”κ±ΈκΉŒ?? ν•˜λ‹€κ°€ long -> 숫자.. μ–΄? μƒκ°ν•΄λ³΄λ‹ˆ overflow !!

ipλ₯Ό DB 에 μ €μž₯ν•  λ•Œ, κ·ΈλŒ€λ‘œ μ €μž₯ν•˜μ§€ μ•Šκ³ , inetConvert λ₯Ό 톡해 long κ°’μœΌλ‘œ μ €μž₯ν•˜λŠ”λ°, λ‘œμ»¬μ—μ„œλŠ” ν…ŒμŠ€νŠΈ ν•  λ•Œ 127.0.0.1 은 int 둜 변경해도 int의 μ΅œλŒ€ κ°’ λ²”μœ„ μ•ˆμΈλ°, λ‹€λ₯Έ μ•„μ΄ν”Όμ˜ 경우 int 의 μ΅œλŒ€ 값을 λ„˜μ–΄κ°€κΈ°μ— overflow κ°€ λ°œμƒν•˜μ—¬ - 값이 λ˜λŠ” κ²ƒμ΄μ—ˆλ‹€.

κ·Έλ ‡λ‹€λ©΄ μ™œ long 둜 μ„€μ •ν•œ 값이 int 둜 κ°•μ œ ν˜•λ³€ν™˜μ΄ λ˜μ–΄ μ €μž₯이 λ˜μ—ˆλ˜ 걸까?

κ·Έλž˜μ„œ λ””μ»΄νŒŒμΌμ„ 톡해 μ°Ύμ•„λ΄€λ‹€.

SimpleJdbcInsert 의 executeAndReturnKey λ₯Ό 보면

  1. μ»΄νŒŒμΌμ„ 톡해 DB 컬럼의 데이터 νƒ€μž…μ„ μ•Œμ•„λƒ„
  2. νŒŒλΌλ―Έν„° κ°’κ³Ό insert ν•  컬럼 값을 맀칭
  3. insert μ‹€ν–‰

이런 μ‹μœΌλ‘œ μ§„ν–‰λ˜λŠ” 것 κ°™λ‹€. (μžμ„ΈνžˆλŠ” λͺ¨λ₯΄κ² μ§€λ§Œ, ν™•μΈν•œ λ°”λ‘œλŠ”)

λ¬Έμ œλŠ” 컴파일 ν•˜λŠ” μ‹œμ μ— DB 컬럼 κ°’μ˜ 데이터 νƒ€μž…μ„ κ°€μ Έμ™€μ„œ java.sql.Types 의 값을 λ§žμΆ”λŠ”λ°, Maria DB νƒ€μž…μ—λŠ” int λŠ” μžˆμ§€λ§Œ, long 은 μ—†λ‹€.

κ·Έλ ‡λ‹€λ³΄λ‹ˆ DB μ»¬λŸΌμ—λŠ” int 둜 λ˜μ–΄ μžˆκΈ°μ—, 컴파일 ν•  λ•Œ reg_ip κ°€ int value κ°€ λ˜λŠ” 것이고.. μ΅œλŒ€ 값을 μ§€λ‚˜κ°€λ²„λ¦° long value λŠ” overflow κ°€ λ‚˜μ„œ κ·ΈλŒ€λ‘œ - κ°’…

이 μ‹œμ μ—μ„œ λ‚΄κ°€ μ·¨ν•  수 μžˆλŠ” μ„ νƒμ§€λŠ” DB 컬럼 값을 λ°”κΎΈκ±°λ‚˜, 이전 jdbcTemplate 을 μ‚¬μš©ν•˜λŠ” 것인데 DB λ₯Ό ν•¨λΆ€λ‘œ λ°”κΏ€ 순 μ—†κ³ , μ†ŒμŠ€λ₯Ό λ°”κΎΈλŠ” 게 λ¦¬μ†ŒμŠ€ 적으둜 μ’‹λ‹€κ³  νŒλ‹¨ν•˜μ—¬ μ†ŒμŠ€λ₯Ό λ³€κ²½ν•˜μ—¬μ„œ 일단락 λ˜μ—ˆλ‹€.

μ—­μ‹œ 잘 μ•Œκ³  써야 ν•œλ‹€.. γ…‹γ…‹

Share on

snack
WRITTEN BY
snack
Web Programmer


What's on this Page