| 1 |
/* |
| 2 |
* junixsocket |
| 3 |
* |
| 4 |
* Copyright 2009-2022 Christian Kohlschütter |
| 5 |
* |
| 6 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 |
* you may not use this file except in compliance with the License. |
| 8 |
* You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, software |
| 13 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 |
* See the License for the specific language governing permissions and |
| 16 |
* limitations under the License. |
| 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 |
* Demonstrates how to connect to a local PostgreSQL server via unix sockets. |
| 35 |
* |
| 36 |
* @author Christian Kohlschuetter |
| 37 |
* @see AFUNIXSocketFactory |
| 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; // the port number doesn't matter |
| 46 |
String socketPath = DemoHelper.getPropertyValue("socketPath", "/tmp/.s.PGSQL.5432", |
| 47 |
"/var/run/postgresql/.s.PGSQL.5432"); |
| 48 |
|
| 49 |
// Just try to connect to the socket using a file:// URI (this is not necessary, just for demo) |
| 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 |
// NOTE: for demo purposes, you can simply specify URIScheme, FactoryArg, etc. |
| 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 |
// System.setProperty("org.newsclub.net.unix.socket.default", "/tmp/.s.PGSQL.5432"); |
| 89 |
// props.setProperty("socketFactory", |
| 90 |
// "org.newsclub.net.unix.AFUNIXSocketFactory$SystemProperty"); |
| 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 |
} |