为什么我会收到“MySQLSyntaxErrorException:Access denied for user:”错误?

时间:2015-05-14 12:11:29

标签: java mysql spring hibernate jpa

当我运行应用程序时,我有堆栈跟踪中提到的错误。

  

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:Accèsrefusépourl'utilisateur:''@'@ localhost'。基础'db_hajar'

(我正在使用弹簧靴)。 你能帮忙吗?

这是我正在使用的数据库类:

package demo.entities;

import java.io.Serializable;
import java.util.Collection;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;

@Entity
public class Database implements Serializable {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)

    private int id_database;
    private String database_name;
    @OneToMany
    @JoinColumn(name="id_entity")
    private Collection<entities>entities;
    public int getId_database() {
        return id_database;
    }
    public void setId_database(int id_database) {
        this.id_database = id_database;
    }
    public String getDatabase_name() {
        return database_name;
    }
    public void setDatabase_name(String database_name) {
        this.database_name = database_name;
    }
    public Database() {
        super();
        // TODO Auto-generated constructor stub
    }
    public Database(int id_database, String database_name) {
        super();
        this.id_database = id_database;
        this.database_name = database_name;
    }
    @Override
    public String toString() {
        return "Database [id_database=" + id_database + ", database_name="
                + database_name + "]";
    }




}

这是实体的类:

package demo.entities;

import java.io.Serializable;
import java.util.Collection;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

@Entity
public class entities  implements Serializable {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)

    private int id_entity;
    private String entity_name;
    @ManyToOne
    @JoinColumn(name="id_database")
    private Database database;
    public int getId_entity() {
        return id_entity;
    }
    public void setId_entity(int id_entity) {
        this.id_entity = id_entity;
    }
    public String getEntity_name() {
        return entity_name;
    }
    public void setEntity_name(String entity_name) {
        this.entity_name = entity_name;
    }
    public entities() {
        super();
        // TODO Auto-generated constructor stub
    }
    public entities(int id_entity, String entity_name) {
        super();
        this.id_entity = id_entity;
        this.entity_name = entity_name;
    }
    @Override
    public String toString() {
        return "entities [id_entity=" + id_entity + ", entity_name="
                + entity_name + "]";
    }

}

以下是应用程序属性设置:

# DataSourcesettings:

 spring.datasource.url= jdbc:mysql://localhost:3306/db_hajar
 spring.datasource.username= root 
 spring.datasource.password= 
 spring.datasource.driverClassName= com.mysql.jdbc.Driver

# Specifythe DBMS #

 spring.jpa.database = MYSQL 

 # Show or not log for each sqlquery 

 spring.jpa.show-sql = true 

 # Hibernateddlauto (create, create-drop, update)
  spring.jpa.hibernate.ddl-auto = update
  # Naming strategy 
  spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
  spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

堆栈跟踪:

    com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Accès refusé pour l'utilisateur: ''@'@localhost'. Base 'db_hajar'
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)
        at com.mysql.jdbc.Util.handleNewInstance(Util.java:377)
        at com.mysql.jdbc.Util.getInstance(Util.java:360)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:978)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3887)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3823)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:870)
        at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1659)
        at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1206)
        at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2234)
        at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2265)
        at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2064)
        at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:790)
        at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)
        at com.mysql.jdbc.Util.handleNewInstance(Util.java:377)
        at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:395)
        at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:325)
        at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:307)
        at org.apache.tomcat.jdbc.pool.PooledConnection.connect(PooledConnection.java:200)
        at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:699)
        at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:633)
        at org.apache.tomcat.jdbc.pool.ConnectionPool.init(ConnectionPool.java:484)
        at org.apache.tomcat.jdbc.pool.ConnectionPool.<init>(ConnectionPool.java:142)
        at org.apache.tomcat.jdbc.pool.DataSourceProxy.pCreatePool(DataSourceProxy.java:115)
        at org.apache.tomcat.jdbc.pool.DataSourceProxy.createPool(DataSourceProxy.java:102)
        at org.apache.tomcat.jdbc.pool.DataSourceProxy.getConnection(DataSourceProxy.java:126)
        at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:139)
        at org.hibernate.tool.hbm2ddl.SuppliedConnectionProviderConnectionHelper.prepare(SuppliedConnectionProviderConnectionHelper.java:51)
        at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:219)
        at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:203)
        at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:522)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1859)
        at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:852)
        at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:845)
        at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:398)
        at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:844)
        at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60)
        at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:343)
        at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:318)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1633)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
        at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:956)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:747)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
        at demo.DemoJpaApplication.main(DemoJpaApplication.java:10)

1 个答案:

答案 0 :(得分:1)

你的问题与mySQL有关。您只需要通过Java错误堆栈跟踪。但是,由于我最近才有一些Java经验,所以我学会了如何阅读一些堆栈跟踪错误。从下往上阅读,顶线是真正的答案。

基于那个金块:

顶部消息:“com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:Accèsrefusépourl'utilisateur:''@'@ localhost'.Base'db_hajar'”...

翻译法语:“拒绝用户”。您几乎可以得到答案:名为“db_hajar”的数据库或表没有搜索或查询权限。我很确定这不是你的代码问题,而是mySQL中的数据库或表权限。

您需要以root身份进入mySQL,并且需要GRANT访问特定用户。 请参阅:https://dev.mysql.com/doc/refman/5.5/en/privilege-system.html

旁边:我希望这台机器没有互联网访问权限或者有防火墙(阻塞端口3306),因为你的mySQL运行时使用根级别凭据而代码中没有密码。哎呀!我建议您设置root密码(不要将其用于查询),创建用户和密码,并为其提供对相关数据库或表的完全访问权限,并在Java程序中提供这些凭据。一旦您开始工作,请避免在代码中放置静态凭据,而是学习如何使用证书或安全保存凭据的凭证管理系统来使用Java,并在需要进行身份验证时进行检索。

希望这有帮助!