Validate data from the database using Hibernate and swing by R4R Team

In this swing session we have learn how to insert data into the database using swing JFrame form. So here we will discuss that how can we validate the data using hibernate and database from the swing JFrame form. For these type of action we need to follow the some basic step which is given below:

Step 1. First of all we need to create two table into the database one to insert the database into the data using JFrame form and second for the validate the data form the table on the Swing JFrame form.

Step 2. We have already discussed that how to insert data into multiple database using Hibernate. Click the below link to check how to insert the data into the multiple table.


Step 3. Given table we will use in this session.

create table SwingLogin

(

id intnotnullprimarykey auto_increment,

fname varchar(50),

lname varchar(50),

uid varchar(50),

mid varchar(50),

pass varchar(50)

)

create table SwingValidate

(

id intnotnullprimarykey auto_increment,

uid varchar(50),

mid varchar(40),

pass varchar(50)

)

Step 4. Now create a Java Application (Follow the instruction as shown in out last example click here)

Step 5. Now create a hibernate cfg file (Follow the instruction as shown in out last example click here)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_pro</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">root</property>
    <mapping resource="r4r/Tblstate.hbm.xml"/>
    <mapping resource="r4r/SwingValidation.hbm.xml"/>
    <mapping resource="r4r/Tblcity.hbm.xml"/>
    <mapping resource="r4r/SwingForgetPass.hbm.xml"/>
    <mapping resource="r4r/Squestion.hbm.xml"/>
    <mapping resource="r4r/SwingLogin.hbm.xml"/>
  </session-factory>
</hibernate-configuration>

Step 6. Create a hibernate reveng file (Follow the instruction as shown in out last example click here)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering PUBLIC "-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd">
<hibernate-reverse-engineering>
  <schema-selection match-catalog="hibernate_pro"/>
  <table-filter match-name="tblstate"/>
  <table-filter match-name="tblcity"/>
  <table-filter match-name="SwingValidation"/>
  <table-filter match-name="SwingLogin"/>
  <table-filter match-name="SwingForgetPass"/>
  <table-filter match-name="SQuestion"/>
</hibernate-reverse-engineering>

Step 6. Now create a POJO class inside the package. (Follow the instruction as shown in out last example click here)

1. SwingLogin.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated Jan 13, 2015 9:19:37 PM by Hibernate Tools 3.6.0 -->
<hibernate-mapping>
<class name="R4R.SwingLogin" table="SwingLogin" schema="dbo" catalog="STRUTS_Using_Hibernate">
<id name="id" type="int">
<column name="id" />
<generator class="assigned" />
</id>
<property name="fname" type="string">
<column name="fname" length="50" />
</property>
<property name="lname" type="string">
<column name="lname" length="50" />
</property>
<property name="uid" type="string">
<column name="uid" length="50" />
</property>
<property name="mid" type="string">
<column name="mid" length="50" />
</property>
<property name="pass" type="string">
<column name="pass" length="50" />
</property>
</class>
</hibernate-mapping>

2. SwingLogin.java

package R4R;
public class SwingLogin implements java.io.Serializable {
private int id;
private String fname;
private String lname;
private String uid;
private String mid;
private String pass;

public SwingLogin() {
}
public SwingLogin(int id) {
this.id = id;
}
public SwingLogin(int id, String fname, String lname, String uid, String mid, String pass) {
this.id = id;
this.fname = fname;
this.lname = lname;
this.uid = uid;
this.mid = mid;
this.pass = pass;
}
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public String getFname() {
return this.fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getLname() {
return this.lname;
}
public void setLname(String lname) {
this.lname = lname;
}
public String getUid() {
return this.uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getMid() {
return this.mid;
}
public void setMid(String mid) {
this.mid = mid;
}
public String getPass() {
return this.pass;
}
public void setPass(String pass) {
this.pass = pass;
}
}

3. SwingValidate.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated Jan 13, 2015 9:19:37 PM by Hibernate Tools 3.6.0 -->
<hibernate-mapping>
<class name="R4R.SwingValidate" table="SwingValidate" schema="dbo" catalog="STRUTS_Using_Hibernate">
<id name="id" type="int">
<column name="id" />
<generator class="assigned" />
</id>
<property name="uid" type="string">
<column name="uid" length="50" />
</property>
<property name="mid" type="string">
<column name="mid" length="40" />
</property>
<property name="pass" type="string">
<column name="pass" length="50" />
</property>
</class>
</hibernate-mapping>

4. SwingValidate.java

package R4R;
public class SwingValidate implements java.io.Serializable {
private int id;
private String uid;
private String mid;
private String pass;
public SwingValidate() {
}
public SwingValidate(int id) {
this.id = id;
}
public SwingValidate(int id, String uid, String mid, String pass) {
this.id = id;
this.uid = uid;
this.mid = mid;
this.pass = pass;
}
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public String getUid() {
return this.uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getMid() {
return this.mid;
}
public void setMid(String mid) {
this.mid = mid;
}
public String getPass() {
return this.pass;
}
public void setPass(String pass) {
this.pass = pass;
}
}

Step 7. Now create a Swing JFrame Form (as we have already discussed into the last example click here)

1. Inserting the Data(Registration Page)

public class RegistrationPage extends javax.swing.JFrame {
    public String pho;
    public RegistrationPage() {
        initComponents();
        {
        List<String> a = Admin.getState();
           for(String s : a)
           state.addItem(s);
        }
        List<String> a = Admin.getSQuestion();
          for(String s : a)
          squestion.addItem(s);
    }
    private boolean validateFields() 
    {
if (fname.getText().equals("")) {
 JOptionPane.showMessageDialog(this,"Please enter To first name!","Error", JOptionPane.ERROR_MESSAGE);
fname.requestFocus();
return false;
}
if (lname.getText().equals("")) 
                {
         JOptionPane.showMessageDialog(this, "Please enter last name!","Error", JOptionPane.ERROR_MESSAGE);
lname.requestFocus();
return false;
}
if (email.getText().equals("")) 
                {
JOptionPane.showMessageDialog(this, "Please enter email!","Error", JOptionPane.ERROR_MESSAGE);
email.requestFocus();
return false;
}
return true;
}


    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
             new LoginPage().setVisible(true);
             dispose();
    }                                        

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
                JFileChooser fd = new JFileChooser("c:/");
                fd.showOpenDialog(this);
                 pho = fd.getSelectedFile().toString(); 
                photo.setIcon(new ImageIcon(pho));
                
    }                                        

    private void cityActionPerformed(java.awt.event.ActionEvent evt) {                                     
          
    }                                    

    private void stateActionPerformed(java.awt.event.ActionEvent evt) {                                      
           city.removeAllItems();
           List<String> a = Admin.getCity(state.getSelectedItem().toString());
           for(String s : a)
           city.addItem(s); 
    }                                     

    private void squestionActionPerformed(java.awt.event.ActionEvent evt) {                                          
//                       List<String> a = Admin.getSQuestion();
//                       for(String s : a)
//                       squestion.addItem(s);
    }                                         

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
                 if (!validateFields()) {
return;
}
                 
                 SessionFactory sf = new Configuration().configure().buildSessionFactory();
                 Session s = sf.openSession();
                 Transaction tr = s.beginTransaction();
                 r4r.SwingValidation sv = new r4r.SwingValidation(fname.getText(), lname.getText(), email.getText(), uid.getText(), pass.getText(), address.getText(), 
                 state.getSelectedItem().toString(), city.getSelectedItem().toString(), Integer.parseInt(pin.getText()), 
                 squestion.getSelectedItem().toString(), sans.getText(), pho.getBytes());
                 r4r.SwingLogin sl = new r4r.SwingLogin(email.getText(), uid.getText(), pass.getText());
                 r4r.SwingForgetPass sfp = new r4r.SwingForgetPass(uid.getText(), pass.getText());
                 JOptionPane.showMessageDialog(this, "One Record Inserted");
                    s.save(sv);
                    s.save(sl);
                    s.save(sfp);
                    tr.commit();
                    s.close();
                    s.clear();
    }                                        


2. For Validate the daTa.
 
