Advertisement
Guest User

Untitled

a guest
May 4th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.ResultSet;
  4. import java.sql.Statement;
  5. ////////////////////////////
  6. public class EPR
  7. {
  8. //-----------------------------
  9.  
  10. public static void main(String args[])throws Exception
  11. {
  12. try
  13. {
  14. String query1 = "select * from E";
  15. String query2 = "select * from P";
  16. String query3 = "select * from R";
  17. String query4 = "select * from EPR";
  18. String eid = "EID", ename = "ENAME", salary = "SALARY"; //columns of E table
  19. String pid = "PID", projname = "PROJECTNAME"; //columns of P table
  20. String rid = "RID", rolename = "ROLENAME"; //columns of R table
  21.  
  22. System.out.println("Printing table E:\n");
  23. System.out.println(eid + " " + ename + " " + salary);
  24. print(query1, eid, ename, salary);
  25.  
  26. System.out.println("Printing table P:\n");
  27. System.out.println(pid + " " + projname);
  28. print(query2, pid, projname, null);
  29.  
  30. System.out.println("Printing table R:\n");
  31. System.out.println(rid + " " + rolename);
  32. print(query3, rid, rolename, null);
  33.  
  34. System.out.println("Printing table EPR:\n");
  35. System.out.println(eid + " " + pid + " " + rid);
  36. print(query4, eid, pid, rid);
  37.  
  38. String url = "jdbc:odbc:Employee-Project-Role";
  39.  
  40. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  41.  
  42. Connection con = DriverManager.getConnection(url);
  43. Statement stmt = con.createStatement();
  44.  
  45.  
  46. stmt.executeUpdate("insert into E values ('X555','Simons',44444)");
  47. //stmt.executeUpdate("insert into EPR values (select EID from E where EID = 'X555', select PID from P where PID = 23, select RID from R where RID = 22)");
  48. //stmt.executeUpdate("insert into EPR values (select EID = 'X555' from E, select PID = '23' from P, select RID = '22' from R)");
  49. stmt.executeUpdate("INSERT INTO EPR VALUES ('X555',14,41)");
  50.  
  51. stmt.close();
  52. con.close();
  53.  
  54. System.out.println("Printing table E:\n");
  55. System.out.println(eid + " " + ename + " " + salary);
  56. print(query1, eid, ename, salary);
  57.  
  58. System.out.println("Printing table P:\n");
  59. System.out.println(pid + " " + projname);
  60. print(query2, pid, projname, null);
  61.  
  62. System.out.println("Printing table R:\n");
  63. System.out.println(rid + " " + rolename);
  64. print(query3, rid, rolename, null);
  65.  
  66. System.out.println("Printing table EPR:\n");
  67. System.out.println(eid + " " + pid + " " + rid);
  68. print(query4, eid, pid, rid);
  69. }catch(Exception ex)
  70. {
  71. System.out.println(ex);
  72. }
  73. }
  74. public static void print(String qury, String colmn1, String colmn2, String colmn3)throws Exception
  75. {
  76. String url = "jdbc:odbc:Employee-Project-Role";
  77.  
  78. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  79.  
  80. Connection con = DriverManager.getConnection(url);
  81. Statement stmt = con.createStatement();
  82. ResultSet rs = stmt.executeQuery(qury);
  83. while (rs.next())
  84. {
  85. String column = rs.getString(colmn1);
  86. System.out.print(column + " ");
  87. column = rs.getString(colmn2);
  88. System.out.print(column + " ");
  89. if(colmn3 != null)
  90. {
  91. column = rs.getString(colmn3);
  92. System.out.print(column + " \n");
  93. }
  94. else
  95. System.out.println("");
  96. }
  97. System.out.println("");
  98. rs.close();
  99. stmt.close();
  100. con.close();
  101. }
  102.  
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement