Monday, October 8, 2012


hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
      <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
      <property name="hibernate.connection.url">jdbc:mysql:///omdata</property>
      <property name="hibernate.connection.username">root</property>
      <property name="hibernate.connection.password">root</property>
      <property name="hibernate.connection.pool_size">10</property>
      <property name="show_sql">true</property>
      <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
      <property name="hibernate.hbm2ddl.auto">update</property>
      <!-- Mapping files -->
      <mapping resource="employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
employee.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
  <class name="pack.Employee" table="EMP">
  <id name="eid" type="long" column="EID"  >
  <generator class="assigned"/>
 </id>

 <property name="ename">
<column name="EMPNAME" />
 </property>
    </class>
</hibernate-mapping>

Employee.java
package pack;
public class Employee {
private String ename;
private long eid;

public void setEid(long l) {
eid = l;
}
public long getEid() {
return eid;
}
public void setEname(String ename) {
this.ename = ename;
}
public String getEname() {
return ename;
}
}


Main.java
package pack;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.Transaction;
 public class Main {
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();
Transaction transaction = null;
transaction = session.beginTransaction();
//Create new instance of Contact and set values in it by reading them from form object
System.out.println("Inserting Record");
Employee e = new Employee();
e.setEid(3);
e.setEname("Prakash om");
session.save(e);
transaction.commit();
System.out.println("Done");
}catch(Exception e){
System.out.println(e.getMessage());
}finally{
// Actual contact insertion will happen at this step
//session.flush();
//session.close();

} }}
InputForm.java
package pack;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class InputForm extends JFrame implements ActionListener
{
JTextField tId=new JTextField();
JTextField tName=new JTextField();
JLabel lbl=new JLabel();
JButton btn=new JButton("Save");

InputForm()
{

JPanel p1=new JPanel();
setLayout(null);
p1.setLayout(new GridLayout(4,1));
getContentPane().add(p1);
p1.setBounds(50,50,200,150);

p1.add(tId);
p1.add(tName);
p1.add(btn);
p1.add(lbl);
setSize(300,300);
setVisible(true);
btn.addActionListener(this);
}
public void actionPerformed(ActionEvent ev)
{
String ename=tName.getText();
int eid=0;
try{
eid=Integer.parseInt(tId.getText());

SaveInputForm sif=new SaveInputForm(eid,ename);
lbl.setText(sif.status);
}
catch(Exception e)
{
System.out.println(e.getMessage());
lbl.setText(e.getMessage());
}


}

public static void main(String arg[])
{
new InputForm();
}
}
SaveInputForm.java
package pack;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.Transaction;
public class SaveInputForm {
String status;
SaveInputForm(int eid,String ename)
{
Session session = null;
try{

// This step will read hibernate.cfg.xml and prepare hibernate for use
@SuppressWarnings("deprecation")
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();
Transaction transaction = null;
transaction = session.beginTransaction();
//Create new instance of Contact and set values in it by reading them from form object
System.out.println("Inserting Record");
Employee e = new Employee();
e.setEid(eid);
e.setEname(ename);
session.save(e);
transaction.commit();
System.out.println("Done");
status="Success";
}catch(Exception e){
System.out.println(e.getMessage());
status=e.getMessage();
}finally{
// Actual contact insertion will happen at this step
//session.flush();
//session.close();

}

}

}

No comments:

Post a Comment