Advertisement
Guest User

Untitled

a guest
May 29th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.54 KB | None | 0 0
  1. package org.java_json_parser.OracleDBConnection;
  2. import java.sql.*;
  3. /**
  4. * Connection to the Oracle Database.
  5. * For each task the insert_query procedure shall be called, communicating with the database.
  6. */
  7. public class DBConnect {
  8. private String user;
  9. private String pass;
  10. private Connection conn;
  11. public DBConnect(String user, String pass){
  12. this.user=user;
  13. this.pass=pass;
  14. }
  15. public void connect() throws SQLException {
  16. this.conn = DriverManager.getConnection("jdbc:oracle:thin:@//localhost:1521/XE", user, pass); //change data here
  17. }
  18. public void disconnect() throws SQLException{
  19. this.conn.close();
  20. }
  21. public void insert_query(String taskName, long date, String solution, boolean personIsInserted) throws SQLException {
  22. PreparedStatement ps;
  23. try {
  24. /**
  25. * Inserting the contestants in the table
  26. * If the contestant is already inserted we have a trigger iterating attempted and completed tasks
  27. */
  28. /*if (personIsInserted==true) {
  29. if (solution != null) {//the person was already inserted
  30. persoaneTable = "UPDATE PERSONS " +
  31. "SET tasks_attempted_counter=tasks_attempted_counter+1," +
  32. "tasks_completed_counter=tasks_completed_counter+1 " +
  33. "WHERE p_id=?";
  34. }
  35. else {
  36. persoaneTable = "UPDATE PERSONS " +
  37. "SET tasks_attempted_counter=tasks_attempted_counter+1" +
  38. "WHERE p_id=?";
  39. }
  40. }*/
  41. String persoaneTable;
  42. if (!personIsInserted) {//insert new person
  43. persoaneTable = "INSERT INTO PERSONS VALUES(PERSON_ID_SEQ.NEXTVAL,0,1)";
  44. ps = conn.prepareStatement(persoaneTable);
  45. ps.execute();
  46. ps.close();
  47. }
  48. /**
  49. * Inserting tasks in it's table
  50. * If a task is found in the table then the statement will be ignored
  51. */
  52. if (taskName!=null) {
  53. String checkForTask = "SELECT FreeCodeCamp.TaskExists(?) AS result FROM DUAL";
  54. ps = conn.prepareStatement(checkForTask);
  55. ps.setString(1, taskName);
  56. ResultSet rs = ps.executeQuery();
  57. rs.next();
  58. int result = rs.getInt("result");
  59. if (result == 0) {
  60. String insertTaks = "INSERT INTO TASKS VALUES(TASK_ID_SEQ.NEXTVAL,?)";
  61. PreparedStatement ps2 = conn.prepareStatement(insertTaks);
  62. ps2.setString(1, taskName);
  63. ps2.execute();
  64. ps2.close();
  65. }
  66. rs.close();
  67. ps.close();
  68. }
  69. /**
  70. * Before inserting each task for a person
  71. * We need to fetch the task name corresponding to the task id inserted earlier
  72. */
  73. ResultSet rs2;
  74. ps=conn.prepareStatement("SELECT task_id AS result FROM TASKS WHERE TASK_NAME=?");
  75. ps.setString(1,taskName);
  76. rs2=ps.executeQuery();
  77. int task_id=0;
  78. while(rs2.next())
  79. task_id=rs2.getInt("result");
  80. rs2.close();
  81. ps.close();
  82. /**
  83. * Inserting for each person it's task
  84. * and checking if it's completed or not
  85. */
  86. if (task_id!=0) {
  87. String insert_task;
  88. if (solution == null) {
  89. insert_task = "INSERT INTO SOLVEDTASKS VALUES(ST_ID_SEQ.NEXTVAL,PERSON_ID_SEQ.CURRVAL,?,FreeCodeCamp.unix_to_timestamp(?),'N',NULL)";
  90. ps = conn.prepareStatement(insert_task);
  91. ps.setInt(1, task_id);
  92. ps.setLong(2, date);
  93. } else {
  94. insert_task = "INSERT INTO SOLVEDTASKS VALUES(ST_ID_SEQ.NEXTVAL,PERSON_ID_SEQ.CURRVAL,?,FreeCodeCamp.unix_to_timestamp(?),'Y',?)";
  95. ps = conn.prepareStatement(insert_task);
  96. ps.setInt(1, task_id);
  97. ps.setLong(2, date);
  98. ps.setString(3, solution);
  99. }
  100. ps.execute();
  101. ps.close();
  102. }
  103. } catch (SQLException e) {
  104. e.printStackTrace();
  105. }
  106. } //insert_query end
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement