Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a simple project to save data to the database. I have successfully mapped the java class with the database table. The code displays no error, it runs very well but it does not save to database, instead it catches an exception and prints out “saving failed”. The database is a mysql database. I have no idea what could be wrong

This is the managed bean code. Save.java(name=”bean”)

Java
//imports
 
@ManagedBean(name="bean")
@RequestScoped
public class Save {
 
    private String username,password,msg;
 
    Session session = null;
    Transaction transaction = null;
 
//GETTERS AND SETTERS
 
    public Save() {
    }
 
    public void saving(){
 
 
    try{
 
         SessionFactory  sessionFactory= new Configuration().configure().buildSessionFactory();
         session = sessionFactory.openSession();
         transaction = session.beginTransaction();
         msg="Saving Record";
         Save sve = new Save();
         sve.setPassword(password);
         sve.setUsername(username);
         session.save(sve);
         transaction.commit();
         session.flush();
         session.close();
         msg="Record Saved";
 
    }
    catch(Exception e){
 
        msg="Saving Failed";
    }               
 
   }
}



This is my Hibernate configuration file

XML
<hibernate-configuration>
  <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/login</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">12345</property>
    <mapping resource="HibernateFiles/Credentials.hbm.xml"/>
  </session-factory>
</hibernate-configuration>
Posted
Updated 2-Nov-14 0:23am
v2
Comments
Richard MacCutchan 2-Nov-14 6:58am    
It is no good catching an exception, then ignoring it and printing a cryptic message that means nothing. Use the information in the exception to help diagnose the problem.
morojele 2-Nov-14 10:59am    
Thank you for the response, i have added printStackTrace() to my catch block.
syed shanu 2-Nov-14 21:29pm    
What exception did you get
Shubhashish_Mandal 5-Nov-14 6:58am    
"msg" also set as null, and if db column mapping for msg is not null then it will failed.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900