Creating and running the JPA CRUD application

In this section we will develop the project articles for JPA application using NetBeans IDE.

Creating and running the JPA CRUD application

Creating and running the JPA CRUD application

     

In this section we will develop the project articles for JPA application using NetBeans IDE.

Follow the following steps to create example code.  Creating and setting up the database

CREATE DATABASE jpacrud;

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


Persistence Manager configuration (persistence.xml)
<?xml version="1.0" encoding="UTF-8"?>
<!--
Document : persistence.xml
Created on : December 15, 2008, 1:11 PM
Author : Administrator
Description:
Purpose of the document follows.
-->
<persistence>
<persistence-unit name="netjpa">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>roseindia.Student</class>
<properties>
<property name="hibernate.connection.url" value="jdbc:mysql://192.168.10.146:3306/jpacrud"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.password" value="root"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.show_sql" value="true"/>
<!--
<property name="hibernate.format_sql" value="true"/>
-->
</properties>
</persistence-unit> 
</persistence>
Right click on "roseindia" package and select New => Java Class.



Create a "Student" class file.



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



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

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")

@NamedQueries({
@NamedQuery(name="readAllRecords",query="SELECT st FROM Student st"),
@NamedQuery(name="updateRecord",query="UPDATE Student st SET st.sname= ?1 WHERE st.sroll= ?2")
})
public class Student implements java.io.Serializable {

@Id
@GeneratedValue
private int id;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

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

public String getSname() {
return sname;
}

public void setSname(String sname) {
this.sname = sname;
}

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

public int getSroll() {
return sroll;
}

public void setSroll(int sroll) {
this.sroll = sroll;
}

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

public String getScourse() {
return scourse;
}

public void setScourse(String scourse) {
this.scourse = scourse;
}
}
Create "JPACretae" class file.



And put the following text in "JPACreate.java" file.



JPACreate.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;

/**
*
* @author Administrator
*/
public class JPACreate { 

public static void main(String arg[]){
EntityManagerFactory emf=Persistence.createEntityManagerFactory("netjpa");
EntityManager em=emf.createEntityManager();
try{
EntityTransaction entr=em.getTransaction();
entr.begin();
Student stu=new Student();
stu.setSname("Vinod Kumar");
stu.setSroll(101);
stu.setScourse("MCA");
em.persist(stu);
entr.commit();
System.out.println("Successfully added into database.");
}
catch(Exception e){
System.out.println(e.getMessage());
}
finally{
em.close();
}
}
}
Right click on "JPACreate.java" file and select "Run "JPACreate.java".



You get the following output in "Output" window.


Go to MySQL and check the inputted data.