database.sarang.net
UserID
Passwd
Database
DBMS
MySQL
ㆍPostgreSQL
Firebird
Oracle
Informix
Sybase
MS-SQL
DB2
Cache
CUBRID
LDAP
ALTIBASE
Tibero
DB 문서들
스터디
Community
공지사항
자유게시판
구인|구직
DSN 갤러리
도움주신분들
Admin
운영게시판
최근게시물
PostgreSQL Q&A 8711 게시물 읽기
No. 8711
CallableStatement를 사용해서 Array 입력 문의입니다.
작성자
박재혁
작성일
2010-10-01 15:23ⓒ
2010-10-01 15:25ⓜ
조회수
8,620

안녕하세요..

Array 입력하기 너무 힘드네요. 5시간도안 구글링 하다가 포기하고 질문 올립니다.

Create Table Test(

     name vachar(50)[]

)

이런 테이블에 name을 입력하는 겁니다.

DBConnection dbc = null;
  CallableStatement cs = null;
  java.sql.Array array = null;
 
  try {
   dbc = new DBConnection();   
   Connection con = dbc.getConnection();
   
   cs = con.prepareCall("{ call insert_group_value(?,?,?) }");

   cs.setString(1, "aquua");
   cs.setString(2, "maillist");
   array = con.createArrayOf("int4", new String[] { "a","b"}); 
   cs.setArray(3, sqlArray);
   cs.executeUpdate();

  } catch (Exception e) {
   System.out.println(e.toString());
  } finally {
   DbUtils.closeQuietly(cs);
   if(dbc!=null) dbc.release();
  }

구글 검색 결과 빨간색 표시한 글씨처럼 하면 된다고 하는데
문제는 array 가 null 이 나옵니다.

array를 입력할 수 만 있으면 나머지 function에서는 어떻게 해 볼 수 있겠는데.. 영 안 되네요..


DB 버전은  "PostgreSQL 8.4.4, compiled by Visual C++ build 1400, 32-bit"
JDBC 버전은 postgresql-8.4-702.jdbc4.jar 입니다.

 

이 글에 대한 댓글이 총 1건 있습니다.

public class TextArray implements Array {

private String[] inner;
    private String stringValue;
   
    /**
     * String[]을 받아 Stirng으로 만들어 준다.
     * @param inner
     */
    public TextArray(String[] inner) {
        this.inner = inner;
        if (null == inner) {
            this.stringValue = null;
        }
        StringBuilder sb = new StringBuilder();
        int len = inner.length;
        sb.append("{");
        for (int si = 0 ; si < len ; si++) {
            if (si > 0) {
                sb.append(",");
            }
            sb.append(inner[si]);
        }
        sb.append("}");
        this.stringValue = sb.toString();
    }
   
    public TextArray() {
       
    }

    public String toString() {
        return this.stringValue;
    }

    @Override
    public void free() throws SQLException {
    }

    @Override
    public Object getArray() throws SQLException {
        if (null == inner) {
            return null;
        }
       
        return inner;
    }

    @Override
    public Object getArray(Map<String, Class<?>> map) throws SQLException {
        if (null == inner) {
            return null;
        }
       
        return inner;
    }

    @Override
    public Object getArray(long index, int count) throws SQLException {
        if (null == inner) {
            return null;
        }
       
        int len = inner.length;
        int countToUse = count;
        if ((index + count) >= len) {
            countToUse = len - (int) index;
        }
        String[] ret = new String[countToUse];
        System.arraycopy(inner, (int) index, ret, 0, countToUse);
        return ret;
    }

    @Override
    public Object getArray(long index, int count, Map<String, Class<?>> map) throws SQLException {
        return getArray(index, count);
    }

    @Override
    public int getBaseType() throws SQLException {
        return Types.VARCHAR;
    }

    @Override
    public String getBaseTypeName() throws SQLException {
        return "varchar";
    }

    @Override
    public ResultSet getResultSet() throws SQLException {
        throw new UnsupportedOperationException();
    }

    @Override
    public ResultSet getResultSet(Map<String, Class<?>> map) throws SQLException {
        throw new UnsupportedOperationException();
    }

    @Override
    public ResultSet getResultSet(long index, int count) throws SQLException {
        throw new UnsupportedOperationException();
    }

    @Override
    public ResultSet getResultSet(long index, int count, Map<String, Class<?>> map) throws SQLException {
        throw new UnsupportedOperationException();
    }
 

}

 

이런식에 클래스를 만들어서 저장할때

tags는 String[] 배열입니다.

TextArray textArray = new TextArray(tags);

이럭식으로 해서 사용중입니다.

열혈지누(jinukey)님이 2010-10-05 15:20에 작성한 댓글입니다.
[Top]
No.
제목
작성자
작성일
조회
8716update할 때 deadlock detected error [2]
심상호
2010-10-15
9584
8715해당테이블에 컬럼갯수에 따른 속도차에 대하여 질문. [1]
김현진
2010-10-13
7679
8713Postgre DB X, Y 좌표 구하기
chrh01
2010-10-07
9201
8711CallableStatement를 사용해서 Array 입력 문의입니다. [1]
박재혁
2010-10-01
8620
8710[9.0] 설치가 제대로 되질 않습니다. [3]
도와주세요
2010-09-30
8556
8709몇가지 질문 [4]
뭉치
2010-09-29
8807
8708DB Connect을 못하겠습니다. [4]
뭉치
2010-09-21
10455
Valid XHTML 1.0!
All about the DATABASE... Copyleft 1999-2024 DSN, All rights reserved.
작업시간: 0.027초, 이곳 서비스는
	PostgreSQL v16.2로 자료를 관리합니다