Issue
I'm fairly new to using hibernate and I'm trying to create a webapp with hibernate as the backend but I'm running into an error I can't seem to figure out how to solve given the current configuration of my code. Any help would be appreciated
I have added my hibernate.cfg.xml configuration into the hibernateutil java class but i can't seem to get my configuration to read data from my database and keep getting
org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
caused by another error: Caused by: org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [com.mysql.jdbc.Driver]
and a ClassNotFoundException
again to the mysql driver
i thought i configured my class properly but i can't seem to get it to work, here is my code where the error starts
public class HibernateUtil {
private static SessionFactory sessionFactory;
public static SessionFactory getSessionFactory() {
if (sessionFactory == null) {
try {
Configuration configuration = new Configuration();
Properties settings = new Properties();
// Hibernate settings equivalent to hibernate.cfg.xml's properties
settings.put(Environment.DRIVER, "com.mysql.jdbc.Driver");
settings.put(Environment.URL, "jdbc:mysql://localhost:3308/demo");
settings.put(Environment.USER, "user");
settings.put(Environment.PASS, "password");
settings.put(Environment.DIALECT, "org.hibernate.dialect.MySQL5Dialect");
settings.put(Environment.SHOW_SQL, "true");
settings.put(Environment.CURRENT_SESSION_CONTEXT_CLASS, "thread");
settings.put(Environment.HBM2DDL_AUTO, "create-drop");
configuration.setProperties(settings);
configuration.addAnnotatedClass(Student.class);
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
} catch (Exception e) {
e.printStackTrace();
}
}
return sessionFactory;
}
}
here is my DAO class
public class StudentDAO {
public void saveStudent(Student student) {
Transaction transaction = null;
try {
Session session = HibernateUtil.getSessionFactory().openSession();
transaction = session.beginTransaction();
session.save(student);
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
}
}
public void updateStudent(Student student) {
Transaction transaction = null;
try {
Session session = HibernateUtil.getSessionFactory().openSession();
transaction = session.beginTransaction();
session.saveOrUpdate(student); //student object updated
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
}
}
public Student getStudent(int id) {
Transaction transaction = null;
Student student = null;
try {
Session session = HibernateUtil.getSessionFactory().openSession();
transaction = session.beginTransaction();
student = session.get(Student.class, id); //get student object by id
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
}
return student;
}
@SuppressWarnings("unchecked")
public List<Student> getAllStudents() {
Transaction transaction = null;
List<Student> students = null;
try {
Session session = HibernateUtil.getSessionFactory().openSession();
transaction = session.beginTransaction();
students = session.createQuery("from student").list(); //get all student objects
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
}
return students;
}
@SuppressWarnings("null")
public void deleteStudent(int id) {
Transaction transaction = null;
Student student = null;
try {
Session session = HibernateUtil.getSessionFactory().openSession();
student = session.get(Student.class, id);
if (student != null) {
session.delete(student);
System.out.println(student + "has been deleted");
}
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
}
}
}
and here is my servlet class
@WebServlet("/")
public class StudentServlet extends HttpServlet {
private static final long serialVersionUID = 2L;
private StudentDAO studentDao;
String home = "/Week05";
public StudentServlet() {
this.studentDao = new StudentDAO();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String sPath = request.getServletPath();
//switch statement to call appropriate method
switch (sPath) {
case "/new":
try {
showNewForm(request, response);
} catch (ServletException | IOException e) {
e.printStackTrace();
}
break;
case "/insert":
try {
insertStudent(request, response);
} catch (SQLException | IOException e) {
e.printStackTrace();
}
break;
case "/delete":
try {
deleteStudent(request, response);
} catch (SQLException | IOException e) {
e.printStackTrace();
}
break;
case "/update":
try {
updateStudent(request, response);
} catch (SQLException | IOException e) {
e.printStackTrace();
}
break;
case "/edit":
try {
editStudent(request, response);
} catch (ServletException | IOException e) {
e.printStackTrace();
}
break;
default:
try {
listAllStudents(request, response); //home page = .../week04/StudentServlet
} catch (ServletException | IOException | SQLException e) {
e.printStackTrace();
}
break;
}
}
// functions to fetch data from studentDao and display data on appropriate jsp
private void listAllStudents(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, SQLException {
List<Student> allStudents = studentDao.getAllStudents();
request.setAttribute("listStudents", allStudents);
RequestDispatcher dispatch = request.getRequestDispatcher("index.jsp"); //home page week04/StudentServlet | list all objects from table
dispatch.forward(request, response);
}
private void showNewForm(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
RequestDispatcher dispatch = request.getRequestDispatcher("student-form.jsp");
dispatch.forward(request, response);
}
private void insertStudent(HttpServletRequest request, HttpServletResponse response)
throws SQLException, IOException{
String firstname = request.getParameter("firstname");
String lastname = request.getParameter("lastname");
String email = request.getParameter("email");
Student newStudent = new Student(firstname, lastname, email);
studentDao.saveStudent(newStudent); //student object inserted to table
response.sendRedirect(home); //redirect to home page
}
private void deleteStudent(HttpServletRequest request, HttpServletResponse response)
throws SQLException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
studentDao.deleteStudent(id); //student object deleted
response.sendRedirect(home);
}
private void updateStudent(HttpServletRequest request, HttpServletResponse response)
throws SQLException, IOException{
int id = Integer.parseInt(request.getParameter("id"));
String firstname = request.getParameter("firstname");
String lastname = request.getParameter("lastname");
String email = request.getParameter("email");
Student updateStudent = new Student(id, firstname, lastname, email);
studentDao.updateStudent(updateStudent); //student object updated
response.sendRedirect(home);
}
private void editStudent(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
// String firstname = request.getParameter("firstname");
// String lastname = request.getParameter("lastname");
// String email = request.getParameter("email");
Student currentStudent = studentDao.getStudent(id);
RequestDispatcher dispatch = request.getRequestDispatcher("student-form.jsp"); //student form called with current student info loaded
request.setAttribute("student", currentStudent);
dispatch.forward(request, response);
}
}
Any help on where I'm going wrong would be appreciated as I truly can't figure what exactly to do, it might be a mapping error issue but i assumed all necessary mapping was covered on the servlet.
Solution
@Alpheus Your project can't find out your driver's dependency, because you haven't put dependency on build path.
Answered By - Ganesh Kumar