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 MySQLArrayExample {
- public static void main(String[] args) throws SQLException {
- Connection conn = setup(args[0], args[1], args[2]);
- try {
- String jsonArray = "[1,2,3,4,5,100,200,-999]";
- String sql =
- "SELECT * FROM t WHERE id IN ( " +
- "SELECT CAST(value AS SIGNED) " +
- "FROM JSON_TABLE(?, '$[*]' COLUMNS (value JSON PATH '$')) AS jt" +
- ")";
- try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
- pstmt.setString(1, jsonArray);
- try (ResultSet rs = pstmt.executeQuery()) {
- while (rs.next()) {
- System.out.println("ID: " + rs.getInt("id"));
- }
- }
- }
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- conn.close();
- }
- }
- private static Connection setup(String jdbcUrl, String user, String pw) throws SQLException {
- DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
- Connection conn = DriverManager.getConnection(jdbcUrl, user, pw);
- conn.setAutoCommit(false);
- return conn;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement