Jelo 0 Newbie Poster

I have been trying two save data into mysql database using hibernate but it have been giving me an error. Could someone give a solution to this. Following is the servlet code and hibernate configuration

servlet
package ac.ke.schoolsystem.controllers;

import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import ac.ke.schoolsystem.enumerators.ClassEnum;
import ac.ke.schoolsystem.enumerators.Gender;
import entity.ClassBean;

/**
 * Servlet implementation class AddClass
 */
@WebServlet(name="AddClass", urlPatterns="/add_class")
public class AddClass extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddClass() {
        super();

    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //request.setAttribute("class_name", "");
        request.setAttribute("streams", "");
        request.setAttribute("capacity", "");
        request.setAttribute("teacher", "");


        RequestDispatcher view = request.getRequestDispatcher("WEB-INF/views/add_class.jsp");

        view.forward(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setAttribute("errors", false);
        Configuration cfg = new Configuration();
        cfg.configure("hibernate.cfg.xml");
        SessionFactory sf  = cfg.buildSessionFactory();
        Session ss  = sf.openSession();
        Transaction tx= ss.beginTransaction();


        ClassBean c = new ClassBean();

        // Students details
        String class_name = request.getParameter("class_name");
        c.setClassName(ClassEnum.valueOf(class_name));

        String streams = request.getParameter("streams");
        if (streams.length() == 0) {
            request.setAttribute("errors", true);
            request.setAttribute("stream error", true);
            request.setAttribute("streams", "");

        } else {
            c.setStreams(Integer.parseInt(streams));
            request.setAttribute("streams", streams);
        }

        String capacity = request.getParameter("capacity");
        if (capacity.length() == 0) {
            // System.out.println("Empty last name");
            request.setAttribute("errors", true);
            request.setAttribute("capacity error", true);
            request.setAttribute("capacity", "");

        } else {
            c.setCapacity(Integer.parseInt(capacity));
            request.setAttribute("capacity", capacity);
        }


        String teacher = request.getParameter("teacher");
        if (teacher.length() == 0) {
            // System.out.println("Empty Address Field");
            request.setAttribute("errors", true);
            request.setAttribute("teacher error", true);
            request.setAttribute("teacher", "");
        } else {
            c.setTeacher(teacher);
            request.setAttribute("teacher", teacher);
        }

        if ((Boolean) request.getAttribute("errors")) {
            RequestDispatcher view = request.getRequestDispatcher("WEB-INF/views/add_class.jsp");
            view.forward(request, response);
        } else {
            ServletContext sc = this.getServletContext();
            synchronized (this){
                ArrayList<Class> classesList = (ArrayList<Class>) sc.getAttribute("classes");

                //System.out.println(student);
            //  classesList.add(classes); 
                sc.setAttribute("classes", classesList);
            }

            c.setClassName(ClassEnum.valueOf(class_name));
            c.setStreams(Integer.parseInt(streams));
            c.setCapacity(Integer.parseInt(capacity));


            ss.save(c);

            ss.flush(); 
            tx.commit();
            ss.close();

            RequestDispatcher view = request.getRequestDispatcher("WEB-INF/views/add_class.jsp");
            view.forward(request, response);
        }
    }
}



    hibernate.cfg.xm
    <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/schoolsystem</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password"></property>

        <!-- JDBC connection pool (use the built-in -->
        <property name="connection.pool_size">1</property>

        <!-- autocommit false --> 
        <property name="hibernate.connection.autocommit">false</property>

        <!-- SQL dialect -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- display all executed SQL to sysout -->
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        <property name="use_sql_comments">true</property>

        <!-- for jdbc transaction -->
        <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">create</property>

         <!-- Map Entity Class -->
        <mapping resource="Student.hbm.xml"></mapping>
        <mapping resource="Parent.hbm.xml"></mapping>
        <mapping resource="Subject.hbm.xml"></mapping>
        <mapping resource="Teacher.hbm.xml"></mapping>
        <mapping resource="ClassBean.hbm.xml"></mapping>
    </session-factory>
</hibernate-configuration>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.