Results 1 to 4 of 4
  1. #1
    vovs is offline Member
    Join Date
    Jul 2010
    Location
    Cherkassy, Ukraine
    Posts
    3
    Rep Power
    0

    Default Servlet getting record from MySQL through hibernate. Exception

    Hi

    I am newbie in Java, and it is my first project.
    I have developed a servlet. This servlet is receiving and inserting record into MySQL through hibernate using eclispe ide.
    I have developed the following code:
    Servlet UserManagerServlet
    Java Code:
    package org.test;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.Iterator;
    import java.util.List;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import org.hibernate.Transaction;
    
    import utils.HibUtil;
    
    public class UserManagerServlet extends HttpServlet {
    	private static final long serialVersionUID = 1L;
           
        public UserManagerServlet() {
            super();
            // TODO Auto-generated constructor stub
        }
    
    	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		
    		try{
    			PrintWriter out = response.getWriter();
    			out.println("<html><head><title>Users Manager</title></head><body>");
    			
    			//Events
    			
    			if ("store".equals(request.getParameter("action"))) {
    				String userName = request.getParameter("userName");
    				String userSurname = request.getParameter("userSurname");
    				if ("".equals(userName) || "".equals(userSurname)) {
    					out.println("<b><i>Please enter user name and user surname.</i></b>");
    				} else {
    						createAndStoreUser(userName, userSurname);
    						out.println("<b><i>Added user</i></b>");
    				}
    			}
    			
    			//Create page
    			printUserForm(out);
    			listUsers(out);
    			
    			//Footer of the page
    			out.println("</body></html>");
    			out.flush();
    
    			
    		} catch (Exception ex) {
    			//if (ServletException.class.isInstance(ex)) {
    			if (ex instanceof ServletException){
    				throw (ServletException) ex;
    			} else {
    				throw new ServletException(ex);
    			}
    		}
    	}
    	
    	private void createAndStoreUser(String userName, String userSurname) {
    		
    		users user = new users();
    		user.setName(userName);
    		user.setSurname(userSurname);
    		HibUtil.getSessionFactory().getCurrentSession().save(user);
    	}
    
    	private void listUsers(PrintWriter out) {
    				
    		//Transaction tx = HibUtil.getSessionFactory().getCurrentSession().beginTransaction();
    		
    		List result = HibUtil.getSessionFactory().getCurrentSession().createCriteria(users.class).list();;
    		
    		for (Iterator iter = result.iterator(); iter.hasNext();){
    			users us = (users) iter.next();
    			out.println(us.getName()+us.getSurname());
    		}
    		//tx.commit();
    		
    	}
    
    	private void printUserForm(PrintWriter out){
    		out.println("<h2>Add new user:</h2>");
    		out.println("<form>");
    		out.println("name: <input name='userName' length='50'/><br/>");
    		out.println("surname: <input name='userSurname' length='50'/><br/>");
    		out.println("<input type='submit' name='action' value='store'/>");
    		out.println("</form>");
    	}
    
    }
    class HibUtil
    Java Code:
    package utils;
    
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.AnnotationConfiguration;
    import org.test.users;
    
    public class HibUtil {
    
    	private static final SessionFactory sessionFactory;
        static {
             try {
                  sessionFactory = new AnnotationConfiguration().addAnnotatedClass(users.class).configure("hibernate.cfg.xml").buildSessionFactory();
             }catch (Exception e) {
                  System.err.println("Initial SessionFactory creation failed." + e);
             throw new ExceptionInInitializerError(e);
             }
        }
        
        public static SessionFactory getSessionFactory() {
             return sessionFactory;
        } 
         
        public static void closeSession(){
             sessionFactory.close();
        }
    
    }
    When I run my code then I find the following exception.

    Java Code:
    javax.servlet.ServletException: Servlet execution threw an exception
    
    
    root cause 
    
    java.lang.NoClassDefFoundError: Could not initialize class utils.HibUtil
    	org.test.UserManagerServlet.listUsers(UserManagerServlet.java:87)
    	org.test.UserManagerServlet.doGet(UserManagerServlet.java:56)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    UserManagerServlet.java:56
    Java Code:
    listUsers(out);
    UserManagerServlet.java:87
    List result =
    Java Code:
    HibUtil.getSessionFactory().getCurrentSession().createCriteria(users.class).list();

    Pleas tell me that where I have done the mistake and how can I resolove the error.
    I am sorry for my English:(

    Note:
    For test i have wrote new class DelMe2 and object arn was succesfull added to MySQL

    Java Code:
    package org.test;
    
    import org.hibernate.Session;
    import org.hibernate.Transaction;
    
    import utils.HibUtil;
    public class DelMe2 {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		Session session = HibUtil.getSessionFactory().getCurrentSession();
    		Transaction tx = null;
    		
    		users arn = new users();
    		arn.setName("test");
    		arn.setSurname("tester");
    		try{
    			tx = session.beginTransaction();
    			session.save(arn);
    			tx.commit();
    		} catch (Exception ex) {
    			if (tx != null){
    				tx.rollback();
    			}
    			System.out.println(ex);
    		}finally{
    			HibUtil.closeSession();	
    		}
    
    		}
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,812
    Rep Power
    25

    Default

    You've done an excellent job of posting your question. The best I've seen!!!

    Sorry I don't know your environment. What I see is that your class file is not being found.
    NoClassDefFoundError: Could not initialize class utils.HibUtil
    Check that the HibUtil class file is in the correct location. It needs to be in the utils folder.
    That could be in a jar file.

  3. #3
    vovs is offline Member
    Join Date
    Jul 2010
    Location
    Cherkassy, Ukraine
    Posts
    3
    Rep Power
    0

    Default

    Norm, thank you for response. I am happy:D

    I work in Eclipse Helios.
    Check that the HibUtil class file is in the correct location. It needs to be in the utils folder.
    That could be in a jar file.
    But test class DelMe2.class worked..It use same class HibUtil
    :confused:
    My project after deploying


    C:\apache-tomcat-6.0.26\webapps\servlet_db\
    |
    ---META-INF
    |...............|
    |................-----MANIFEST.MF
    ---WEB-INF
    |...............|
    |................-----classes
    |...............|...............|
    |...............|................-org
    |...............|...............|.....|
    |...............|...............|......-test
    |...............|...............|...........|
    |...............|...............|...........-UserManagerServlet.class, DelMe2.class, users.class
    |...............|...............|
    |...............|................-utils
    |...............|.....................|
    |...............|......................-HibUtil.class
    |...............|
    |............... ---- lib(*.jars)
    |...............|
    |............... --web.xml
    |
    -index.jsp
    ffuh..
    Last edited by vovs; 07-03-2010 at 04:24 PM.

  4. #4
    vovs is offline Member
    Join Date
    Jul 2010
    Location
    Cherkassy, Ukraine
    Posts
    3
    Rep Power
    0

Similar Threads

  1. Difficulties using Hibernate + MySQL
    By berlindutza in forum Web Frameworks
    Replies: 3
    Last Post: 02-17-2010, 10:23 AM
  2. J2EE = Servlet+Mysql connect
    By initmax in forum Java Servlet
    Replies: 2
    Last Post: 10-30-2009, 10:42 AM
  3. J2EE = Servlet+Mysql connect
    By initmax in forum Java Servlet
    Replies: 2
    Last Post: 10-30-2009, 08:57 AM
  4. hibernate hbm.xml exception
    By sundarjothi in forum Advanced Java
    Replies: 0
    Last Post: 05-31-2008, 08:55 AM
  5. servlet using MySql
    By Peter in forum Java Servlet
    Replies: 2
    Last Post: 07-04-2007, 02:48 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •