The Data Access Object is a design pattern, which consists java classes and methods to access the data. It provides a technique to separate the object persistence and data access logic. All the data access codes are written inside a dao class and it provides interface to access data.
The DAO given with application, uses hibernate to access data from database. Hibernate contains java some classes and .xml files. To make a Hibernate Dao at first you need to make hibernate.cfg.xml. This file contain all the information required to connect to a database.
Following are the properties of this file.ADS_TO_REPLACE_1
<session-factory> <property name="hibernate.connection.driver_class">Database Driver Name</property> <property name="hibernate.connection.url">Connection URL of the database</property> <property name="hibernate.connection.username">User Name</property> <property name="connection.password">Password</property> <property name="connection.pool_size">Size of the connection pool</property> <property name="hibernate.dialect">Dialect of the using database</property> <property name="show_sql">show/hide SQL query</property> <property name="hbm2ddl.auto">used to map with the contact table</property> <mapping class="path of the database table" /> </session-factory>hibernate.connection.driver_class- Whenever you wish to connect to the database, you fist need to load the driver of that database. Every database have their own drivers. This property used to load the driver of the database.
package dao.tables; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name = "adminuser") public class Adminuser implements Serializable { @Id @GeneratedValue /** identifier field */ private Integer id; /** persistent field */ private String userid; /** persistent field */ private String password; /** full constructor */ public Adminuser(Integer id, String userid, String password) { this.id = id; this.userid = userid; this.password = password; } /** default constructor */ public Adminuser() { } @Column(name = "id") public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } @Column(name = "userid") public String getUserid() { return this.userid; } public void setUserid(String userid) { this.userid = userid; } @Column(name = "password") public String getPassword() { return this.password; } public void setPassword(String password) { this.password = password; } }
private static SessionFactory sessionFactory; static { try { sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory(); } catch (Exception e) { e.getMessage(); e.toString(); } } public static synchronized SessionFactory getSessionfactory() { return sessionFactory; } }
HibernateUtil util = new HibernateUtil(); public List showAll() { Session session = util.getSessionfactory().openSession(); Transaction transaction = null; List users = new ArrayList<User>(); try { transaction = session.beginTransaction(); users = session.createQuery("from User").list(); transaction.commit(); } catch (Exception e) { e.getMessage(); e.toString(); transaction.rollback(); } finally { session.close(); } return users; }
Advertisements
Ads
Ads