Advertisement
Guest User

Untitled

a guest
May 16th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.IOException;
  3. import java.io.UncheckedIOException;
  4. import java.net.InetAddress;
  5. import java.net.Socket;
  6. import java.net.SocketAddress;
  7. import java.net.UnknownHostException;
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.ResultSet;
  11. import java.sql.SQLException;
  12. import java.sql.Statement;
  13. import java.util.Properties;
  14.  
  15. import javax.net.SocketFactory;
  16.  
  17. import org.newsclub.net.unix.AFUNIXSocket;
  18. import org.newsclub.net.unix.AFUNIXSocketAddress;
  19. import org.postgresql.PGProperty;
  20. import org.postgresql.core.SocketAddressFactory;
  21. import org.postgresql.util.HostSpec;
  22.  
  23. public class SocketTest {
  24. public static void main(String[] args) throws SQLException {
  25. Properties props = new Properties();
  26. props.setProperty(PGProperty.SOCKET_FACTORY.getName(), LocalPostgresSocketFactory.class.getName());
  27. props.setProperty(PGProperty.SOCKET_ADDRESS_FACTORY.getName(),
  28. LocalPostgresSocketAddressFactory.class.getName());
  29.  
  30. props.setProperty(PGProperty.PG_DBNAME.getName(), "testdb");
  31. props.setProperty(PGProperty.USER.getName(), "isopov");
  32. props.setProperty(PGProperty.PASSWORD.getName(), "isopov");
  33.  
  34. try (Connection con = DriverManager.getConnection("jdbc:postgresql:testdb", props);
  35. Statement statement = con.createStatement();
  36. ResultSet res = statement.executeQuery("select 1")) {
  37. while (res.next()) {
  38. System.out.println(res.getString(1));
  39. }
  40. }
  41. }
  42.  
  43. public static class LocalPostgresSocketAddressFactory implements SocketAddressFactory {
  44. @Override
  45. public SocketAddress create(HostSpec hostSpec) {
  46. try {
  47. return new AFUNIXSocketAddress(new File("/var/run/postgresql/.s.PGSQL.5432"));
  48. } catch (IOException e) {
  49. throw new UncheckedIOException(e);
  50. }
  51. }
  52.  
  53. }
  54.  
  55. public static class LocalPostgresSocketFactory extends SocketFactory {
  56.  
  57. @Override
  58. public Socket createSocket() throws IOException {
  59. return AFUNIXSocket.newInstance();
  60. }
  61.  
  62. @Override
  63. public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
  64. throw new RuntimeException();
  65. }
  66.  
  67. @Override
  68. public Socket createSocket(InetAddress host, int port) throws IOException {
  69. throw new RuntimeException();
  70. }
  71.  
  72. @Override
  73. public Socket createSocket(String host, int port, InetAddress localHost, int localPort)
  74. throws IOException, UnknownHostException {
  75. throw new RuntimeException();
  76. }
  77.  
  78. @Override
  79. public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort)
  80. throws IOException {
  81. throw new RuntimeException();
  82. }
  83.  
  84. }
  85.  
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement