-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostgreSQLApp.java
More file actions
110 lines (94 loc) · 5.56 KB
/
PostgreSQLApp.java
File metadata and controls
110 lines (94 loc) · 5.56 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/* ************************************************************************************************************************
# * *
# * @License Starts *
# * *
# * Copyright © 2015 - present. MongoExpUser. All Rights Reserved. *
# * *
# * License: MIT - hhttps://github.com/MongoExpUser/euro-soccer-league-full-text-search-with-postgres/blob/main/LICENSE *
# * *
# * @License Ends *
# ************************************************************************************************************************
# * 1) The class implements method for: *
# * a) Connecting to and disconnecting from PostgreSQL or PostgreSQL-Compatible DB *
# * b) Running SQL queries on PostgreSQL or PostgreSQL-Compatible DBs *
# * 2) Implementation is done with Java version 17 and Java JDBC API *
# * *
# ************************************************************************************************************************/
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;
public class PostgreSQLApp
{
public void connectAndRunQueries(String host, String dbName, String schemaName, String url, String user, String password, String sslrootcertPath) throws SQLException, ClassNotFoundException
{
Properties props = new Properties();
props.setProperty("user", user);
props.setProperty("password", password);
props.setProperty("ssl", "true");
props.setProperty("sslmode", "verify-ca"); // "verify-full" || "verify-ca" || disable || "allow" || prefer || "require"
props.setProperty("sslrootcert", sslrootcertPath);
Integer statementTimeout = 60000;
String options = String.format("-c search_path=%s,public -c statement_timeout=%s", schemaName, statementTimeout);
props.setProperty("options", options);
try
{
// conn = DriverManager.getConnection(url, user, password);
Connection conn = DriverManager.getConnection(url, props);
if(conn instanceof Connection)
{
System.out.println("Successfully connected to PostgreSQL server.");
// then add queries to run
String query0 = "SELECT datname, usename, ssl, client_addr FROM pg_stat_ssl JOIN pg_stat_activity ON pg_stat_ssl.pid = pg_stat_activity.pid;";
String query1 = "SELECT VERSION()";
String query2 = "SELECT * FROM pg_stat_ssl ORDER BY ssl;";
String query3 = "SELECT * FROM league.soccer LIMIT 5;";
Statement st = conn.createStatement();
String [] queryList = new String[4];
queryList[0] = query0;
queryList[1] = query1;
queryList[2] = query2;
queryList[3] = query3;
// add other queries from the repo and expand the list as defined in the queryList above
for(int index = 0; index < queryList.length; index++)
{
// execute query
ResultSet rs = st.executeQuery(queryList[index]);
// fetch and show query result
while(rs.next())
{
if(index == 1)
{
System.out.println("==================================");
System.out.println("Version: " + rs.getString(1));
System.out.println("==================================");
}
}
rs.close();
}
st.close();
conn.close();
System.out.println("Connection closed....");
}
}
catch (SQLException error)
{
System.out.println(error.getMessage());
}
}
public static void main(String[] args) throws SQLException, ClassNotFoundException
{
String host = "host";
String dbName = "euro";
String schemaName = "league";
String engineName = "postgresql";
String url = String.format("jdbc:%s://%s/%s", engineName, host, dbName);
String user = "user";
String password = "password";
String sslrootcertPath = "/path/to/root-cert.pem";
PostgreSQLApp app = new PostgreSQLApp();
app.connectAndRunQueries(host, dbName, schemaName, url, user, password, sslrootcertPath);
}
}