Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.Statement;
- public class MySQLConnect {
- public static void main(String[] args) {
- MySQLConnect m = new MySQLConnect();
- m.testDatabase();
- }
- private void testDatabase() {
- try {
- String url = "jdbc:mysql://localhost:3306/telegramphotoid?autoReconnect=true&useSSL=false";
- String login = "root";
- String password = "root";
- Connection con = DriverManager.getConnection(url, login, password);
- try {
- Statement stmt = con.createStatement();
- ResultSet rs = stmt.executeQuery("SELECT * FROM photoid");
- while (rs.next()) {
- String str = rs.getString("id") + ":" + rs.getString(2);
- System.out.println("fileId:" + str);
- }
- rs.close();
- stmt.close();
- } finally {
- con.close();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>testbd</groupId>
- <artifactId>bdtest</artifactId>
- <version>1.0-SNAPSHOT</version>
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <configuration>
- <source>7</source>
- <target>7</target>
- </configuration>
- </plugin>
- </plugins>
- </build>
- <dependencies>
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>8.0.11</version>
- </dependency>
- </dependencies>
- </project>
- java.sql.SQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
- at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:108)
- at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:95)
- at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:87)
- at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:61)
- at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:71)
- at com.mysql.cj.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:932)
- at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:857)
- at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:444)
- at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:230)
- at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:226)
- at java.sql.DriverManager.getConnection(DriverManager.java:664)
- at java.sql.DriverManager.getConnection(DriverManager.java:247)
- at MySQLConnect.testDatabase(MySQLConnect.java:18)
- at MySQLConnect.main(MySQLConnect.java:11)
- 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.
- at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
- at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
- at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
- at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
- at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:59)
- at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:83)
- at com.mysql.cj.util.TimeUtil.getCanonicalTimezone(TimeUtil.java:128)
- at com.mysql.cj.protocol.a.NativeProtocol.configureTimezone(NativeProtocol.java:2201)
- at com.mysql.cj.protocol.a.NativeProtocol.initServerSession(NativeProtocol.java:2225)
- at com.mysql.cj.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:1391)
- at com.mysql.cj.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:895)
- ... 8 more
- Process finished with exit code 0
- import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
- import java.sql.*;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- public class MySQLDriver {
- private static final String URL = "jdbc:mysql://localhost:3306/mysql?autoReconnect=true&useSSL=false";
- private static final String USERNAME = "root";
- private static final String PASSWORD = "root";
- public void dbtest() {
- System.out.println("------- Проверка подключения к MySQL -------");
- Connection connection = null;
- MysqlDataSource dataSource = new MysqlDataSource();
- dataSource.setUser(USERNAME);
- dataSource.setPassword(PASSWORD);
- dataSource.setServerName(URL);
- if(null != connection) {
- System.out.println("------- Подключение установлено -------");
- } else {
- System.out.println("------- Подключение НЕ установлено -------");
- }
- try {
- Connection conn = dataSource.getConnection();
- Statement stmt = conn.createStatement();
- ResultSet rs = stmt.executeQuery("SELECT * FROM telegramphotoid");
- System.out.println("code...");
- } catch (SQLException e) {
- System.out.println("Не удалось установить соединение!");
- return;
- }
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>5.1.36</version>
- </dependency>
- ------- Проверка подключения к MySQL -------
- ------- Подключение НЕ установлено -------
- Не удалось установить соединение!
- Process finished with exit code 0
Add Comment
Please, Sign In to add comment