0

I am trying to connect to Mysql through hibernate. I could create database in mysqlworkbench as admin but while trying to connect through hibernate I get the below error

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)

The following is my code 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);
        }
    }
}

This is my connection details enter image description here

Database screenshot enter image description here

Aarthi Ananth
  • 460
  • 1
  • 4
  • 16

3 Answers3

1

Note that 127.0.0.1 is different from localhost for MySQL security scheme.

Try setting hibernate.connection.url to localhost, or grant specific privileges, such as(but not restricted to this example):

GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1';

Diogo Silvério
  • 334
  • 3
  • 7
  • I get an error when I try to run the above 10:21:41 GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1' Error Code: 1410. You are not allowed to create a user with GRANT 0.037 sec – Aarthi Ananth Sep 19 '18 at 04:52
  • setting hibernate.connection.url to localhost I get the same error – Aarthi Ananth Sep 19 '18 at 04:54
  • 1
    I believe your root user might not have grant privileges. Please, check this thread: https://stackoverflow.com/questions/19237475/cannot-grant-privileges-to-mysql-database – Diogo Silvério Sep 19 '18 at 05:00
0
 <property name="hibernate.connection.url">jdbc:Mysql://127.0.0.1:3306/myschema</property>

Other than correctness of username and password, are you sure that your schema name is myschema? Also big M of Mysql might be a problem.

yılmaz
  • 1,818
  • 13
  • 15
0

root user did not have the privilege. so changed the permission by below query

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

closed my connection and opened again.

Aarthi Ananth
  • 460
  • 1
  • 4
  • 16