StudentInf.java ---------------
package com.example; import javax.servlet.*;
import javax.servlet.http.*; import java.io.*;
import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration;
public class StudentInf extends HttpServlet{
/** * */ private static final long serialVersionUID = 1L;
public void service(HttpServletRequest request, HttpServletResponse response)throws ServletException,IOException{
PrintWriter out = response.getWriter();
String vid = request.getParameter("studentid"); String vname = request.getParameter("name"); String vage = request.getParameter("age"); String vcourse = request.getParameter("course"); String vaddress = request.getParameter("address"); SessionFactory sessionFactory = null; Session session = null; // sessionFactory = new Configuration().configure("/hibernate.cfg.xml").buildSessionFactory(); //session =sessionFactory.openSession(); System.out.println(vid); try{ sessionFactory = new Configuration().configure("com/example/hibernate.cfg.xml").buildSessionFactory(); session =sessionFactory.openSession();
StudentHibernate sh = new StudentHibernate(); System.out.println(sh); sh.setStudentid(Integer.parseInt(vid)); sh.setName(vname); sh.setAge(Integer.parseInt(vage)); sh.setCourse(vcourse); sh.setAddress(vaddress);
session.save(sh); System.out.println("Done");
out.println("User Data Saved Successfully......."); out.println("<a href=\"http://localhost:8080/Servlet Ex/New Registration Screen.jsp \"> click here to Add one more Employee Data.</a>");
}catch(Exception e){
System.out.println(e.getMessage()); }
} }
StudentHibernate.java --------------------- package com.example;
public class StudentHibernate{
int studentid; String name; int age; String course; String address;
public int getStudentid(){
return studentid; }
public void setStudentid(int studentid){
this.studentid = studentid;
}
public String getName(){
return name; }
public void setName(String name){ this.name = name;
} public int getAge(){
return age; }
public void setAge(int age){
this.age = age;
} public String getCourse(){
return course; }
public void setCourse(String course){
this.course = course;
} public String getAddress(){
return address; }
public void setAddress(String address){
this.address = address;
}
}
studenthibernate.hbm.xml ------------------------- <?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.example.StudentHibernate" table="studenthibernate"> <id name="studentid" column="Studentid" > <generator class="assined"/> </id> <property name="name" > <column name="Name" /> </property> <property name="age" > <column name= "Age" /> </property> <property name="course" > <column name="Course"/> </property> <property name="address" > <column name="Address"/> </property> </class> </hibernate-mapping>
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> <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">root</property> <property name="hibernate.connection.pool_size">10</property> <property name="show_sql">true</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.hbm2ddl.auto">update</property> <!-- Mapping files --> <mapping resource="com/example/studenthibernate.hbm.xml"/> </session-factory> </hibernate-configuration>
Thanq
|