MCPcopy Create free account
hub / github.com/MaterializeInc/demos / App

Class App

connection-examples/java/subscribe.java:8–50  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

6import java.sql.Statement;
7
8public class App {
9
10 private final String url = "jdbc:postgresql://MATERIALIZE_HOST:6875/materialize";
11 private final String user = "MATERIALIZE_USERNAME";
12 private final String password = "MATERIALIZE_PASSWORD";
13
14 /**
15 * Connect to Materialize
16 *
17 * @return a Connection object
18 */
19 public Connection connect() throws SQLException {
20 Properties props = new Properties();
21 props.setProperty("user", user);
22 props.setProperty("password", password);
23 props.setProperty("ssl","true");
24
25 return DriverManager.getConnection(url, props);
26
27 }
28
29 public void subscribe() {
30 try (Connection conn = connect()) {
31
32 Statement stmt = conn.createStatement();
33 stmt.execute("BEGIN");
34 stmt.execute("DECLARE c CURSOR FOR SUBSCRIBE my_view");
35 while (true) {
36 ResultSet rs = stmt.executeQuery("FETCH ALL c");
37 if(rs.next()) {
38 System.out.println(rs.getString(1) + " " + rs.getString(2) + " " + rs.getString(3));
39 }
40 }
41 } catch (SQLException ex) {
42 System.out.println(ex.getMessage());
43 }
44 }
45
46 public static void main(String[] args) {
47 App app = new App();
48 app.subscribe();
49 }
50}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected