1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.newsclub.net.unix.demo.jdbc;
19
20 import java.io.IOException;
21 import java.net.URLEncoder;
22 import java.sql.Connection;
23 import java.sql.DatabaseMetaData;
24 import java.sql.DriverManager;
25 import java.sql.ResultSet;
26 import java.sql.SQLException;
27 import java.sql.Statement;
28 import java.util.Properties;
29
30 import org.newsclub.net.unix.AFUNIXSocketFactory;
31 import org.newsclub.net.unix.demo.DemoHelper;
32
33
34
35
36
37
38
39 public final class PostgresDemo {
40 public static void main(String[] args) throws SQLException, ClassNotFoundException, IOException {
41 DemoHelper.initJDBCDriverClass("postgresqlDriver", "org.postgresql.Driver", null);
42
43 String databaseName = "postgres";
44
45 int port = 12345;
46 String socketPath = DemoHelper.getPropertyValue("socketPath", "/tmp/.s.PGSQL.5432",
47 "/var/run/postgresql/.s.PGSQL.5432");
48
49
50 new AFUNIXSocketFactory.URIScheme().createSocket("file://" + socketPath, port).close();
51
52 String urlEncoded = URLEncoder.encode("file://" + socketPath, "UTF-8");
53 System.out.println("urlEncoded: " + urlEncoded);
54 System.out.println();
55
56 String socketFactory = DemoHelper.getPropertyValue(
57 "socketFactory", "org.newsclub.net.unix.AFUNIXSocketFactory$FactoryArg",
58 "org.newsclub.net.unix.AFUNIXSocketFactory$URIScheme", (String s) -> {
59 if (!s.isEmpty() && !s.contains(".")) {
60
61 return "org.newsclub.net.unix.AFUNIXSocketFactory$" + s;
62 } else {
63 return s;
64 }
65 });
66
67 Properties props = new Properties();
68 props.setProperty("socketFactory", socketFactory);
69
70 DemoHelper.addProperty(props, "user", System.getProperty("user.name"), "postgresqlUser",
71 "root");
72 DemoHelper.addProperty(props, "password", "", "postgresqlPassword", "secret");
73 DemoHelper.addProperty(props, "sslMode", "disable", "postgresqlSslMode", "prefer");
74
75 String url;
76 if ("org.newsclub.net.unix.AFUNIXSocketFactory$URIScheme".equals(socketFactory)) {
77 url = "jdbc:postgresql://[" + urlEncoded + ":" + port + "/" + databaseName;
78 } else {
79 url = "jdbc:postgresql://localhost/postgres";
80 if ("org.newsclub.net.unix.AFUNIXSocketFactory$FactoryArg".equals(socketFactory)) {
81 DemoHelper.addProperty(props, "socketFactoryArg", socketPath, "socketFactoryArg", null);
82 }
83 }
84
85 System.out.println();
86 System.out.println("Connecting to db: " + url);
87
88
89
90
91
92 System.out.println();
93
94 try (Connection conn = DriverManager.getConnection(url, props)) {
95 System.out.println("Connection " + conn);
96
97 DatabaseMetaData metadata = conn.getMetaData();
98 System.out.println("Database version: " + metadata.getDatabaseProductName() + " " + metadata
99 .getDatabaseProductVersion());
100 String sql = "SHOW ALL";
101 System.out.println(sql);
102 try (Statement stmt = conn.createStatement();
103 ResultSet rs = stmt.executeQuery(sql)) {
104 while (rs.next()) {
105 System.out.println("* " + rs.getString(1) + "=" + rs.getString(2));
106 }
107 }
108 }
109 }
110 }