Tuesday 16 August 2016

Distinct Keyword in Hibernate

Home.jsp


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="org.hibernate.*,org.hibernate.cfg.*,java.util.*" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        Standard <select name="std" >
            <%
                Session ses = null;

                try {
                    Configuration cfg = new Configuration();
                    cfg.configure("hibernate.cfg.xml");//populates the data of the configuration file
                    SessionFactory factory = cfg.buildSessionFactory();
                    ses = factory.openSession();
                    Transaction t = ses.beginTransaction();
                    String s = "select distinct s.std from add_std s";
                    ListIterator<Object[]> ObjectsIterator = ses.createSQLQuery(s).list().listIterator();
                    while (ObjectsIterator.hasNext()) {
                        Object object = (Object) ObjectsIterator.next();
                        String name = (String) object;
                        System.out.println(name);
            %>
            <option><%=name%></option>
            <%
                    }
                    t.commit();
                    ses.close();
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                }
            %>
        </select>
    </body>
</html>

hibernate.cfg.xml


<?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/student?zeroDateTimeBehavior=convertToNull</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">root</property>
    <mapping resource="my/AddStd.hbm.xml"/>
  </session-factory>
</hibernate-configuration>

addstd.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 Aug 16, 2016 2:31:08 PM by Hibernate Tools 4.3.1 -->
<hibernate-mapping>
    <class name="my.add_std" table="add_std" catalog="student" optimistic-lock="version">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="identity" />
        </id>
        <property name="std" type="string">
            <column name="std" length="30" not-null="true" />
        </property>
        <property name="sec" type="string">
            <column name="sec" length="30" not-null="true" />
        </property>
    </class>
</hibernate-mapping>


No comments:

Post a Comment