Arquivo cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">12345</property>
<property name="connection.url">jdbc:mysql://localhost:3306/vendaspecas</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">false</property>
<property name="hbm2ddl.auto">update</property>
<!-- Livro -->
<mapping class="br.com.VendasPecas.domain.Fornecedor" />
<mapping class="br.com.VendasPecas.domain.Funcionario" />
<mapping class="br.com.VendasPecas.domain.Produto" />
</session-factory>
</hibernate-configuration>
Classe HibernateUtil
package br.com.VendasPecas.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 final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
// Cria uma conexão a partir do hibernate.cfg.xml
Configuration configuration = new Configuration();
configuration.configure();
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
return sessionFactory;
//return new Configuration().configure().buildSessionFactory(
// new StandardServiceRegistryBuilder().build() );
}
catch (Throwable ex) {
// Mensagem de erro ao conectar
System.err.println("Erro na conexão." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
Classe GerarTabelas
package br.com.VendasPecas.main;
import br.com.VendasPecas.util.HibernateUtil;
public class GerarTabela {
public static void main(String[] args) {
HibernateUtil.getSessionFactory();
HibernateUtil.getSessionFactory().close();
}
}
Erro na saida (console)
Erro na conexão.org.hibernate.exception.JDBCConnectionException: Error calling Driver#connect Exception in thread "main" java.lang.ExceptionInInitializerError at br.com.VendasPecas.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:34) at br.com.VendasPecas.util.HibernateUtil.(HibernateUtil.java:10) at br.com.VendasPecas.main.GerarTabela.main(GerarTabela.java:9) Caused by: org.hibernate.exception.JDBCConnectionException: Error calling Driver#connect at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:132)