14
respostas

Unknown database 'financas'

Antes funcionava perfeitamente, hoje resolveu não funcionar. O banco financas está criado. todos os jars:

/financas/lib/antlr-2.7.7.jar /financas/lib/byte-buddy-1.8.17.jar /financas/lib/classmate-1.3.4.jar /financas/lib/dom4j-2.1.1.jar /financas/lib/hibernate-commons-annotations-5.0.4.Final.jar /financas/lib/hibernate-core-5.3.7.Final.jar /financas/lib/jandex-2.0.5.Final.jar /financas/lib/javassist-3.23.1-GA.jar /financas/lib/javax.activation-api-1.2.0.jar /financas/lib/javax.persistence-api-2.2.jar /financas/lib/jboss-logging-3.3.2.Final.jar /financas/lib/jboss-transaction-api_1.2_spec-1.1.1.Final.jar /financas/lib/mysql-connector-java-8.0.13.jar

PERSISTENCE.XML

<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0">
 <!--  unidade de persistencia com o nome financas  -->
<persistence-unit name="financas">
 <!--  Implementação do JPA, no nosso caso Hibernate  -->
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
 <!--  Aqui são listadas todas as entidades  -->
<class>financas.modelo.Conta</class>
<class>financas.modelo.Movimentacao</class>
<class>financas.modelo.Categoria</class>
<class>financas.modelo.Cliente</class>
<properties>
 <!--  Propriedades JDBC  -->
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/financas?useTimezone=true&serverTimezone=UTC"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="password"/>
 <!--  Configurações específicas do Hibernate  -->
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
</properties>
</persistence-unit>
</persistence>
14 respostas
nov 20, 2018 3:53:20 PM org.hibernate.jpa.internal.util.LogHelper logPersistenceUnitInformation
INFO: HHH000204: Processing PersistenceUnitInfo [
    name: financas
    ...]
nov 20, 2018 3:53:20 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.3.7.Final}
nov 20, 2018 3:53:20 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
nov 20, 2018 3:53:20 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.4.Final}
nov 20, 2018 3:53:21 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
nov 20, 2018 3:53:21 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/financas?useTimezone=true&serverTimezone=UTC]
nov 20, 2018 3:53:21 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=root, password=****}
nov 20, 2018 3:53:21 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
nov 20, 2018 3:53:21 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
nov 20, 2018 3:53:21 PM org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator initiateService
WARN: HHH000342: Could not obtain connection to query metadata : null
nov 20, 2018 3:53:21 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect
nov 20, 2018 3:53:21 PM org.hibernate.engine.jdbc.env.internal.LobCreatorBuilderImpl makeLobCreatorBuilder
INFO: HHH000422: Disabling contextual LOB creation as connection was null
nov 20, 2018 3:53:21 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 1049, SQLState: 42000
nov 20, 2018 3:53:21 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Unknown database 'financas'
Exception in thread "main" java.lang.ExceptionInInitializerError
    at financas.teste.TesteConta.main(TesteConta.java:29)
Caused by: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:275)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:237)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:214)
    at org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory.injectServices(DefaultIdentifierGeneratorFactory.java:152)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.injectDependencies(AbstractServiceRegistryImpl.java:286)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:243)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:214)
    at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.<init>(InFlightMetadataCollectorImpl.java:179)
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:119)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:904)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:935)
    at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:56)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:79)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
    at financas.util.JPAutil.<clinit>(JPAutil.java:8)
    ... 1 more
Caused by: org.hibernate.exception.SQLGrammarException: Error calling Driver#connect
    at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:63)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:113)
    at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.convertSqlException(BasicConnectionCreator.java:118)

Hum se ele funcionava ontem e hoje não mais é problema de infraestrutura. Coisas triviais mas tenta dar rebuild no projeto e reiniciar o computador. Houve alguma atualização da sua IDE ou das dependências do seu projeto?

Olá Matheus,

Observe essa linha:

com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. 

Tente Mudar:

<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>

Para:

<property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/>

Ainda não deu certo

Bom dia Matheus, qual é a versão do mysql?

Tente Utilizar da Seguinte forma:

Class do Driver

"com.mysql.cj.jdbc.Driver"

A URL:

"jdbc:mysql://localhost:port/bd_name?useTimezone=true&serverTimezone=UTC";

A versão é a 8.0.13, testei com a 5.1.24 e também não deu certo. Testei a conexão sem utilizar o hibernate e deu certo, mas eu queria utilizar o hibernate. Botei a porta:3306 e também não deu certo

Este é o resto do erro

at org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:41)
    at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.createConnection(BasicConnectionCreator.java:58)
    at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections.addConnections(DriverManagerConnectionProviderImpl.java:363)
    at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections.<init>(DriverManagerConnectionProviderImpl.java:282)
    at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections.<init>(DriverManagerConnectionProviderImpl.java:260)
    at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections$Builder.build(DriverManagerConnectionProviderImpl.java:401)
    at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.buildPool(DriverManagerConnectionProviderImpl.java:112)
    at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:75)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:100)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:246)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:214)
    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.buildJdbcConnectionAccess(JdbcEnvironmentInitiator.java:145)
    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:66)
    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:94)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:263)
    ... 15 more
Caused by: java.sql.SQLSyntaxErrorException: Unknown database 'financas'
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:835)
    at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:455)
    at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240)
    at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:207)
    at org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:38)
    ... 30 more

Eu poderia simplemente dizer que será que existe o db financas ? Mas acho que não é isso.

Estou sem ideias, preciso estudar mais sobre esse assunto. Algum instrutor poderia nos ajudar a chegar nessa solução?

eu acho que eles deveriam atualizar o curso, a versao que eles usam nem está mais lá no site deles (https://bintray.com/hibernate/artifacts/hibernate-orm/5.3.7.Final), pra quem seguiu o plano de ensino da formação java e foi fazer com o maven, de ctz que deu errado, olha o que aparece no meu

{

Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:exec (default-cli) on project JPATeste: Command execution failed. Process exited with an error: 1 (Exit value: 1) -> [Help 1]

To see the full stack trace of the errors, re-run Maven with the -e switch. Re-run Maven using the -X switch to enable full debug logging.

For more information about the errors and possible solutions, please read the following articles: [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

}

Descobri uma parte do problema, ele está no banco de dados mysql, ele somente conecta a bancos que já estavam criados, se eu criar um novo dá esse erro. Porém eu consigo inserir, criar tabelas, normalmente manualmente nos novos. Quando eu conecto a um banco que já estava lá o fj21 por exemplo, a conexão retorna com.mysql.cj.jdbc.ConnectionImpl@3796751b mas quando eu mudo o banco para qualquer outro retorna o erro

Exception in thread "main" java.sql.SQLSyntaxErrorException: Unknown database 'fj_financas'
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:835)
    at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:455)
    at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240)
    at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:207)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at teste.Teste.main(Teste.java:16)

Eis os banco de dados:

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| fj21               |
| fj_financas        |
| information_schema |
| mysql              |
| performance_schema |
| sakila             |
| sys                |
| world              |
+--------------------+
8 rows in set (0.00 sec)

OBS: Eu deletei o financas que estava criado anteriormente.

Você não está conseguindo criar novos bancos?

Eu consigo criar, porém quando tento conectar a ele utilizando o connect mysql ocorre o erro que postei agora a pouco. Exemplificando, se eu conectar ao banco de dados fj21, mysql, ou qualquer outro que já estava criado antes dessa bagunça começar, NÃO retorna erro, se eu conectar a outro banco novo qualquer que eu criar vai ocorrer o erro.