-
Notifications
You must be signed in to change notification settings - Fork 3
/
insert.java
50 lines (42 loc) · 1.44 KB
/
insert.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;
import java.sql.PreparedStatement;
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 insert() {
try (Connection conn = connect()) {
String code = "GH";
String name = "Ghana";
PreparedStatement st = conn.prepareStatement("INSERT INTO countries(code, name) VALUES(?, ?)");
st.setString(1, code);
st.setString(2, name);
int rowsDeleted = st.executeUpdate();
System.out.println(rowsDeleted + " rows inserted.");
st.close();
} catch (SQLException ex) {
System.out.println(ex.getMessage());
}
}
public static void main(String[] args) {
App app = new App();
app.insert();
}
}