Wednesday, 3 August 2011

Simple Java Script Example

<input type="button" value="Cick Me"  onclick="show()">
<script>
function show(){
alert("This is a test");
}
</script>
Here as you see I used <script> means it default treated as <script language="javascript">

Lest See another example..


<html>
<head>
<title>JavaScript Example</title>
</head>
<body>
<table>
<tr><td>Please Enter Your Name:</td><td><input type="text" id="name"></td></tr>
<tr><td>&nbsp; &nbsp;&nbsp;&nbsp;</td><td><input type="button" onClick="test()" value="Click on Me."></td></tr>
</table>
</body>
<script language="javascript">
function test()
{
   var name=document.getElementById('name').value;
   alert("Welcome"+name);
}
</script>
</html>

Simple Hibernate Example

Here I put Simple Hibernate Example.




This is My Fist Program For Hibernate.
Using Eclipse
 It requires three things.
1. Simple bean class
2. Simple Hibernating Mapping Xml file.
3.Hibernate Configuration File.
4. And the one Main class under the scr folder.


In scr folder create the following package.

com.vikash.core.bean
   now ceate the class Student.java
as follows
package com.vikash.core.beans;

public class Student {
long id;
String name;
public long getId() {
    return id;
}
public void setId(long id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}

}


create the file student.hbm.xml  at the same place as Student.java resides.
<?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="com.vikash.core.beans.Student" table="STD_TABLE">
<id name="id" type="long" column="ID">
<generator class="assigned"/>
</id>
<property name="name">
<column name="STD_NAME"/>
</property>
</class>
</hibernate-mapping>

now in src folder create the file name 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>

      <!-- Database connection settings -->

      <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
      <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
      <property name="hibernate.connection.username">root</property>
      <property name="hibernate.connection.password">vikash</property>

         <!-- JDBC connection pool (use the built-in) -->
        <property name="hibernate.connection.pool_size">10</property>

        <!-- SQL dialect -->
          <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- Enable Hibernate's automatic session context management -->
   
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->

        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
   
        <property name="hibernate.hbm2ddl.auto">update</property>
 
  <mapping resource="com/vikash/core/beans/student.hbm.xml" />
    <!--<mapping resource="beans/player.hbm.xml" />
    --></session-factory>
  </hibernate-configuration>

Now create the main class under scr as follows:




import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;

import com.vikash.test.Employee;


public class MainClazz {
    public static void main(String args[]){

    try{
        Configuration cfg=new Configuration();
        SessionFactory sessionFactory=cfg.configure("/hibernate.cfg.xml").buildSessionFactory();
        session=sessionFactory.openSession();
        Transaction tx=session.beginTransaction();
        Student std=new Student();
        std.setId(System.currentTimeMillis());
        std.setName("Manohar Kumar");
        session.save(std);
        tx.commit();
        }catch(Exception e){
            e.printStackTrace();
        }
        finally{
            if(session!=null)
                session.flush();
                session.close();
              
        }
         }
    }
   
   
If any query then let me know.