Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- public class PostgresArrayExample {
- public static void main(String[] args) throws SQLException {
- Connection conn = setup(args[0], args[1], args[2]);
- try {
- Integer[] idArray = {1,2,3,4,5,100,200,-999};
- String sql = "SELECT * FROM t WHERE id = ANY(?)";
- try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
- pstmt.setArray(1, conn.createArrayOf("INTEGER", idArray));
- // 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 org.postgresql.Driver());
- Connection conn = DriverManager.getConnection(jdbcUrl, user, pw);
- conn.setAutoCommit(false);
- return conn;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement