Advertisement
Guest User

Untitled

a guest
Sep 17th, 2024
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.66 KB | None | 0 0
  1. import java.sql.*;
  2.  
  3. public class OracleArrayExample {
  4.  
  5.     private static final int NUM_IDS=100000;
  6.  
  7.     public static void main(String[] args) throws SQLException {
  8.         Connection conn = setup(args[0], args[1], args[2]);
  9.         OracleConnection oConn = (OracleConnection) conn;
  10.  
  11.         try {
  12.             // Define the array of IDs
  13.             //Integer[] ids = {2, 6, 12}; // Example array
  14.  
  15.             //let's make an array of 100,000 IDs to search by
  16.             Integer[] ids = new Integer[NUM_IDS];
  17.             for (int i=0; i<NUM_IDS; i++) {
  18.                 ids[i] = i;
  19.             }
  20.  
  21.             // prepare the SQL query with array parameter
  22.             String sql = "SELECT * FROM t WHERE id IN (SELECT column_value FROM TABLE(?))";
  23.             try (PreparedStatement pstmt = oConn.prepareStatement(sql)) {
  24.                 // set the array parameter
  25.                 Array oArray = oConn.createOracleArray("NUM_ARRAY", ids);
  26.  
  27.                 pstmt.setArray(1, oArray);
  28.  
  29.                 // Execute the query
  30.                 try (ResultSet rs = pstmt.executeQuery()) {
  31.                     while (rs.next()) {
  32.                         System.out.println("ID: " + rs.getInt("id"));
  33.                     }
  34.                 }
  35.             }
  36.         } catch (SQLException e) {
  37.             e.printStackTrace();
  38.         }
  39.     }
  40.  
  41.     private static Connection setup(String jdbcUrl, String user, String pw) throws SQLException {
  42.         DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
  43.  
  44.         Connection conn = DriverManager.getConnection(jdbcUrl, user, pw);
  45.         conn.setAutoCommit(false);
  46.  
  47.         return conn;
  48.     }
  49.  
  50. }
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement