Advertisement
Guest User

Untitled

a guest
Jun 21st, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.08 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.sql.*;
  3.  
  4. /**
  5. * Created by eli on 12/02/2017.
  6. */
  7. public class ass4 {
  8.  
  9. public static void main(String[] argv) throws SQLException, IOException {
  10. String userName = "user8";
  11. String password = "HTYH3fYS";
  12. String host = "jdbc:oracle:thin:@132.72.40.216:1522:orcl"; // jdbc:oracle:thin:@132.72.40.216:1522:orcl
  13. Connection conn = connectOracleDB(host, userName, password);
  14. if (conn == null) {
  15. return;
  16. }
  17.  
  18. Statement stmt = conn.createStatement();
  19. dropAndCreate(stmt);
  20. insertDataToTables(stmt);
  21. testFunc(conn);
  22.  
  23. try {
  24. if (stmt != null) {
  25. conn.close();
  26. System.out.println("\nConnection closed");
  27. }
  28. } catch (SQLException e) {
  29. System.out.println("Cannot close connection");
  30. e.printStackTrace();
  31. }
  32.  
  33. }
  34.  
  35.  
  36. public static Connection connectOracleDB(String host, String userName, String password) {
  37. System.out.println("-------- Oracle JDBC Connection Testing ------");
  38.  
  39. try {
  40.  
  41. Class.forName("oracle.jdbc.driver.OracleDriver");
  42.  
  43. } catch (ClassNotFoundException e) {
  44.  
  45. System.out.println("Where is your Oracle JDBC Driver?");
  46. e.printStackTrace();
  47. return null;
  48.  
  49. }
  50.  
  51. System.out.println("Oracle JDBC Driver Registered!");
  52.  
  53. Connection connection = null;
  54.  
  55. try {
  56.  
  57. connection = DriverManager.getConnection(host, userName, password);
  58.  
  59. } catch (SQLException e) {
  60.  
  61. System.out.println("Connection Failed! Check output console");
  62. e.printStackTrace();
  63. return null;
  64.  
  65. }
  66.  
  67. if (connection != null) {
  68. System.out.println("You made it, take control your database now!");
  69. return connection;
  70.  
  71. } else {
  72. System.out.println("Failed to make connection!");
  73. return null;
  74. }
  75. }
  76.  
  77. public static void dropTables(Statement stmt) throws SQLException {
  78.  
  79. String drop1 = "DROP TABLE EmployeesShifts";
  80. String drop2 = "DROP TABLE Salaries";
  81. String drop3 = "DROP TABLE Employees";
  82. String drop4 = "DROP TABLE Expertise";
  83. String drop5 = "DROP TABLE ShiftsTypes";
  84. String drop6 = "DROP TABLE Weeks";
  85. String drop7 = "DROP TABLE Days";
  86.  
  87. stmt.executeUpdate(drop1);
  88. stmt.executeUpdate(drop2);
  89. stmt.executeUpdate(drop3);
  90. stmt.executeUpdate(drop4);
  91. stmt.executeUpdate(drop5);
  92. stmt.executeUpdate(drop6);
  93. stmt.executeUpdate(drop7);
  94. }
  95.  
  96. public static void createTables(Statement stmt) throws SQLException {
  97.  
  98. String createExpertiseTable = "CREATE TABLE Expertise" +
  99. " (expr_id INTEGER not NULL," +
  100. " name VARCHAR(255)," +
  101. " PRIMARY KEY ( expr_id ))";
  102. stmt.executeUpdate(createExpertiseTable);
  103.  
  104. String createEmployeesTable = "CREATE TABLE Employees" +
  105. " (eid INTEGER not NULL," +
  106. " name VARCHAR(255)," +
  107. " expr_id INTEGER not NULL," +
  108. " PRIMARY KEY ( eid )," +
  109. " FOREIGN KEY ( expr_id ) references Expertise)";
  110. stmt.executeUpdate(createEmployeesTable);
  111.  
  112. String createDaysTable = "CREATE TABLE Days" +
  113. " (did INTEGER not NULL," +
  114. " name VARCHAR(255)," +
  115. " PRIMARY KEY ( did ))";
  116. stmt.executeUpdate(createDaysTable);
  117.  
  118. String createWeeksTable = "CREATE TABLE Weeks" +
  119. " (wid INTEGER not NULL," +
  120. " name VARCHAR(255)," +
  121. " PRIMARY KEY ( wid ))";
  122. stmt.executeUpdate(createWeeksTable);
  123.  
  124. String createShiftsTable = "CREATE TABLE ShiftsTypes" +
  125. " (sid INTEGER not NULL," +
  126. " name VARCHAR(255)," +
  127. " quota INTEGER," +
  128. " curr_quota INTEGER," +
  129. " PRIMARY KEY (sid ))";
  130. stmt.executeUpdate(createShiftsTable);
  131.  
  132. String createSalariesTable = "CREATE TABLE Salaries" +
  133. " (expr_id INTEGER not NULL," +
  134. " sid INTEGER not NULL," +
  135. " salary INTEGER not NULL," +
  136. " PRIMARY KEY ( expr_id,sid )," +
  137. " FOREIGN KEY (expr_id) references Expertise," +
  138. " FOREIGN KEY (sid) references ShiftsTypes)";
  139. stmt.executeUpdate(createSalariesTable);
  140.  
  141. String createEmployeesShifts = "CREATE TABLE EmployeesShifts" +
  142. " (eid INTEGER not NULL," +
  143. " wid INTEGER not NULL," +
  144. " did INTEGER not NULL," +
  145. " sid INTEGER not NULL," +
  146. " PRIMARY KEY ( eid,wid,did,sid )," +
  147. " FOREIGN KEY (eid) references Employees," +
  148. " FOREIGN KEY (wid) references Weeks," +
  149. " FOREIGN KEY (did) references Days," +
  150. " FOREIGN KEY (sid) references ShiftsTypes)";
  151. stmt.executeUpdate(createEmployeesShifts);
  152. }
  153.  
  154. public static void dropAndCreate(Statement stmt) throws SQLException {
  155. System.out.println("DB is droping...");
  156. dropTables(stmt);
  157. System.out.println("DB is droped...");
  158. System.out.println("Creating tables...");
  159. createTables(stmt);
  160. System.out.println("Tables created...");
  161. }
  162.  
  163. public static void insertDataToExpertise(Statement stmt) throws SQLException {
  164. stmt.executeUpdate("INSERT INTO Expertise VALUES (1, 'beginner')");
  165. stmt.executeUpdate("INSERT INTO Expertise VALUES (2, 'regular')");
  166. stmt.executeUpdate("INSERT INTO Expertise VALUES (3, 'expert')");
  167. stmt.executeUpdate("INSERT INTO Expertise VALUES (4, 'very expert')");
  168. stmt.executeUpdate("INSERT INTO Expertise VALUES (5, 'very very expert')");
  169. }
  170.  
  171. public static void insertDataToDays(Statement stmt) throws SQLException {
  172. stmt.executeUpdate("INSERT INTO Days VALUES (1, 'sunday')");
  173. stmt.executeUpdate("INSERT INTO Days VALUES (2, 'monday')");
  174. stmt.executeUpdate("INSERT INTO Days VALUES (3, 'tuesday')");
  175. stmt.executeUpdate("INSERT INTO Days VALUES (4, 'wednesday')");
  176. stmt.executeUpdate("INSERT INTO Days VALUES (5, 'thursday')");
  177. stmt.executeUpdate("INSERT INTO Days VALUES (6, 'friday')");
  178. stmt.executeUpdate("INSERT INTO Days VALUES (7, 'saturday')");
  179. }
  180.  
  181. public static void insertDataToWeeks(Statement stmt) throws SQLException {
  182. stmt.executeUpdate("INSERT INTO Weeks VALUES (1, 'first')");
  183. stmt.executeUpdate("INSERT INTO Weeks VALUES (2, 'second')");
  184. stmt.executeUpdate("INSERT INTO Weeks VALUES (3, 'third')");
  185. stmt.executeUpdate("INSERT INTO Weeks VALUES (4, 'forth')");
  186. }
  187.  
  188. public static void insertDataToShiftsTypes(Statement stmt) throws SQLException {
  189. stmt.executeUpdate("INSERT INTO ShiftsTypes VALUES (1,'dayTime',10,10)");
  190. stmt.executeUpdate("INSERT INTO ShiftsTypes VALUES (2,'midDay',20,20)");
  191. stmt.executeUpdate("INSERT INTO ShiftsTypes VALUES (3,'night',30,30)");
  192. }
  193.  
  194. public static void insertDataToSalaries(Statement stmt) throws SQLException {
  195. stmt.executeUpdate("INSERT INTO Salaries VALUES (1,1,50)");
  196. stmt.executeUpdate("INSERT INTO Salaries VALUES (4,1,100)");
  197. stmt.executeUpdate("INSERT INTO Salaries VALUES (5,2,200)");
  198.  
  199. //common shift
  200. stmt.executeUpdate("INSERT INTO Salaries VALUES (1,3,500)");
  201. stmt.executeUpdate("INSERT INTO Salaries VALUES (4,3,1500)");
  202. stmt.executeUpdate("INSERT INTO Salaries VALUES (5,3,2500)");
  203.  
  204. // line for A-3
  205. stmt.executeUpdate("INSERT INTO Salaries VALUES (1,2,20)");
  206. stmt.executeUpdate("INSERT INTO Salaries VALUES (4,2,1700)");
  207. }
  208.  
  209. public static void insertDataToTables(Statement stmt) throws SQLException {
  210. insertDataToExpertise(stmt);
  211. insertDataToDays(stmt);
  212. insertDataToWeeks(stmt);
  213. insertDataToShiftsTypes(stmt);
  214. insertDataToSalaries(stmt);
  215. System.out.println("Data inserted successfully to DB...");
  216. }
  217.  
  218. // wid-week id
  219. // did - day id
  220. // sid - shift type
  221. public static void expensePerShift(Connection conn, String wid ,String did, String sid) throws SQLException {
  222.  
  223. String get_expr_id;
  224. String get_sal;
  225. ResultSet rs1,rs2;
  226. int eid ,expr_id ,salaries_sum = 0;
  227.  
  228. //getting all the eids of the employees that are working in that exact shift.
  229. String get_eids = "SELECT eid FROM EmployeesShifts WHERE wid = " + wid + " AND did= " + did + " AND sid= " + sid;
  230. rs1 = conn.createStatement().executeQuery(get_eids);
  231.  
  232. // for each employee , we getting his salary.
  233. while (rs1.next()) {
  234. // getting specific employee eid
  235. eid = rs1.getInt("eid");
  236. //getting expr_id for that specific employee
  237. get_expr_id = "SELECT expr_id FROM Employees WHERE eid =" + eid ;
  238. rs2 = conn.createStatement().executeQuery(get_expr_id);
  239. rs2.next();
  240. expr_id = rs2.getInt("expr_id");
  241. // getting employee's salary
  242. get_sal = "SELECT salary FROM Salaries WHERE sid= " + sid + " AND expr_id = " + expr_id;
  243. rs2 = conn.createStatement().executeQuery(get_sal);
  244. rs2.next();
  245. salaries_sum = salaries_sum + rs2.getInt("salary");
  246.  
  247. }
  248. System.out.println("\n\nThe total salary expenses of shift: week_id: " +wid +", day_id: "+ did + ", shift_type: "+sid);
  249. System.out.println(salaries_sum);
  250.  
  251. }
  252.  
  253. public static void employeSalary(Connection conn, String eid ,int[] weeks) throws SQLException {
  254.  
  255. int sum =0,expr_id,sid;
  256. String get_expr_id,get_sids, get_salary_per_shift;
  257. ResultSet rs1,rs2,rs3;
  258.  
  259. //getting expr_id for that specific employee
  260. get_expr_id = "SELECT expr_id FROM Employees WHERE eid =" + eid ;
  261. rs1 = conn.createStatement().executeQuery(get_expr_id);
  262. rs1.next();
  263. expr_id = rs1.getInt("expr_id");
  264.  
  265. for(int i = 0; i < weeks.length; i++){
  266. get_sids = "SELECT sid FROM EmployeesShifts WHERE wid = " + weeks[i] + " AND eid= " + eid;
  267. rs2 = conn.createStatement().executeQuery(get_sids);
  268. while (rs2.next()) {
  269. //getting shift id for that specific shift and employee
  270. sid = rs2.getInt("sid");
  271. get_salary_per_shift = "SELECT salary FROM Salaries WHERE sid= " + sid + " AND expr_id = " + expr_id;
  272. rs3 = conn.createStatement().executeQuery(get_salary_per_shift);
  273. rs3.next();
  274. sum = sum + rs3.getInt("salary");
  275. }
  276. }
  277.  
  278. System.out.println("Salary for employee : " + eid + "\n salary: " + sum);
  279. }
  280.  
  281. public static void employeSalaryForMonth(Connection conn, String eid ) throws SQLException {
  282. int[] weeks = {1,2,3,4};
  283. employeSalary(conn,eid,weeks);
  284. }
  285.  
  286. public static void checkEmployeeSalary(Connection conn, String eid) throws SQLException {
  287.  
  288. int expr_id,sid,maxSalaryMorning = 0 , maxSalaryMidDay =0, maxSalaryEvening =0 ,salary;
  289. int[] weeks = {1,2,3,4};
  290. String get_expr_id,get_sids, get_salary_per_shift;
  291. ResultSet rs1,rs2,rs3;
  292.  
  293. //getting expr_id for that specific employee
  294. get_expr_id = "SELECT expr_id FROM Employees WHERE eid =" + eid ;
  295. rs1 = conn.createStatement().executeQuery(get_expr_id);
  296. rs1.next();
  297. expr_id = rs1.getInt("expr_id");
  298.  
  299.  
  300. for(int i = 0; i < weeks.length; i++){
  301. get_sids = "SELECT sid FROM EmployeesShifts WHERE wid = " + weeks[i] + " AND eid= " + eid;
  302. rs2 = conn.createStatement().executeQuery(get_sids);
  303. while (rs2.next()) {
  304. //getting shift id for that specific shift and employee
  305. sid = rs2.getInt("sid");
  306. get_salary_per_shift = "SELECT salary FROM Salaries WHERE sid= " + sid + " AND expr_id = " + expr_id;
  307. rs3 = conn.createStatement().executeQuery(get_salary_per_shift);
  308. rs3.next();
  309.  
  310. salary = rs3.getInt("salary");
  311. switch(sid){
  312.  
  313. case 1:
  314. if (salary > maxSalaryMorning)
  315. maxSalaryMorning = salary;
  316. break;
  317. case 2:
  318. if (salary > maxSalaryMidDay)
  319. maxSalaryMidDay = salary;
  320. break;
  321. case 3:
  322. if (salary > maxSalaryEvening)
  323. maxSalaryEvening = salary;
  324. break;
  325.  
  326. }
  327.  
  328.  
  329. }
  330. }
  331.  
  332. if (maxSalaryMorning > maxSalaryMidDay){
  333. System.out.println("Morning salary > MidDay salary");
  334. }
  335. if (maxSalaryMidDay > maxSalaryEvening){
  336. System.out.println("MidDay salary > Evening salary");
  337. }
  338. }
  339.  
  340. public static void insertEmployeeShift(Connection conn, int eid, int wid, int did, int sid) throws SQLException {
  341.  
  342. String get_curr_quota = "SELECT curr_quota FROM ShiftsTypes WHERE sid =" + sid ;
  343. ResultSet rs1 = conn.createStatement().executeQuery(get_curr_quota);
  344. rs1.next();
  345. int quota = rs1.getInt("curr_quota");
  346.  
  347. // Getting quota from DB - inserting only if > 0.
  348. if (quota > 0){
  349. String query = "INSERT INTO EmployeesShifts VALUES ("+ eid +","+ wid +","+ did + ","+ sid+ ")";
  350. Statement stmt = conn.createStatement();
  351. stmt.executeUpdate(query);
  352.  
  353. quota--;
  354.  
  355. String insert_updated_quota = "UPDATE ShiftsTypes SET curr_quota = "+ quota + " WHERE sid = "+sid ;
  356. stmt.executeUpdate(insert_updated_quota);
  357. System.out.println("Shift quota updated from: "+ (quota+1) +", to: "+ quota);
  358. }
  359. else{
  360. System.out.println("Sorry, the shift is full.");
  361. }
  362.  
  363. }
  364.  
  365. public static void testFunc(Connection conn) throws SQLException {
  366. System.out.println("\n~~~~~ Our testfunc results : ~~~~~");
  367.  
  368. String sql1 = "INSERT INTO Employees VALUES (301463550,'Ron Klar', 1)";
  369. String sql2 = "INSERT INTO Employees VALUES (204558439,'Eli Tsinberg', 4)";
  370. String sql3 = "INSERT INTO Employees VALUES (123456789,'Tom Weiss', 5)";
  371.  
  372.  
  373. Statement stmt = conn.createStatement();
  374. stmt.executeUpdate(sql1);
  375. stmt.executeUpdate(sql2);
  376. stmt.executeUpdate(sql3);
  377.  
  378. // B-question
  379. insertEmployeeShift(conn,301463550,1,1,1);
  380. insertEmployeeShift(conn,301463550,1,2,3);
  381. insertEmployeeShift(conn,301463550,4,5,2);
  382. insertEmployeeShift(conn,204558439,2,2,2);
  383. insertEmployeeShift(conn,204558439,1,2,3);
  384. insertEmployeeShift(conn,123456789,3,3,3);
  385. insertEmployeeShift(conn,123456789,1,2,3);
  386.  
  387. // A-2 question
  388. expensePerShift(conn,"1","2","3"); // common shift expenses
  389. expensePerShift(conn,"1","1","1");
  390.  
  391. // A-2 question
  392. int[] weeks = {1,3};
  393. System.out.println("\n\nRon earn in weeks 1,3: ");
  394. employeSalary(conn,"301463550",weeks);
  395. int[] weeks1 = {2,4};
  396. System.out.println("\nEli earn in weeks 2,4: ");
  397. employeSalary(conn,"204558439",weeks1);
  398.  
  399. System.out.println("\n\nRon earn in the whole month: ");
  400. employeSalaryForMonth(conn,"301463550");
  401. System.out.println("\nEli earn in the whole month: ");
  402. employeSalaryForMonth(conn,"204558439");
  403.  
  404.  
  405. //A-3 question
  406. System.out.println("\n\nCheck employee salary for Ron: ");
  407. checkEmployeeSalary(conn,"301463550");
  408.  
  409. System.out.println("\nCheck employee salary for Eli: ");
  410. checkEmployeeSalary(conn,"204558439");
  411.  
  412. }
  413. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement