Background Image

FORUM

조회 수 8917 추천 수 0 댓글 2
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄 첨부



package test.test.test;


import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.SQLException;

import java.sql.Statement;

import java.util.Locale;


import javax.sql.DataSource;


import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.jdbc.core.PreparedStatementCreator;

import org.springframework.jdbc.core.support.JdbcDaoSupport;

import org.springframework.jdbc.support.GeneratedKeyHolder;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;


/**

 * Handles requests for the application home page.

 */

@Controller

public class HomeController extends JdbcDaoSupport {

@Autowired

public void setDataSourceForAutoWire(DataSource ds){

super.setDataSource(ds);

}

/**

* Simply selects the home view to render by returning its name.

*/

@RequestMapping(value = "/", method = RequestMethod.GET)

public String home(Locale locale, Model model) {

GeneratedKeyHolder generatedKeyHolder = new GeneratedKeyHolder();

System.out.println("DB Source obejct check : " + getJdbcTemplate().getDataSource().toString()); 

try{

getJdbcTemplate().update("DROP TABLE test_tbl;");

}catch(Exception e){

//없을수도 있음

}


getJdbcTemplate().update(

"CREATE TABLE test_tbl( " +

"tt_idx INTEGER AUTO_INCREMENT PRIMARY KEY, " +

"tt_str character varying(15) NOT NULL, " +

"tt_dt datetime " +

")"

);

getJdbcTemplate().update(

new PreparedStatementCreator() {

public PreparedStatement createPreparedStatement(Connection con) throws SQLException {

PreparedStatement ps = con.prepareStatement("INSERT INTO test_tbl(" +

"tt_str, " +

"tt_dt" +

") VALUES(" +

"?, " +

"sysdate" +

")",

Statement.RETURN_GENERATED_KEYS

);

ps.setString(1, "test");

return ps;

}

},

generatedKeyHolder

);

System.out.println(generatedKeyHolder.getKey() == null ? "getKey is NULL" : "OK");

System.out.println(generatedKeyHolder.getKeyList().size() == 0 ? "getKeyList is empty" : "OK");

System.out.println(generatedKeyHolder.getKeys() == null ? "getKeys is NULL" : "OK");

return "home";

}

}





이렇게 해본 결과 키가 나오지 않습니다.


DB Source obejct check : org.apache.commons.dbcp.BasicDataSource@3acc7
getKey is NULL
getKeyList is empty
getKeys is NULL




스프링 JdbcTemplate 을 사용해서 큐브리드 사용이 다른건 다 되는거 같은데,

키 가져오는것은 안되는거 같네요.

혹시 방법이 없을까요?


  • ?
    이용미 2012.08.27 18:03

    안녕하세요. 질문 감사드립니다.

     

    우선, 바인드를 사용한 쿼리를 Static으로 변경하여 Test가 가능하신가요?

    가능하시다면 쿼리를 변경하여 Test 부탁 드립니다.

  • ?
    안지민 2012.08.29 20:00

    static이 뭔지 잘 모르겠네요... 바인드를 쓰지 않는다는 의미시면


    getJdbcTemplate().update(

    new PreparedStatementCreator() {

    public PreparedStatement createPreparedStatement(Connection con) throws SQLException {

    PreparedStatement ps = con.prepareStatement("INSERT INTO test_tbl(" +

    "tt_str, " +

    "tt_dt" +

    ") VALUES(" +

    "'test', " +

    "sysdate" +

    ")",

    Statement.RETURN_GENERATED_KEYS

    );

    return ps;

    }

    },

    generatedKeyHolder

    );



    이렇게 해봤지만 동일 하네요.

    아니면 혹시 PreparedStatement 가 아니라 Statement를 쓰라는 의미이시면...
    spring에 GeneratedKeyHolder를 사용하려면 반드시 PreparedStatement를 사용해야만 하는것으로 알고 있습니다.


List of Articles
번호 제목 글쓴이 날짜 조회 수
공지 CUBRID 사용자를 위한 DBeaver 도구 출시 안내 admin 2024.04.23 132
공지 SQLGate for CUBRID 영구 무료 라이선스 제공 file admin 2020.04.09 4476
4003 tomcat7 + cubrid 1 깽즈야 2012.07.27 91109
4002 원격접속이 안되네요????????? 2 Chris 2013.07.23 89531
4001 현재 연결은 원격 호스트에 의해 강제로 끊겼습니다 1 바람바람 2013.05.29 84908
4000 CUBRID 7.3 + zeroboard XE 관리자 로그인 에러 1 윤희서 2009.01.17 83643
3999 cub_master: Cannot bind local address... aborting.... Address already in use 4 박상현 2008.11.28 83568
3998 큐브리드 다운로더 오류 발생 5 file 차오이 2008.11.22 78500
3997 zeroboard XE 설치시 'database connect fail' error 4 윤희서 2009.01.16 78165
3996 Image 데이터타입 사용방법 3 AD 2008.11.25 73851
3995 clob 문의 1 김해영 2012.11.29 73010
3994 linux server 환경에서 php 버전은 어떤걸로 선택해야하는지. 1 거기 2008.11.30 66367
3993 델파이에서는 어떻게 사용할수 있을까요? 2 이상원 2008.11.29 65569
3992 if exists 조건 처리문? 2 콩이아부지 2013.03.06 63612
3991 update 쿼리를 이용한 시리얼 수정에 관하여 1 인경수 2008.11.27 63456
3990 큐브리드 2008 설치시 윈도우 서비스에 등록되지 않습니다. 3 file 진이 2008.11.22 63171
3989 CUBRID 7.3 + zeroboard XE 관리자 로그인 에러 1 윤희서 2009.01.17 62131
3988 큐브리드 maven repository 관련 문의 3 StoneHouse 2013.04.30 61469
3987 메니저에서 접속 안되는 오류 2 유나인 2013.08.16 61069
3986 .NET DB 제어를 위한 방법. 2 우페 2008.11.23 60879
3985 JDK가 두개 설치된 경우 선택적으로 JDK를 설정 할 수 있는 방법은 없는지요 1 GoFly 2008.11.28 60616
3984 데이터 이관시 소유자 변경 가능한가요??? 1 델몬트 2013.08.08 60070
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 201 Next
/ 201

Contact Cubrid

대표전화 070-4077-2110 / 기술문의 070-4077-2113 / 영업문의 070-4077-2112 / Email. contact_at_cubrid.com
Contact Sales