我正在尝试通过休眠连接到Mysql。我可以在mysqlworkbench中以管理员身份创建数据库,但是尝试通过休眠连接时出现以下错误
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)
以下是我的代码 hibernate.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.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:Mysql://127.0.0.1:3306/myschema</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">Hariharan@1604</property>
<property name="show_sql">true</property>
<mapping class="hibernate.Customer" />
</session-factory>
</hibernate-configuration>
HibernateUtil.java
package hibernate;
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;
static
{
try
{
Configuration cfg=new Configuration().configure("config/Hibernate.cfg.xml");
ServiceRegistry serviceRegistry=new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
sessionFactory=cfg.buildSessionFactory();
}
catch(Throwable ex)
{
System.err.println("Initial sessionfactory creationfailed"+ex);
throw new ExceptionInInitializerError();
}
}
public static SessionFactory getSessionFactory()
{
return sessionFactory;
}
}
Icustomer.java
package hibernate;
public interface ICustomer {
public void addCustomer(Customer custDAO);
}
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;
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);
}
}
}
答案 0 :(得分:1)
请注意,MySQL安全方案的127.0.0.1与本地主机不同。
尝试将 hibernate.connection.url 设置为localhost,或授予特定的特权,例如(但不限于此示例):
GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1';
答案 1 :(得分:0)
<property name="hibernate.connection.url">jdbc:Mysql://127.0.0.1:3306/myschema</property>
除了用户名和密码正确外,您确定架构名称为myschema
吗?另外,M
ysql的大M可能是个问题。
答案 2 :(得分:0)
root用户没有特权。因此通过以下查询更改了权限
UPDATE mysql.user SET Grant_priv='Y', Super_priv='Y' WHERE User='root';
FLUSH PRIVILEGES;
GRANT ALL ON *.* TO 'root'@'localhost';
关闭我的连接,然后再次打开。