Guest User

Untitled

a guest
Oct 20th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.85 KB | None | 0 0
  1. package hibernatepractise;
  2.  
  3. public class Student {
  4. private long id;
  5. private String name;
  6. private String degree;
  7. private String phone;
  8.  
  9.  
  10.  
  11. public Student() {
  12. super();
  13. }
  14.  
  15. public long getId() {
  16. return id;
  17. }
  18.  
  19. public String getName() {
  20. return name;
  21. }
  22.  
  23. public String getDegree() {
  24. return degree;
  25. }
  26.  
  27. public String getPhone() {
  28. return phone;
  29. }
  30.  
  31. public void setId(long String) {
  32. id = String;
  33. }
  34.  
  35. public void setName(String string) {
  36. name = string;
  37. }
  38.  
  39. public void setDegree(String string) {
  40. degree = string;
  41. }
  42.  
  43. public void setPhone(String string) {
  44. phone = string;
  45. }
  46.  
  47. public String toString() {
  48. return name;
  49. }
  50.  
  51. }
  52.  
  53. package hibernatepractise;
  54.  
  55.  
  56. import org.hibernate.SessionFactory;
  57. import org.hibernate.Transaction;
  58. import org.hibernate.Session;
  59. import org.hibernate.cfg.Configuration;
  60.  
  61. import hibernatepractise.Student;
  62.  
  63. public class AddStudent {
  64. private static SessionFactory sessionFactory;
  65.  
  66. public static void main(String args[]) throws Exception {
  67. // begin if
  68. // A
  69. String name = "Jayesh Vyas";
  70. String degree = "B.tech Completed";
  71. String phone = "9421345678";
  72.  
  73. System.out.println("Name: " + name);
  74. System.out.println("Degree: " + degree);
  75. System.out.println("Phone: " + phone);
  76.  
  77. if ((name.equals("") || degree.equals("") || phone.equals(""))) {
  78. System.out.println("All informations are Required");
  79. } else {
  80.  
  81. try {// begin try
  82. sessionFactory = new Configuration().configure("/hibernate.cfg.xml").buildSessionFactory();
  83. // sessionFactory1 = new
  84. // Configuration().configure("com\xml\student1.cfg.xml").buildSessionFactory();
  85. } catch (Exception e) {
  86. System.out.println("mathan");
  87. System.out.println(e.getMessage());
  88. System.err.println("Initial SessionFactory creation failed."+ e);
  89.  
  90. }
  91. Session s = sessionFactory.openSession();
  92. // Session s1 =sessionFactory1.openSession();
  93. // Transaction tx1= s1.beginTransaction();
  94. Transaction tx = s.beginTransaction();
  95. Student stu = new Student();
  96. stu.setName(name);
  97. stu.setDegree(degree);
  98. stu.setPhone(phone);
  99. s.save(stu);
  100. tx.commit();
  101. System.out.println("Added to oracle Database");
  102. if (s != null)
  103. s.close();
  104.  
  105. // Student1 stu1=new Student1();
  106. // stu1.setName(name1);
  107. // s1.save(stu1);
  108. // tx1.commit();
  109. // System.out.println("Added to mysql Database");
  110. // if (s1 != null)
  111. // s1.close();
  112. }
  113. // }// end of if A
  114. }// end of method
  115. }// end of class
  116.  
  117. <!DOCTYPE hibernate-configuration PUBLIC
  118. "-//Hibernate/Hibernate Configuration DTD//EN"
  119. "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
  120. <hibernate-configuration>
  121. <session-factory name="studentFactory">
  122. <property name="connection.driver_class">
  123. oracle.jdbc.OracleDriver
  124. </property>
  125. <property name="connection.url">
  126. jdbc:oracle:thin:@localhost:1521:test
  127. </property>
  128. <property name="connection.username">
  129. system
  130. </property>
  131. <property name="connection.password">
  132. manager
  133. </property>
  134. <property name="connection.pool_size">5</property>
  135. <!-- SQL dialect -->
  136. <property name="dialect">
  137. org.hibernate.dialect.OracleDialect
  138. </property>
  139. <!-- Echo all executed SQL to stdout -->
  140. <property name="show_sql">true</property>
  141. <property name="hbm2ddl.auto">update</property>
  142. <mapping resource="Student.hbm.xml" />
  143. </session-factory>
  144. </hibernate-configuration>
  145.  
  146. <?xml version="1.0" encoding="UTF-8"?>
  147. <!DOCTYPE hibernate-mapping PUBLIC
  148. "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  149. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  150. <hibernate-mapping>
  151. <class name="hibernatepractise.Student" table="studentOracle1">
  152. <id name="id" type="long" column="ID">
  153. <generator class="increment" />
  154. </id>
  155. <property name="name" column="name" not-null="true" />
  156. <property name="degree" column="degree" />
  157. <property name="phone" column="phone" />
  158. </class>
  159. </hibernate-mapping>
Add Comment
Please, Sign In to add comment