Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package hibernatepractise;
- public class Student {
- private long id;
- private String name;
- private String degree;
- private String phone;
- public Student() {
- super();
- }
- public long getId() {
- return id;
- }
- public String getName() {
- return name;
- }
- public String getDegree() {
- return degree;
- }
- public String getPhone() {
- return phone;
- }
- public void setId(long String) {
- id = String;
- }
- public void setName(String string) {
- name = string;
- }
- public void setDegree(String string) {
- degree = string;
- }
- public void setPhone(String string) {
- phone = string;
- }
- public String toString() {
- return name;
- }
- }
- package hibernatepractise;
- import org.hibernate.SessionFactory;
- import org.hibernate.Transaction;
- import org.hibernate.Session;
- import org.hibernate.cfg.Configuration;
- import hibernatepractise.Student;
- public class AddStudent {
- private static SessionFactory sessionFactory;
- public static void main(String args[]) throws Exception {
- // begin if
- // A
- String name = "Jayesh Vyas";
- String degree = "B.tech Completed";
- String phone = "9421345678";
- System.out.println("Name: " + name);
- System.out.println("Degree: " + degree);
- System.out.println("Phone: " + phone);
- if ((name.equals("") || degree.equals("") || phone.equals(""))) {
- System.out.println("All informations are Required");
- } else {
- try {// begin try
- sessionFactory = new Configuration().configure("/hibernate.cfg.xml").buildSessionFactory();
- // sessionFactory1 = new
- // Configuration().configure("com\xml\student1.cfg.xml").buildSessionFactory();
- } catch (Exception e) {
- System.out.println("mathan");
- System.out.println(e.getMessage());
- System.err.println("Initial SessionFactory creation failed."+ e);
- }
- Session s = sessionFactory.openSession();
- // Session s1 =sessionFactory1.openSession();
- // Transaction tx1= s1.beginTransaction();
- Transaction tx = s.beginTransaction();
- Student stu = new Student();
- stu.setName(name);
- stu.setDegree(degree);
- stu.setPhone(phone);
- s.save(stu);
- tx.commit();
- System.out.println("Added to oracle Database");
- if (s != null)
- s.close();
- // Student1 stu1=new Student1();
- // stu1.setName(name1);
- // s1.save(stu1);
- // tx1.commit();
- // System.out.println("Added to mysql Database");
- // if (s1 != null)
- // s1.close();
- }
- // }// end of if A
- }// end of method
- }// end of class
- <!DOCTYPE hibernate-configuration PUBLIC
- "-//Hibernate/Hibernate Configuration DTD//EN"
- "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
- <hibernate-configuration>
- <session-factory name="studentFactory">
- <property name="connection.driver_class">
- oracle.jdbc.OracleDriver
- </property>
- <property name="connection.url">
- jdbc:oracle:thin:@localhost:1521:test
- </property>
- <property name="connection.username">
- system
- </property>
- <property name="connection.password">
- manager
- </property>
- <property name="connection.pool_size">5</property>
- <!-- SQL dialect -->
- <property name="dialect">
- org.hibernate.dialect.OracleDialect
- </property>
- <!-- Echo all executed SQL to stdout -->
- <property name="show_sql">true</property>
- <property name="hbm2ddl.auto">update</property>
- <mapping resource="Student.hbm.xml" />
- </session-factory>
- </hibernate-configuration>
- <?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="hibernatepractise.Student" table="studentOracle1">
- <id name="id" type="long" column="ID">
- <generator class="increment" />
- </id>
- <property name="name" column="name" not-null="true" />
- <property name="degree" column="degree" />
- <property name="phone" column="phone" />
- </class>
- </hibernate-mapping>
Add Comment
Please, Sign In to add comment