Guest User

Untitled

a guest
Oct 31st, 2018
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.32 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.ResultSet;
  4. import java.sql.Statement;
  5.  
  6. public class MySQLConnect {
  7.  
  8.  
  9. public static void main(String[] args) {
  10. MySQLConnect m = new MySQLConnect();
  11. m.testDatabase();
  12. }
  13. private void testDatabase() {
  14. try {
  15. String url = "jdbc:mysql://localhost:3306/telegramphotoid?autoReconnect=true&useSSL=false";
  16. String login = "root";
  17. String password = "root";
  18. Connection con = DriverManager.getConnection(url, login, password);
  19. try {
  20. Statement stmt = con.createStatement();
  21. ResultSet rs = stmt.executeQuery("SELECT * FROM photoid");
  22. while (rs.next()) {
  23. String str = rs.getString("id") + ":" + rs.getString(2);
  24. System.out.println("fileId:" + str);
  25. }
  26. rs.close();
  27. stmt.close();
  28. } finally {
  29. con.close();
  30. }
  31. } catch (Exception e) {
  32. e.printStackTrace();
  33. }
  34. }
  35. }
  36.  
  37. <?xml version="1.0" encoding="UTF-8"?>
  38. <project xmlns="http://maven.apache.org/POM/4.0.0"
  39. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  40. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  41. <modelVersion>4.0.0</modelVersion>
  42.  
  43. <groupId>testbd</groupId>
  44. <artifactId>bdtest</artifactId>
  45. <version>1.0-SNAPSHOT</version>
  46. <build>
  47. <plugins>
  48. <plugin>
  49. <groupId>org.apache.maven.plugins</groupId>
  50. <artifactId>maven-compiler-plugin</artifactId>
  51. <configuration>
  52. <source>7</source>
  53. <target>7</target>
  54. </configuration>
  55. </plugin>
  56. </plugins>
  57. </build>
  58. <dependencies>
  59. <dependency>
  60. <groupId>mysql</groupId>
  61. <artifactId>mysql-connector-java</artifactId>
  62. <version>8.0.11</version>
  63. </dependency>
  64. </dependencies>
  65. </project>
  66.  
  67. java.sql.SQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
  68. at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:108)
  69. at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:95)
  70. at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:87)
  71. at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:61)
  72. at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:71)
  73. at com.mysql.cj.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:932)
  74. at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:857)
  75. at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:444)
  76. at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:230)
  77. at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:226)
  78. at java.sql.DriverManager.getConnection(DriverManager.java:664)
  79. at java.sql.DriverManager.getConnection(DriverManager.java:247)
  80. at MySQLConnect.testDatabase(MySQLConnect.java:18)
  81. at MySQLConnect.main(MySQLConnect.java:11)
  82. Caused by: com.mysql.cj.exceptions.InvalidConnectionAttributeException: The server time zone value 'RTZ 2 (çèìà)' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
  83. at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
  84. at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
  85. at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
  86. at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
  87. at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:59)
  88. at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:83)
  89. at com.mysql.cj.util.TimeUtil.getCanonicalTimezone(TimeUtil.java:128)
  90. at com.mysql.cj.protocol.a.NativeProtocol.configureTimezone(NativeProtocol.java:2201)
  91. at com.mysql.cj.protocol.a.NativeProtocol.initServerSession(NativeProtocol.java:2225)
  92. at com.mysql.cj.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:1391)
  93. at com.mysql.cj.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:895)
  94. ... 8 more
  95.  
  96. Process finished with exit code 0
  97.  
  98. import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
  99.  
  100. import java.sql.*;
  101. import java.util.logging.Level;
  102. import java.util.logging.Logger;
  103.  
  104. public class MySQLDriver {
  105. private static final String URL = "jdbc:mysql://localhost:3306/mysql?autoReconnect=true&useSSL=false";
  106. private static final String USERNAME = "root";
  107. private static final String PASSWORD = "root";
  108. public void dbtest() {
  109.  
  110. System.out.println("------- Проверка подключения к MySQL -------");
  111.  
  112. Connection connection = null;
  113. MysqlDataSource dataSource = new MysqlDataSource();
  114. dataSource.setUser(USERNAME);
  115. dataSource.setPassword(PASSWORD);
  116. dataSource.setServerName(URL);
  117.  
  118. if(null != connection) {
  119. System.out.println("------- Подключение установлено -------");
  120. } else {
  121. System.out.println("------- Подключение НЕ установлено -------");
  122. }
  123.  
  124.  
  125. try {
  126. Connection conn = dataSource.getConnection();
  127. Statement stmt = conn.createStatement();
  128. ResultSet rs = stmt.executeQuery("SELECT * FROM telegramphotoid");
  129. System.out.println("code...");
  130. } catch (SQLException e) {
  131. System.out.println("Не удалось установить соединение!");
  132. return;
  133. }
  134.  
  135. <dependency>
  136. <groupId>mysql</groupId>
  137. <artifactId>mysql-connector-java</artifactId>
  138. <version>5.1.36</version>
  139. </dependency>
  140.  
  141. ------- Проверка подключения к MySQL -------
  142. ------- Подключение НЕ установлено -------
  143. Не удалось установить соединение!
  144.  
  145. Process finished with exit code 0
Add Comment
Please, Sign In to add comment