Guest User

7

a guest
May 10th, 2017
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.94 KB | None | 0 0
  1. package ques7;
  2.  
  3. import java.util.*;
  4. import java.sql.*;
  5. public class Customer
  6. {
  7.  
  8. static Connection con=null;
  9. static Statement st=null;
  10. static ResultSet rs=null;
  11. public static void display(int i)
  12. {
  13. try
  14. {
  15. if(i==1)
  16. {
  17. RegularCustomer.show();
  18. }
  19. if(i==2)
  20. {
  21. CorporateCustomer.show();
  22. }
  23.  
  24. }catch(Exception e)
  25. {
  26. System.out.println(e);
  27. }
  28. }
  29. public static void main(String args[])
  30. {
  31. Scanner sc = new Scanner(System.in);
  32. int i,id,ch;
  33. String name,address;
  34. try
  35. {
  36. Class.forName("oracle.jdbc.driver.OracleDriver");
  37. con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","hr","hr");
  38. st=con.createStatement();
  39. System.out.println("if you are going to run this class for first time then press 1 for creating table else press 2");
  40. ch=sc.nextInt();
  41. if(ch==1)
  42. {
  43. st.execute("create table RegularCustomer(id number primary key,name varchar2(20),address varchar2(20))");
  44. st.execute("create table CorporateCustomer(id number primary key,name varchar2(20),address varchar2(20))");
  45. ch=2;
  46. }
  47. if(ch==2)
  48. {
  49. while(true)
  50. {
  51. System.out.println("enter 1 for insertion in regular employee and 2 for corporate employee and 3 for display");
  52. i=sc.nextInt();
  53. if(i==1)
  54. {
  55. System.out.println("enter id");
  56. id=sc.nextInt();
  57. System.out.println("Enter name");
  58. name=sc.next();
  59. System.out.println("Enter address");
  60. address=sc.next();
  61. RegularCustomer r=new RegularCustomer(id,name,address);
  62. r.DB();
  63. }
  64. if(i==2)
  65. {
  66. System.out.println("enter id");
  67. id=sc.nextInt();
  68. System.out.println("Enter name");
  69. name=sc.next();
  70. System.out.println("Enter address");
  71. address=sc.next();
  72. CorporateCustomer c=new CorporateCustomer(id,name,address);
  73. c.DB();
  74. }
  75. if(i==3)
  76. {
  77. System.out.println("enter 1 for regular employee and 2 for corporate employee");
  78. i=sc.nextInt();
  79. display(i);
  80. }
  81. System.out.println("enter 1 for more insert 2 for display 3 for quit");
  82. ch=sc.nextInt();
  83. if(ch==3)
  84. {
  85. con.close();
  86. break;
  87. }
  88. else if(ch==2)
  89. {
  90. System.out.println("enter 1 for regular employee and 2 for corporate employee");
  91. i=sc.nextInt();
  92. display(i);
  93. }
  94. }
  95. }
  96. }catch(Exception e){System.out.println(e);}
  97. }
  98. }
  99.  
  100. -------------------------------------------
  101. package ques7;
  102.  
  103.  
  104.  
  105. import java.sql.Connection;
  106. import java.sql.DriverManager;
  107. import java.sql.ResultSet;
  108. import java.sql.Statement;
  109.  
  110.  
  111. public class RegularCustomer
  112. {
  113.  
  114. static Connection con=null;
  115. static Statement st=null;
  116. static ResultSet rs=null;
  117. int id;
  118. String name;
  119. String address;
  120.  
  121.  
  122. RegularCustomer(int id,String name,String address)
  123. {
  124. this.id=id;
  125. this.name=name;
  126. this.address=address;
  127. }
  128. public void DB()
  129. {
  130. try
  131. {
  132. Class.forName("oracle.jdbc.driver.OracleDriver");
  133. con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","hr","hr");
  134. st=con.createStatement();
  135. st.execute("insert into RegularCustomer values("+id+",'"+name+"','"+address+"')");
  136. }catch(Exception e){System.out.println(e);}
  137. }
  138. public static void show()
  139. {
  140. try
  141. {
  142.  
  143. Class.forName("oracle.jdbc.driver.OracleDriver");
  144. con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","hr","hr");
  145. st=con.createStatement();
  146. rs=st.executeQuery("select * from RegularCustomer");
  147. while(rs.next())
  148. {
  149. System.out.println("Id = "+rs.getInt(1)+" name = "+rs.getString(2)+" address = "+rs.getString(3));
  150. }
  151. }catch(Exception e){System.out.println(e);}
  152. }
  153. }
  154.  
  155. ----------------------------------------
  156. package ques7;
  157.  
  158. import java.sql.Connection;
  159. import java.sql.DriverManager;
  160. import java.sql.ResultSet;
  161. import java.sql.Statement;
  162.  
  163.  
  164. public class CorporateCustomer
  165. {
  166.  
  167. static Connection con=null;
  168. static Statement st=null;
  169. static ResultSet rs=null;
  170. int id;
  171. String name;
  172. String address;
  173.  
  174.  
  175. CorporateCustomer(int id,String name,String address)
  176. {
  177. this.id=id;
  178. this.name=name;
  179. this.address=address;
  180. }
  181. public void DB()
  182. {
  183. try
  184. {
  185. Class.forName("oracle.jdbc.driver.OracleDriver");
  186. con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","hr","hr");
  187. st=con.createStatement();
  188. st.execute("insert into RegularCustomer values("+id+",'"+name+"','"+address+"')");
  189. }catch(Exception e){System.out.println(e);}
  190. }
  191. public static void show()
  192. {
  193. try
  194. {
  195. Class.forName("oracle.jdbc.driver.OracleDriver");
  196. con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","hr","hr");
  197. st=con.createStatement();
  198. rs=st.executeQuery("select * from RegularCustomer");
  199. while(rs.next())
  200. {
  201. System.out.println("Id = "+rs.getInt(1)+" name = "+rs.getString(2)+" address = "+rs.getString(3));
  202. }
  203. }catch(Exception e){System.out.println(e);}
  204. }
  205. }
Add Comment
Please, Sign In to add comment