In my Java web project the mysql driver does not work when using the dependency in the pom.xml file

Asked

Viewed 40 times

0

All right, here’s the deal. I am doing a java web project and when I add mysql-Connector-java as dependency within the pom.xml folder, the project does not communicate with the database.

However, what’s funny is that when I lower the mysql . jar and add it in the build path, then yes it communicates with the database normally. But I need the project to make the connection as a database using the dependency in the pom.xml file to not give problem there in front.

I am using Hibernate for connection to the bank

Folder pom.xml:

<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>br.pro.delfino.drogaria</groupId>
    <artifactId>Drogaria</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <!-- Codificação dos caracteres -->
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <!-- Parâmetros de execução -->
    <build>
        <!-- Nome do projeto empacotado -->
        <finalName>Drogaria</finalName>

        <!-- Plugins -->
        <plugins>
            <!-- Compilador -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                

                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                    <source>1.8</source>
                    <target>1.8</target>
                    
                </configuration>
                
            </plugin>
        </plugins>
    </build>

    <!-- Dependências -->

    <dependencies>
        <!-- Hibernate Core -->

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.3.11.Final</version>
        </dependency>


        <!-- MySQL dependencia --> 
        
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.15</version>
        </dependency>
        
        <!-- junit -->
        
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13</version>
            <scope>test</scope>
        </dependency>
        
        <!-- JSF -->
        <dependency>
            <groupId>org.glassfish</groupId>
            <artifactId>javax.faces</artifactId>
            <version>2.2.20</version>

        </dependency>
        
         <!-- primefaces -->
        <dependency>
            <groupId>org.primefaces</groupId>
            <artifactId>primefaces</artifactId>
            <version>7.0</version>
        </dependency>
        
        <!--OmniFaces: https://mvnrepository.com/artifact/org.omnifaces/omnifaces --> 
        <dependency>
            <groupId>org.omnifaces</groupId>
            <artifactId>omnifaces</artifactId>
            <version>1.13</version>
        </dependency>
        

    </dependencies>
</project>

Pasta Hibernate.cfg:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
    
        <!-- Configurações de conexão com o banco de dados -->
        <property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/drogaria</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">xyc2dy9t</property>
        
        
        <!-- Pool de Conexões -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>

        <!-- Gerenciamento do Contexto das Sessões -->
        <property name="current_session_context_class">thread</property>

        <!-- Cache de Segundo Nível -->
        <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

        <!-- Mostra as SQLs Geradas -->
        <property name="show_sql">true</property>

        <!-- Cria as tabelas do banco de dados -->
        <property name="hbm2ddl.auto">validate</property>
        
        <!-- Mapeamento das entidades -->
        <mapping class="br.pro.delfino.drogaria.domain.Cidade" />
        <mapping class="br.pro.delfino.drogaria.domain.Cliente" />
        <mapping class="br.pro.delfino.drogaria.domain.Estado" />
        <mapping class="br.pro.delfino.drogaria.domain.Fabricante" />
        <mapping class="br.pro.delfino.drogaria.domain.Funcionario" />
        <mapping class="br.pro.delfino.drogaria.domain.ItemVenda" />
        <mapping class="br.pro.delfino.drogaria.domain.Pessoa" />
        <mapping class="br.pro.delfino.drogaria.domain.Produto" />
        <mapping class="br.pro.delfino.drogaria.domain.Usuario" />
        <mapping class="br.pro.delfino.drogaria.domain.Venda" />
        
    </session-factory>
</hibernate-configuration>

I am using the Hibernate class to generate the connection as follows:

package br.pro.delfino.drogaria.util;

import org.hibernate.SessionFactory;
    
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class HibernateUtil {
    private static SessionFactory fabricaDeSessoes = criarFabricaDeSessoes();
    
    public static SessionFactory getFabricaDeSessoes() {
        return fabricaDeSessoes;
    }
    
    private static SessionFactory criarFabricaDeSessoes() {
        try {
            Configuration configuracao = new Configuration().configure();
        
            ServiceRegistry registro = new StandardServiceRegistryBuilder().applySettings(configuracao.getProperties()).build();
        
            SessionFactory fabrica = configuracao.buildSessionFactory(registro);
        
            return fabrica;
        } catch (Throwable ex) {
            System.err.println("A fábrica de sessões não pode ser criada " + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }
}
 
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.