PostgreSQL
 sql >> Teknologi Basis Data >  >> RDS >> PostgreSQL

Bagaimana cara mengakses prosedur yang mengembalikan setof refcursor dari PostgreSQL di Jawa?

returns setof refcursor berarti Anda mendapatkan ResultSet regular biasa di mana setiap "baris" berisi lainnya ResultSet saat memanggil getObject() :

Berikut ini berfungsi untuk saya:

ResultSet rs = stmt.executeQuery("select * from usp_sel_article_initialdata_new1()");
if (rs.next())
{
  // first result set returned
  Object o = rs.getObject(1);
  if (o instanceof ResultSet)
  {
    ResultSet rs1 = (ResultSet)o;
    while (rs1.next())
    {
       int id = rs1.getInt(1);
       String name = rs1.getString(2);
       .... retrieve the other columns using the approriate getXXX() calls
    }
  }
}

if (rs.next()) 
{
  // process second ResultSet 
  Object o = rs.getObject(1);
  if (o instanceof ResultSet)
  {
    ResultSet rs2 = (ResultSet)o;
    while (rs2.next())
    {
       ......
    }
  }
}

Dari dalam psql anda juga dapat menggunakan select * from usp_sel_article_initialdata_new1() Anda hanya perlu menggunakan FETCH ALL setelah itu. Lihat manual untuk contoh:http://www. postgresql.org/docs/current/static/plpgsql-cursors.html#AEN59018

postgres=> select * from usp_sel_article_initialdata_new1();
 usp_sel_article_initialdata_new1
----------------------------------
 <unnamed portal 1>
 <unnamed portal 2>
(2 rows)

postgres=> fetch all from "<unnamed portal 1>";
 ?column?
----------
        1
(1 row)

postgres=> fetch all from "<unnamed portal 2>";
 ?column?
----------
        2
(1 row)

postgres=>

(Saya membuat fungsi dummy untuk contoh di atas yang hanya mengembalikan satu baris dengan nilai 1 untuk kursor pertama dan 2 untuk kursor kedua)

Sunting :

Agar ini berfungsi, ini perlu dijalankan di dalam transaksi. Oleh karena itu autocommit harus dimatikan:

connection.setAutoCommit(false);


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. PostgreSQL:KUNCI ASING/ON DELETE CASCADE

  2. Bagikan koneksi ke postgres db di seluruh proses dengan Python

  3. PostgreSQL setara dengan pengumpulan massal Oracle

  4. Masukkan seluruh nilai massal DataTable ke dalam tabel postgreSQL

  5. Bagaimana cara meminta UUID untuk postgres