SpringBoot with Mysql, error connecting

1

I'm trying to connect to the MySQL database with SpringBoot but it gives me 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/pruebas";
    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)
    
asked by EduBw 28.06.2018 в 17:32
source

1 answer

2

Spring Boot by default uses hibernate for the connection to the database and is already integrated with it. This is a small glimpse of what he does.

  

Hibernate is an object-relational mapping tool (ORM) for the   Java platform (and also available for .Net with the name of   NHibernate) that facilitates the mapping of attributes between a database   traditional relational and object model of an application,   by means of declarative files (XML) or annotations in the beans of the   entities that allow establishing these relationships.

To configure the connection in your DB you only need to modify a file in the root that is called aplication.properties . These fields are configured:

spring.datasource.url=jdbc:mysql://localhost/db_springboot?useSSL=false
spring.datasource.username=root
spring.datasource.password=tucontraseña
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto= create-drop
logging.level.org.hibernate.SQL=debug

The line spring.jpa.hibernate.ddl-auto= create-drop is responsible for creating the structure of your database and every time the server is restarted it deletes the data (Do not use it in production !!)

To save information you only need to create Entities, Repositories and inject them. This is a tutorial on that. If you connect in this way, what you're going to do is lose your mind trying to redo the wheel.

These maven dependencies are also needed in your pom.xml :

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

If all this seems strange to you or you do not find it in your project try to create a new one from here . It's the simplest, fastest and most romantic way to start with spring boot.

    
answered by 29.06.2018 / 00:31
source