Guest User

Untitled

a guest
Jan 1st, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. import java.sql.*;
  2. import java.util.Properties;
  3.  
  4. public class MySQLSSLConnection {
  5.  
  6. /**
  7. * MySQL SSLMode Type
  8. */
  9. enum SSLModeType {
  10. DISABLED, PREFERRED, REQUIRED, VERIFY_CA, VERIFY_IDENTITY;
  11. }
  12.  
  13. private static final String DRIVER_CLASS_NAME = "com.mysql.cj.jdbc.Driver";
  14. private static final String MYSQL_JDBC_PREFIX = "jdbc:mysql://";
  15.  
  16. // Database Connection Information
  17. private static final String host = "";
  18. private static final int port = 0;
  19. private static final String database = "";
  20.  
  21. // Database User and Password
  22. private static final String USER = "";
  23. private static final String PASSWORD = "";
  24.  
  25. private static Properties properties = new Properties();
  26.  
  27. private static Connection connection;
  28.  
  29. public static void main(String[] args) {
  30. try {
  31. onPrepare();
  32. onRun();
  33. onDestroy();
  34. } catch (Exception e) {
  35. e.printStackTrace();
  36. }
  37. }
  38.  
  39. /**
  40. * Preparing for SSL connection setup
  41. */
  42. private static void onPrepare() throws ClassNotFoundException {
  43. Class.forName(DRIVER_CLASS_NAME);
  44.  
  45. // SSL Mode
  46. properties.put("sslMode", SSLModeType.VERIFY_CA.name());
  47.  
  48. properties.put("user", USER);
  49. properties.put("password", PASSWORD);
  50. }
  51.  
  52. /**
  53. * Connecting MySQL database to SSL
  54. */
  55. private static void onRun() throws SQLException {
  56. String jdbcUrl = jdbcUrl(host, port, database);
  57.  
  58. connection = DriverManager.getConnection(jdbcUrl, properties);
  59.  
  60. String testQuery = "select version()";
  61.  
  62. try (Statement statement = connection.createStatement();
  63. ResultSet resultSet = statement.executeQuery(testQuery)) {
  64. ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
  65. int columnCount = resultSetMetaData.getColumnCount();
  66.  
  67. while (resultSet.next()) {
  68. for (int i = 1; i <= columnCount; i++) {
  69. System.out.print(resultSet.getString(i) + "\t");
  70. }
  71. System.out.println();
  72. }
  73. } catch (SQLException e) {
  74. throw e;
  75. }
  76. }
  77.  
  78. /**
  79. * Close Database Connection Resource
  80. */
  81. private static void onDestroy() throws SQLException {
  82. if (connection != null) {
  83. connection.close();
  84. }
  85. }
  86.  
  87. /**
  88. * Generate and Return a MySQL JDBC URL
  89. *
  90. * @param host hostname or ip
  91. * @param port port number
  92. * @param database database name
  93. * @return String
  94. */
  95. private static String jdbcUrl(String host, int port, String database) {
  96. return MYSQL_JDBC_PREFIX + host + ":" + port + "/" + database;
  97. }
  98. }
Add Comment
Please, Sign In to add comment