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