Home Jpa Eclipsejpaexamples JPA executeUpdate

Related Tutorials


 
 

Share on Google+Share on Google+

JPA executeUpdate

Advertisement
In this section, we will learn how to use executeUpdate() method in your JPA application.

JPA executeUpdate

     

In this section, we will learn how to use executeUpdate() method in your  JPA application. To use executeUpdate() method in your jpa application for executing a jpa UPDATE/DELETE query. This method returns the number of objects that is changed by to call this method. 

[Note: The executeUpdate() method should not be used with "SELECT" query. ]

Steps to create example code:

  1. Database Table: student
  2. Model Class: Student.java
  3. Main Class: JPAExecuteUpdate.java

Database Table: student

CREATE TABLE `student` ( 
`id` int(11) NOT NULL auto_increment, 
`sname` varchar(100) NOT NULL, 
`sroll` int(11) NOT NULL, 
`scourse` varchar(10) NOT NULL, 
PRIMARY KEY (`id`) 
)

Model Class: Student.java

/**

*/
package roseindia;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

/**
* @author Administrator
*
*/
@Entity
@Table(name="student")

public class Student {

@Id
@GeneratedValue
private int id;

/**
* @return the id
*/
public int getId() {
return id;
}

/**
* @param id the id to set
*/
public void setId(int id) {
this.id = id;
}

@Column(name="sname", length=100,nullable=false)
private String sname;

/**
* @return the sname
*/
public String getSname() {
return sname;
}

/**
* @param sname the sname to set
*/
public void setSname(String sname) {
this.sname = sname;
}

@Column(name="sroll",nullable=false)
private int sroll;

/**
* @return the sroll
*/
public int getSroll() {
return sroll;
}

/**
* @param sroll the sroll to set
*/
public void setSroll(int sroll) {
this.sroll = sroll;
}

@Column(name="scourse",length=10,nullable=false)
private String scourse;

/**
* @return the scourse
*/
public String getScourse() {
return scourse;
}

/**
* @param scourse the scourse to set
*/
public void setScourse(String scourse) {
this.scourse = scourse;
}

}

Main Class: JPAExecuteUpdate.java

/**

*/
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 JPAExecuteUpdate {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

EntityManagerFactory emf=Persistence.createEntityManagerFactory("jpa");
EntityManager em=emf.createEntityManager();
try{
EntityTransaction entr=em.getTransaction();
entr.begin();
Query query = em.createQuery("DELETE FROM Student st WHERE st.sroll= ?1");
query.setParameter(1, 104);
int noOf_delete_record = query.executeUpdate();
System.out.println(noOf_delete_record+" record is deleted");
entr.commit();
}
finally{
em.close();
}
}

}

Output:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).

log4j:WARN Please initialize the log4j system properly.

Hibernate: delete from student where sroll=?

1 record is deleted

 

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

Discuss: JPA executeUpdate  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
Mansi
January 8, 2012
executeUpdate not working.

hi.. i'm trying to insert data to sql from jsp.. but its not inserted.. my code is as below.. pls correct it .. <%-- JSP directive --%> <%@ page import="java.net.*, java.io.*, java.sql.*, java.util.* " %> <%-- JSP scriplet --%> <% String url = "jdbc:oracle:thin:@127.0.0.1:1521:XE" ; Connection con = null; Statement stmt =null; Statement stmt1; ResultSet rs = null; String uname="system"; String passwd="oracle"; String fn=request.getParameter("fname"); String ln=request.getParameter("lname"); String un=request.getParameter("uname"); String pw=request.getParameter("passwd"); String street=request.getParameter("street"); String city=request.getParameter("city"); String state=request.getParameter("state"); String zip=request.getParameter("zip"); String phone=request.getParameter("phone"); String email=request.getParameter("email"); String query; try { Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); con = DriverManager.getConnection(url, uname, passwd); stmt = con.createStatement(); out.println("1"); stmt.executeUpdate("INSERT INTO tcust (fname,lname,uname,pswd,street,city,state,zipcode,phone,email)" + "VALUES('"+fn+"','"+ln+"','"+un+"','"+pw+"','"+street+"','"+city+"','"+state+"','"+zip+"','"+phone+"','"+email+"')"); out.println("2"); out.println("4"); stmt.close(); con.close(); } catch (Exception e) {e.printStackTrace();} %>
DMCA.com