0

I'm trying to connect to the MySQL database with SpringBoot but I get an error

    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.bind.annotation.*;
    import java.sql.*;

    @RestController
    @RequestMapping
    public class miController {

        private static String driver = "com.mysql.jdbc.Driver";
        private static String server = "jdbc:mysql://localhost/test";
        private static Connection conexion = null;

        miController(){
            conectar();
        }


        private void conectar() {
             try {
                 Class.forName(driver);
                 conexion = DriverManager.getConnection(server, "Yo", "pass");
             } catch (Exception e) {
                 System.out.println("Error: Imposible realizar la conexion a BD.");
                 e.printStackTrace();
             }

        }

    @RequestMapping(value = "/hola", method = RequestMethod.GET)
        public @ResponseBody String hola() {
            return "Hola mundo2!!  " ;
        }

    }

The code of the error it gives when the application starts is:

Thu Jun 28 17:24:42 CEST 2018 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Error: Imposible realizar la conexion a BD.
java.sql.SQLException: Unable to load authentication plugin 'caching_sha2_password'.
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:869)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:865)
    at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1746)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1226)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2188)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2219)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2014)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:776)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)

It is my first app with SpringBoot and I don't understand why I get error when I connect with mysql... I think that I saw something the SSL, but I don't understand... Ty

3
  • way of getting Connection is far, far from Boot philosophy. What envinronment, servlet container, JEE do You use? All have this problem solved Commented Jun 28, 2018 at 15:51
  • Ihilosophy... I am new with Boot, I don't know nothing...My connected is with... DriverManager.getConnection. I don't more know Commented Jun 28, 2018 at 15:56
  • why didi you do this? private void conectar() { try { Class.forName(driver); conexion = DriverManager.getConnection(server, "Yo", "pass"); } catch (Exception e) { System.out.println("Error: Imposible realizar la conexion a BD."); e.printStackTrace(); } } try using "spring-boot-starter-data-jpa" Commented Jun 28, 2018 at 16:05

1 Answer 1

1

Mysql should be configured in the application.properties file for SpringBoot. This is the beauty of SpringBoot.

Sample of the configuration as follows:

#Mysql connection configurations
spring.datasource.url = jdbc:mysql://localhost:3306/yourDatabaseName?useSSL=false
spring.datasource.username = yourDbUserName
spring.datasource.password = yourDbPassword
spring.datasource.driver-class-name = com.mysql.cj.jdbc.Driver
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.