使用jdbc从Oracle获取自定义类型数据

时间:2015-01-28 09:27:52

标签: java oracle jdbc

我在从Oracle DB获取Java数据时遇到了麻烦。而不是真正的价值,只从数据库返回3个问号。

首先我按如下方式创建了我的类型:

create or replace TYPE varchar2_ntt AS VARRAY(5000) OF varchar2(200);    

然后我写了我的sql:

SELECT del.stop_name AS direction,
  del.departures
FROM
  (SELECT mvv.STOP_NAME,
    mvv.stop_id,
    CAST( COLLECT(SUBSTR(departure_time,1,5)
  ORDER BY departure_time) AS varchar2_ntt ) departures
  FROM stop_times st,
    MV_TRIP_V_SMER mvv,
    trips t
  LEFT JOIN calendar c
  ON c.service_id= t.service_id,
    routes r,
    mv_rezimi_za_postajni_vr pvr
  WHERE mvv.trip_id =st.trip_id
  AND st.trip_id    =t.TRIP_ID
  AND r.route_id    =t.route_id
  AND pvr.service_id=c.service_id
  AND pvr.workday=1
  AND st.stop_id    ='2206100000201'
  GROUP BY mvv.STOP_NAME,
    mvv.stop_id
  ) DEL

sql developer中列离开的结果看起来很好。

SHEMA.VARCHAR2_NTT('13:15','16:25','18:15','19:45')

在Java中我试图得到结果:

while (rs.next()){
    ARRAY departures_a =((OracleResultSet)rs).getARRAY("departures");
    System.out.println ("Array is of type "+departures_a.getSQLTypeName());
    System.out.println ("Array is of length "+departures_a.length());
    String[] departures_arr = (String[]) departures_a.getArray();
    for (int n=0; n<departures_arr.length; n++){
         System.out.println("departure "+n+":"+ departures_arr[n]);
    }
}

控制台输出:

Array is of type SHEMA.VARCHAR2_NTT
Array is of length 4
departure 0:???
departure 1:???
departure 2:???
departure 3:???

我做错了什么?为什么有问号而不是真值?

我找到了指示 herehere

1 个答案:

答案 0 :(得分:0)

可能是一个字符集问题。尝试打印您收到的结果。

System.out.printf("departure %d: %s%n", i, Arrays.toString(departures[i].getBytes()));

还打印typecode

System.out.println("Array element is of typecode " + departures_a.getBaseType());