-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathRDS.java
More file actions
54 lines (49 loc) · 2 KB
/
RDS.java
File metadata and controls
54 lines (49 loc) · 2 KB
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
51
52
53
54
import java.sql.*;
public class RDS {
private static final String DEFAULT_USER = "test";
private static final String DEFAULT_PW = "test";
/**
* Simple connection to test for demo purpose
* Connects to an existing Postgres Database, creates a table, inserts some dummy data, and selects the data afterwards.
*
* @param hostname name of the database host
* @param port port
* @param dbname database name
* @return String with the result of the select query
*
* @throws SQLException
* @throws ClassNotFoundException
*/
protected static String test_connection(String hostname, Integer port, String dbname) throws SQLException, ClassNotFoundException {
Connection c = null;
try {
Class.forName("org.postgresql.Driver");
String url = String.format("jdbc:postgresql://%s:%d/%s", hostname, port, dbname);
c = DriverManager.getConnection(url, DEFAULT_USER, DEFAULT_PW);
c.setAutoCommit(true);
try (Statement stmt = c.createStatement()) {
String sql = "CREATE TABLE HELLO " +
"(ID INT PRIMARY KEY NOT NULL," +
" NAME TEXT NOT NULL) ";
stmt.executeUpdate(sql);
sql = "INSERT INTO HELLO (ID,NAME) VALUES (1, 'world');";
stmt.executeUpdate(sql);
String response = "";
try (ResultSet rs = stmt.executeQuery("SELECT * FROM HELLO;")) {
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
response += String.format("ID = %d\nNAME = %s", id, name);
}
}
return response;
}
} finally {
try {
if (c != null) c.close();
} catch (SQLException e) {
// we can ignore error here
}
}
}
}