Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- public class OracleArrayExample {
- private static final int NUM_IDS=100000;
- public static void main(String[] args) throws SQLException {
- Connection conn = setup(args[0], args[1], args[2]);
- OracleConnection oConn = (OracleConnection) conn;
- try {
- // Define the array of IDs
- //Integer[] ids = {2, 6, 12}; // Example array
- //let's make an array of 100,000 IDs to search by
- Integer[] ids = new Integer[NUM_IDS];
- for (int i=0; i<NUM_IDS; i++) {
- ids[i] = i;
- }
- // prepare the SQL query with array parameter
- String sql = "SELECT * FROM t WHERE id IN (SELECT column_value FROM TABLE(?))";
- try (PreparedStatement pstmt = oConn.prepareStatement(sql)) {
- // set the array parameter
- Array oArray = oConn.createOracleArray("NUM_ARRAY", ids);
- pstmt.setArray(1, oArray);
- // Execute the query
- try (ResultSet rs = pstmt.executeQuery()) {
- while (rs.next()) {
- System.out.println("ID: " + rs.getInt("id"));
- }
- }
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- private static Connection setup(String jdbcUrl, String user, String pw) throws SQLException {
- DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
- Connection conn = DriverManager.getConnection(jdbcUrl, user, pw);
- conn.setAutoCommit(false);
- return conn;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement