Created
May 31, 2017 07:27
-
-
Save chaudum/490eb4f0e65459983bd44382484605a1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.crate.sampleapp; | |
import java.sql.*; | |
import java.util.HashMap; | |
import java.util.Map; | |
public class ObjectArrayApplication { | |
public static void main(String[] args) { | |
// CREATE TABLE t1 ( | |
// obj_col OBJECT AS ( | |
// col_int INTEGER, | |
// col_str STRING | |
// ), | |
// obj_arr_col ARRAY(OBJECT AS ( | |
// col_int INTEGER, | |
// col_str STRING | |
// )) | |
// ); | |
try (Connection conn = DriverManager.getConnection("crate://localhost:5432/doc")) { | |
System.out.println("INSERT"); | |
PreparedStatement insertStatement = conn.prepareStatement("INSERT INTO t1 (obj_col, obj_arr_col) VALUES (?, ?)"); | |
insertStatement.setObject(1, new HashMap<String, Object>(){{ | |
put("col_int", 1); | |
put("col_str", "some text"); | |
}}); | |
insertStatement.setArray(2, conn.createArrayOf("object", new Map[]{ | |
new HashMap<String, Object>() {{ | |
put("col_int", 2); | |
put("col_str", "some other text"); | |
}}, | |
new HashMap<String, Object>() {{ | |
put("col_int", 3); | |
put("col_str", "another text"); | |
}} | |
})); | |
insertStatement.execute(); | |
System.out.println("REFRESH"); | |
Statement refreshStatement = conn.createStatement(); | |
refreshStatement.execute("REFRESH TABLE t1"); | |
System.out.println("SELECT"); | |
Statement selectStatement = conn.createStatement(); | |
ResultSet rs = selectStatement.executeQuery("SELECT obj_col, obj_arr_col FROM t1"); | |
while (rs.next()) { | |
Map<String, Object> obj_col = (Map<String, Object>) rs.getObject("obj_col"); | |
System.out.println(obj_col); | |
Array obj_arr_col = rs.getArray("obj_arr_col"); | |
System.out.println(obj_arr_col); | |
} | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
OUTPUT: