Today, I did a simple JPA program in console.
As usual , my database is MySql in XAMPP.
The database name is ‘jpadb1’ .
Then I created a table ‘player’ which contains 3 fields. They are id, name and place.
In this table, id is an auto increment field and, name and place are strings.
goto c:\xampp\mysql\bin
>mysql -u root
>create database jpadb1;
>use jpadb1;
>create table player (id int(5) NOT NULL
primary key auto_increment, name
varchar(20), place varchar(20));
Then I inserted some records into player table.
Then I opened a new command prompt and created a folder in g:\ as ‘jpademo2’.
In this folder, I am going to put three java files and a META-INF folder for JPA persistence xml mapping file. The java files I had created are,
1.player.java
2.querybean.java
3.demo.java
Here player is a value bean which will hold record of the player table whenever necessary.
import javax.persistence.*;
@Entity
@Table(name="player")
public class player
{
@Id
@GeneratedValue
String id;
String name;
String place;
public void setId(String s) {id=s;}
public String getId() {return id;}
public void setName(String s) {name=s;}
public String getName() {return name;}
public void setPlace(String s) {place=s;}
public String getPlace() {return place;}
}
Here I used some annotations for mapping the player table to this entity bean class and I specified the database name, driver class in persistence.xml file. This xml file should be placed in META-INF folder.
So I create a folder under jpademo2 directory.
>md META-INF
>cd META-INF
>edit persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence>
<persistence-unit name="jpa"
transaction-type="RESOURCE_LOCAL">
<provider>
org.hibernate.ejb.HibernatePersistence
</provider>
<class>player</class>
<properties>
<property name="hibernate.connection.url"
value="jdbc:mysql://localhost/jpadb1" />
<property name="hibernate.dialect"
value="net.sf.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.connection.driver_class"
value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.password"
value="" />
<property name="hibernate.connection.username"
value="root" />
<property name="hibernate.hbm2ddl.auto"
value="update" />
<property name="hibernate.show_sql"
value="true" />
</properties>
</persistence-unit>
</persistence>
*************************************************
Then I wrote the querybean.java code. In which we have to do three things
1.Create EntityManagerFactory object
2.Create an object for EntityManager using
EntityManagerFactory object
3.Start the transaction with the help of
EntityManager Object.
******************************************************
import java.util.*;
import javax.persistence.*;
@SuppressWarnings("serial")
public class querybean
{
EntityManager em;
player player1;
public String sqlquery
(String a,String b,String c,String d)
{
String r="\n*****************************************\n\n";
try
{
System.out.println("beginning");
EntityManagerFactory emf=
Persistence.createEntityManagerFactory("jpa");
System.out.println("entitymanagerfactory obtained");
em = emf.createEntityManager();
System.out.println("entity manager obtained");
em.getTransaction().begin();
System.out.println("transaction begun");
System.out.println(">java demo 100 tom trichy find (etc)" );
if(d.equals("add"))
{
player1 = new player();
player1.setName(b);
player1.setPlace(c);
em.persist(player1);
r=r+"one record added!";
}
if(d.equals("update"))
{
player1 = em.find(player.class, a);
player1.setName(b);
player1.setPlace(c);
em.merge(player1);
r=r+"Updated!...";
}
if(d.equals("find"))
{
player1 = em.find(player.class,a);
String s1=player1.getName();
String s2=player1.getPlace();
r=r+a+" "+s1+" "+s2;
}
if(d.equals("delete"))
{
player1 = em.find(player.class,a);
String s1 = player1.getName();
String s2 = player1.getPlace();
em.remove(player1);
r=r+" deleted!";
}
if(d.equals("show"))
{
Query query=em.createQuery("SELECT p FROM player p");
List list = query.getResultList();
Iterator it = list.iterator();
while(it.hasNext())
{
player1 = (player) it.next();
String s3 = player1.getId();
String s1 = player1.getName();
String s2 = player1.getPlace();
r=r+s3+" "+s1+" "+s2+"\n";
}
}
em.getTransaction().commit();
em.close();
} catch(Exception e1){System.out.println(""+e1);}
return r;
}
}
Finally, I created a console client for this bean. I have give the code below,
demo.java
//-----------
class demo
{
public static void main(String[] args)
{
String a = args[0]; //id
String b = args[1]; //name
String c = args[2]; //place
String d = args[3]; //operation
querybean bean1 = new querybean();
String r = bean1.sqlquery(a,b,c,d);
System.out.println(r);
}
}
Now, we have to compile the above java files. Before that we should have some jar files which are necessary to write a jpa application in class path. The jar file are
ant-1.6.5.jar
ant-swing-1.6.5.jar
antlr-2.7.6.jar
asm.jar
cglib-2.1.3.jar
commons-collections-2.1.1.jar
commons-logging-1.0.4.jar
dom4j-1.6.1.jar
ejb3-persistence.jar
hibernate3.jar
hibernate-annotations.jar
hibernate-commons-annotations.jar
hibernate-core.jar
hibernate-entitymanager.jar
javassist-3.3.ga.jar
jdbc2_0-stdext.jar
jta.jar
log4j.jar
mysql-connector-java-3.1.6-bin.jar
persistence-api-1.0.jar
lib\slf4j-api.jar
slf4j-log4j12.jar
xerces-2.6.2.jar;c:\jpajars\lib\xml-apis.jar
mysqldriver.jar
Now we can compile the java files
>setpath
>setcpath
>javac player.java
>javac querybean.java
>javac demo.java
Now we can run the demo.java as follows
>java demo “” “sam” “chennai” “add”
then you will get following messages
then go to your mysql prompt and query
“select *from player” you will get the following table