Java Oracle ARRAY read

Description

Java Oracle ARRAY read


import java.math.BigDecimal;
import java.sql.Array;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Main {
   public static void queryArray(Connection conn) {
      PreparedStatement pstmt = null;
      String sql = null;/*from  www  .  jav a2  s  .  c om*/
      ResultSet rset = null;
      Array chapters = null;
      try {

         sql = "SELECT ID, CHAPTER_LIST, LAST " + 
                 "FROM AUTHOR "; 
         pstmt = conn.prepareStatement(sql);

         rset = pstmt.executeQuery(sql);
         while (rset.next()) {
            chapters = rset.getArray(2);
            BigDecimal[] chapterNumbers = (BigDecimal[]) chapters.getArray();
            System.out.println(rset.getString(3) + "\n");
            for (BigDecimal idx : chapterNumbers) {
               System.out.println(idx + "\n");
            }
         }
      } catch (SQLException ex) {
         ex.printStackTrace();
      } finally {
         if (pstmt != null) {
            try {
               pstmt.close();
            } catch (SQLException ex) {
               ex.printStackTrace();
            }
         }
         if (rset != null) {
            try {
               pstmt.close();
            } catch (SQLException ex) {
               ex.printStackTrace();
            }
         }
      }
   }
}



PreviousNext

Related