Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 不能';t使用mysqlworkbench作为管理员从hibernate连接到mysql_Java_Hibernate_Mysql Workbench - Fatal编程技术网

Java 不能';t使用mysqlworkbench作为管理员从hibernate连接到mysql

Java 不能';t使用mysqlworkbench作为管理员从hibernate连接到mysql,java,hibernate,mysql-workbench,Java,Hibernate,Mysql Workbench,我正在尝试通过hibernate连接到Mysql。我可以在mysqlworkbench中以管理员的身份创建数据库,但在尝试通过hibernate连接时,我遇到了以下错误 ERROR: Access denied for user 'root'@'localhost' (using password: YES) Initial sessionfactory creationfailedorg.hibernate.service.spi.ServiceException: Unable to cre

我正在尝试通过hibernate连接到Mysql。我可以在mysqlworkbench中以管理员的身份创建数据库,但在尝试通过hibernate连接时,我遇到了以下错误

ERROR: Access denied for user 'root'@'localhost' (using password: YES)
Initial sessionfactory creationfailedorg.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
Exception in thread "main" java.lang.ExceptionInInitializerError
    at hibernate.HibernateUtil.<clinit>(HibernateUtil.java:21)
    at hibernate.CustomerDAO.addCustomer(CustomerDAO.java:14)
    at hibernate.ClientDemo.main(ClientDemo.java:20)
Icustomer.java

package hibernate;

public interface ICustomer {
    public void addCustomer(Customer custDAO);


}
package hibernate;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="CUSTOMER")
public class Customer {
    @Id

    private int customerId;
    private String customerName;

    public Customer()
    {}
    public int getCustomerId() {
        return customerId;
    }
    public void setCustomerId(int customerId) {
        this.customerId = customerId;
    }
    public String getCustomerName() {
        return customerName;
    }
    public void setCustomerName(String customerName) {
        this.customerName = customerName;
    }
}
CustomerDAO.java

package hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class CustomerDAO implements ICustomer
{

    @Override
    public void addCustomer(Customer custDAO) {
        // TODO Auto-generated method stub
        Session session=HibernateUtil.getSessionFactory().openSession();
        Transaction tx=session.beginTransaction();
        session.save(tx);
        tx.commit();
        session.close();

    }

}
Customer.java

package hibernate;

public interface ICustomer {
    public void addCustomer(Customer custDAO);


}
package hibernate;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="CUSTOMER")
public class Customer {
    @Id

    private int customerId;
    private String customerName;

    public Customer()
    {}
    public int getCustomerId() {
        return customerId;
    }
    public void setCustomerId(int customerId) {
        this.customerId = customerId;
    }
    public String getCustomerName() {
        return customerName;
    }
    public void setCustomerName(String customerName) {
        this.customerName = customerName;
    }
}
ClientDemo.java

package hibernate;
import java.util.Scanner;
import org.hibernate.HibernateException;
public class ClientDemo {
    public static void main(String[] args) {
        CustomerDAO custdao=new CustomerDAO();
        try
        {
            System.out.println("Create");
            System.out.println("Enter the data");
            Scanner sc=new Scanner(System.in);
            System.out.println("ID");
            int id=sc.nextInt();
            System.out.println("Name");
            String str=sc.next();
            Customer cust=new Customer();
            cust.setCustomerId(id);
            cust.setCustomerName(str);
            custdao.addCustomer(cust);
            System.out.println("One record created");
            sc.close();

        }
        catch (HibernateException e) {
            // TODO: handle exception
            System.out.println(e);
        }
    }
}
这是我的连接详细信息

数据库截图

jdbc:Mysql://127.0.0.1:3306/myschema

除了用户名和密码的正确性之外,您是否确定您的架构名称是
myschema
?另外,ysql中的大M可能是一个问题。

请注意,127.0.0.1与localhost for MySQL安全方案不同


尝试将hibernate.connection.url设置为localhost,或授予特定权限,例如(但不限于此示例):


将**上的所有权限授予'root'@'127.0.0.1'

根用户没有此权限。所以通过下面的查询更改了权限

UPDATE mysql.user SET Grant_priv='Y', Super_priv='Y' WHERE User='root';
FLUSH PRIVILEGES;
GRANT ALL ON *.* TO 'root'@'localhost';

关闭我的连接并再次打开。

当在hibernate属性文件中传递的密码与数据库密码不匹配时,通常会出现此问题。请重新验证您的密码。您能否提供“用户”表的输出我在尝试运行上述10:21:41授予所有权限时出错。至“root”@“127.0.0.1”错误代码:1410。您不允许创建授予0.037秒权限的用户将hibernate.connection.url设置为localhost我收到相同的错误我相信您的根用户可能没有授予权限。请检查这个线程:是的,将Mysql更改为Mysql,是的,我的模式名是myschemado,数据库和应用程序服务器都在同一台机器上?
UPDATE mysql.user SET Grant_priv='Y', Super_priv='Y' WHERE User='root';
FLUSH PRIVILEGES;
GRANT ALL ON *.* TO 'root'@'localhost';