Home Jpa Jpacrud JPA delete data
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

JPA delete data

Advertisement
In this section, you will learn how to delete data from database.

JPA delete data

     

In this section, you will learn how to delete data from database. You follow the  following steps:

Create a "JPADelete" class file.

Put the following text in "JPADelete.java" file.

JPADelete.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package roseindia;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
*
* @author Administrator
*/
public class JPADelete {
public static void main(String arg[]){
EntityManagerFactory emf=Persistence.createEntityManagerFactory("netjpa");
EntityManager em=emf.createEntityManager();
try{
EntityTransaction entr=em.getTransaction();
entr.begin();
Query query=em.createQuery("DELETE FROM Student st WHERE st.id= ?1");
query.setParameter(1, 1);
int deleteRecord=query.executeUpdate();
entr.commit();
System.out.println(deleteRecord+" are deleted.");
}
catch(Exception ex){
System.out.println(ex.getMessage());
}
finally{
em.close();
}
}
}
Run it and get the following output.

Go in MySQL and check it.
Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 10, 2009

Related Tutorials

Ask Questions?    Discuss: JPA delete data  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com