Wednesday, April 11, 2012

The day i started using frameworks

it took some time while getting acquainted with hibernate.but no matter what it was easy learning.
why hibernate?
The developers get rid of writing complex SQLs and no more need of JDBC APIs for resultset handling.
x.cfg.xml file provides  the connection with database.
Y.hbm.xml provides mapping between database column and POJO variable.
A simple web app test to know the basics.

remember to create a database connection on the back end and then select that when you open a new project to execute
the code can be downloaded from 
http://www.mediafire.com/?lw5pvhxpfwx5xr9

hibernate.cfg.xml file


<?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/hibernatetutorial</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="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <mapping resource="entities/Subscriber.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

Subscriber.hbm.xml


<?xml version="1.0" encoding="UTF-8"?>


<!DOCTYPE hibernate-mapping PUBLIC
  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">


<hibernate-mapping>
    <class name="entities.Subscriber" table="test">
        <id name="uname" type="java.lang.String">
            <column name="username" />
            <generator class="assigned"/>
        </id>
        <property name="pwd" type="java.lang.String">
            <column name="password"/>
        </property>
    </class>
  


</hibernate-mapping>


create a jsp page .pass the login and password say through a form into a servlet.create a getter setter class with the instance variables that are to be used to insert data in a database..these are mapped to column names in the Y.hbm.xml file

the servlet file

 protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        PrintWriter out = response.getWriter();
        String uname = request.getParameter("uname");
        String pwd = request.getParameter("pwd");
        System.out.println("inside the servlet");
        try {
            SessionFactory sf = new Configuration().configure().buildSessionFactory();
            Session s = sf.openSession();
            Subscriber sub = new Subscriber();
            sub.setUname(uname);
            sub.setPwd(pwd);
            s.beginTransaction();
            s.save(sub);
            s.getTransaction().commit();
            s.close();
            sf.close();
            out.println(
                    "<h3 >[" + uname + "  ] has been registered successfully!</h3 >");
        } catch (Exception ex) {
            out.println(
                    "<h3 >[" + uname + "] could NOT be registered!</h3 >");
            System.out.println(ex.getMessage());
        }
    }

No comments:

Post a Comment