View Javadoc
1   /*
2    * junixsocket
3    *
4    * Copyright 2009-2024 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.mysql.demo;
19  
20  import static org.newsclub.net.unix.demo.DemoHelper.addProperty;
21  
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.demo.DemoHelper;
31  
32  /**
33   * Demonstrates how to connect to a local MySQL server.
34   *
35   * @author Christian Kohlschuetter
36   */
37  public class AFUNIXDatabaseSocketFactoryDemo {
38    public static void main(String[] args) throws ClassNotFoundException, SQLException {
39      DemoHelper.initJDBCDriverClass("mysqlDriver", "", "com.mysql.jdbc.Driver");
40      System.out.println();
41  
42      final String connectionUrl = "jdbc:mysql://";
43      System.out.println("Connection URL=" + connectionUrl);
44  
45      // Override these properties by specifying JVM properties on the command line.
46      // try -DmysqlUser=test -DmysqlPassword=test -DmysqlSocket=/var/lib/mysql.sock
47      Properties props = new Properties();
48      addProperty(props, "socketFactory", "org.newsclub.net.mysql.AFUNIXDatabaseSocketFactory", //
49          "mysqlSocketFactory", "org.newsclub.net.mysql.AFUNIXDatabaseSocketFactoryCJ");
50      addProperty(props, "junixsocket.file", "/tmp/mysql.sock", //
51          "mysqlSocket", "/var/lib/mysql.sock");
52      addProperty(props, "user", "root", //
53          "mysqlUser", "test");
54      addProperty(props, "password", "", //
55          "mysqlPassword", "test");
56  
57      // Disable SSL with -DsslMode=DISABLED
58      // This could make things faster, or break everything.
59      // It should be fine, however, since we're not leaving the machine.
60      addProperty(props, "sslMode", "PREFERRED", "mysqlSslMode", "DISABLED");
61      // NOTE: in older versions of MySQL Connector/J, you need to set "useSSL" instead of "sslMode"
62      // addProperty(props, "useSSL", "true", "mysqlUseSSL", "false");
63  
64      System.out.println();
65  
66      try (Connection conn = DriverManager.getConnection(connectionUrl, props)) {
67        System.out.println("Connection: " + conn);
68  
69        DatabaseMetaData metadata = conn.getMetaData();
70        System.out.println("Database version: " + metadata.getDatabaseProductName() + " " + metadata
71            .getDatabaseProductVersion());
72  
73        String sql = "SHOW DATABASES";
74        System.out.println(sql);
75        try (Statement stmt = conn.createStatement(); //
76            ResultSet rs = stmt.executeQuery(sql)) {
77          while (rs.next()) {
78            System.out.println("* " + rs.getString(1));
79          }
80        }
81      }
82    }
83  }