• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

im getting this error when i execute the folling code

 
noor harris
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
package roseindia.tutorial.hibernate;
import java.sql.*;

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


/**
* @author Deepak Kumar
*
* http://www.roseindia.net
* Hibernate example to inset data into Contact table
*/

public class FirstExample {
public static void main(String[] args) {
Session session = null;

try{
// This step will read hibernate.cfg.xml and prepare hibernate for use
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();
//Connection con = session.connection();
Transaction t =session.beginTransaction();
//Create new instance of Contact and set values in it by reading them from form object
/* System.out.println("Inserting Record");
Contact contact = new Contact();
contact.setId(6);
contact.setFirstName("Deepak");
contact.setLastName("Kumar");
contact.setEmail("deepak_38@yahoo.com");
session.save(contact);*/
Contact contact = null;
for (int i = 0; i < 2; i++)
{
contact = (Contact)session.get(Contact.class, new Integer(4));
contact.setFirstName("Kiran");
contact.setLastName("A");
contact.setEmail("kiran.rredy@gmail.com");
if(i==1){
contact.setEmail("kiranrredy@hotmail.com");
}
System.out.println(contact.getFirstName() +" "+ contact.getLastName() +" "+contact.getEmail());
session.update(contact);
}
t.commit();
System.out.println("Done");
}catch(Exception e){
e.printStackTrace();
System.out.println(e.getMessage());
}finally{
if(session!=null)
{


// Actual contact insertion will happen at this step
session.flush();
session.close();

}
}
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic