Java Oracle ARRAY insert

Description

Java Oracle ARRAY insert


import java.sql.PreparedStatement;
import java.sql.SQLException;
import oracle.sql.ARRAY;
import oracle.sql.ArrayDescriptor;

public class Main {
   public static void storeArray() throws SQLException{
      PreparedStatement pstmt = null;
      String sql = null;//from  w ww  .j ava  2  s  . com
      Object [] chapters = {1,2,3};
      ARRAY chapterArray = null;
      try{
          ArrayDescriptor descriptor = ArrayDescriptor.createDescriptor("CHAP_LIST_TYPE", conn);
          chapterArray = new ARRAY(descriptor, conn, chapters);
          sql = "INSERT INTO AUTHOR VALUES(" +
                "author_recipes_seq.nextval, " +
                "?, " +
                "?)";
          pstmt = conn.prepareStatement(sql);
          pstmt.setString(1, "Jack");
          pstmt.setArray(2, chapterArray);
          pstmt.executeUpdate();
      } catch (SQLException ex){
          ex.printStackTrace();
      } finally {
          if (pstmt != null){
              pstmt.close();
          }
      }
  }
}



PreviousNext

Related