Guest User

JDBC: Урок 4. Connection - решение сложностей

a guest
Jun 14th, 2016
2,734
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.sql.Connection;
  2. import java.sql.Driver;
  3. import java.sql.DriverManager;
  4. import java.sql.SQLException;
  5.  
  6. /**
  7.  * JDBC: Урок 4. Connection - Получаем соединение к БД
  8.  */
  9. public class Main {
  10.  
  11.     private final static String URL =
  12.             "jdbc:mysql://localhost:3306/mydbtest";
  13.     private final static String URLFIXED =
  14.             "jdbc:mysql://localhost:3306/mydbtest?useUnicode=true&useSSL=true&useJDBCCompliantTimezoneShift=true" +
  15.                     "&useLegacyDatetimeCode=false&serverTimezone=UTC";
  16.     private final static String USERNAME = "root";
  17.     private final static String PASSWORD = "root";
  18.  
  19.     public static void main(String[] args) throws SQLException {
  20.         //Connection to database; Соединение с базой данных
  21.         Connection connection;
  22.  
  23.         /**
  24.          <dependency>
  25.          <groupId>mysql</groupId>
  26.          <artifactId>mysql-connector-java</artifactId>
  27.          <version>6.0.2</version>
  28.          </dependency>
  29.          * The driver is automatically registered via the SPI
  30.          * and manual loading of the driver class is generally unnecessary.
  31.          * Драйвер автоматически регистрируется через SPI и теперь не нужно вручную его регистрировать*/
  32.  
  33.         // load mysql driver | загружаем драйвер
  34. //        Driver driver = new com.mysql.cj.jdbc.Driver();
  35.  
  36.         // to register our driver | регистрируем драйвер
  37. //        DriverManager.registerDriver(driver);
  38.  
  39.         connection = DriverManager.getConnection(URLFIXED, USERNAME, PASSWORD);
  40.  
  41.         /*WARN: Establishing SSL connection without server's identity verification is not recommended.
  42.         According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default
  43.         if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'.
  44.         You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
  45.         Exception in thread "main" java.sql.SQLException:
  46.         The server time zone value 'Russia TZ 2 Standard Time' is unrecognized or represents more than one time zone.
  47.         You must configure either the server or JDBC driver (via the serverTimezone configuration property)
  48.         to use a more specifc time zone value if you want to utilize time zone support.
  49.         http://stackoverflow.com/questions/26515700/mysql-jdbc-driver-5-1-33-time-zone-issue
  50.  
  51.         при возникновении эксепшена или warninga, пользуемся URLFIXED
  52. */
  53.         if (!connection.isClosed()) {
  54.             System.out.println("Соединение с БД Установлено!");
  55.         }
  56.         connection.close();
  57.         if (connection.isClosed()) {
  58.             System.out.println("Соединение с БД Закрыто!");
  59.         }
  60.     }
  61. }
RAW Paste Data