  public class LoginPage extends javax.swing.JFrame {
    public LoginPage() {
        initComponents();
    } 

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        new RegistrationPage().setVisible(true);
    }                                        
    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        new ForgetPass().setVisible(true);
    }                                        
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            int a = Admin.checkId(uid.getText(),new String(pass.getPassword()));
           if(a!=0)
            JOptionPane.showMessageDialog(this,"Sorry this is Invalid Login Id & Password");
             else 
               {
                 new WelcomePage().setVisible(true);
               }
    }
 
3. After the Validate the data.

Step 8. Now take a admin class for making holding the action of the application.

package swing.validation;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Restrictions;
public class Admin 
{
   public static List<String> getState() 
   {
     List<String> a = new ArrayList<String>();   
     try
     {
        SessionFactory sf = new Configuration().configure().buildSessionFactory();
        Session s = sf.openSession();
        Transaction tr = s.beginTransaction();
         Criteria cr = s.createCriteria(r4r.Tblstate.class);
        java.util.List<r4r.Tblstate> ds = cr.list();
        for(r4r.Tblstate dd : ds)
        {
        a.add(dd.getStateName());   
         }   
     }catch(Exception ex) { a.add(ex.getMessage()); }
     return a;
   }
   public static List<String> getCity(String st) 
   {
     List<String> a = new ArrayList<String>();   
     try
     {
         SessionFactory sf = new Configuration().configure().buildSessionFactory();
         Session s = sf.openSession();
         Transaction tr = s.beginTransaction();
         Criteria cr = s.createCriteria(r4r.Tblcity.class).add(Restrictions.eq( "stateName", st));
        java.util.List<r4r.Tblcity> ds = cr.list();
        for(r4r.Tblcity dd : ds)
        {
        a.add(dd.getCityName());   
         }
     }catch(Exception ex) { a.add(ex.getMessage()); }
     return a;
   }
   public static List<String> getSQuestion()
   {
     List<String> a = new ArrayList<String>();   
     try
     {
         SessionFactory sf = new Configuration().configure().buildSessionFactory();
         Session s = sf.openSession();
         Transaction tr = s.beginTransaction();
         Criteria cr = s.createCriteria(r4r.Squestion.class);
         java.util.List<r4r.Squestion> ds = cr.list();
         for(r4r.Squestion dd : ds)
         {
            a.add(dd.getSquestion());   
         }
     }catch(Exception ex) { a.add(ex.getMessage()); }
     return a;
   }
 public static int checkId(String u,String ps)
  {
    try
    {
      SessionFactory sf = new Configuration().configure().buildSessionFactory();
      Session s = sf.openSession();
      Transaction tr = s.beginTransaction();
      //Criteria cr = s.createCriteria(r4r.Login1.class).add(Restrictions.eq( "mid", u )); 
      
      Query query = s.createQuery("select count(*) from SwingLogin where uid = "+u+" && pass = "+ps+"");
       
        query.executeUpdate();

     return query.executeUpdate();
    }catch(Exception ex) { return 0; }
      //  return 0;
  }
}

Step 9. Now run the application.

1. Insert page Outlook

2. Login Page Outlook.

3. After Login Page Outlook.




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!