程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> Oracle數據庫 >> 關於Oracle數據庫 >> 用java從oracle取數

用java從oracle取數

編輯:關於Oracle數據庫

/*
drop table varray_table;
drop type num_varray; CREATE TYPE num_varray AS VARRAY(10) OF VARCHAR2(12)
/
CREATE TABLE varray_table (col1 num_varray);
INSERT INTO varray_table VALUES (num_varray('你好', 'abc')); select * from varray_table; */ import Java.sql.*;
import java.math.*;
import Oracle.jdbc.driver.*;
import oracle.sql.*; class Array1
{ public static void main(String args[]) throws Exception
{
  int oracleId = CharacterSet.ZHS16GBK_CHARSET;
  CharacterSet dbCharset = CharacterSet.make(oracleId);   DriverManager.registerDriver
                (new oracle.jdbc.driver.OracleDriver());   Connection conn =
      DriverManager.getConnection
                       ("jdbc:oracle:thin:@10.9.200.58:1521:db01",
                        "mytest",
                        "mytest");   Statement stmt = conn.createStatement();   ResultSet rs = stmt.executeQuery("SELECT * FROM varray_table");   while (rs.next()) {
    ARRAY my_array = ((OracleResultSet)rs).getARRAY(1);     // return the SQL type names, integer codes,
    // and lengths of the columns
    System.out.println ("Array is of type " + my_array.getSQLTypeName());
    System.out.println ("Array element is of typecode " + my_array.getBaseType());
    System.out.println ("Array is of length " + my_array.length());     // get Array elements
    String[] values = (String[]) my_array.getArray();
    for (int i = 0; i < values.length; i++)
    {
       oracle.sql.CHAR out_value = new oracle.sql.CHAR(values[i], dbCharset);
       System.out.println(">> index " + i + " = " + out_value);
    }
  }   rs.close();
  stmt.close();
  conn.close();
  }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved