Advertisement
Guest User

Untitled

a guest
Nov 14th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.10 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.FileReader;
  3. import java.io.IOException;
  4. import java.sql.CallableStatement;
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.PreparedStatement;
  8. import java.sql.ResultSet;
  9. import java.sql.SQLException;
  10. import java.sql.SQLSyntaxErrorException;
  11. import java.sql.Statement;
  12. import java.util.ArrayList;
  13. import java.util.Scanner;
  14.  
  15. import oracle.jdbc.OracleTypes;
  16.  
  17. public class Task1 {
  18.  
  19. private Connection conn = null;
  20. private String driver = "oracle.jdbc.driver.OracleDriver";
  21.  
  22. // 1.2.1
  23. // constructor
  24. public Task1(String connectionUrl, String username, String password) throws ClassNotFoundException, SQLException {
  25. connect(connectionUrl, username, password);
  26. }
  27.  
  28. private void connect(String connectionUrl, String username, String password) {
  29. try {
  30. Class.forName(this.driver);
  31. this.conn = DriverManager.getConnection(connectionUrl, username, password);
  32. this.conn.setAutoCommit(false);
  33. }
  34. catch(Exception e){
  35. e.printStackTrace();
  36. }
  37. }
  38.  
  39. // 1.2.2
  40. // The function reads the file content and inserts it to the MediaItems table
  41. public void fileToDataBase(String csvFilePath) throws IOException, SQLException, SQLSyntaxErrorException {
  42. try {
  43. // csv file: [title, production year]
  44. BufferedReader reader = new BufferedReader(new FileReader(csvFilePath ));//+ "\\films.csv"));
  45.  
  46. String line = null;
  47. Scanner scanner = null;
  48.  
  49. while ((line = reader.readLine()) != null) {
  50. // comma separated values
  51. scanner = new Scanner(line);
  52. scanner.useDelimiter(",");
  53.  
  54. // extract title and year from media item
  55. String title = null;
  56. String year = null;
  57.  
  58. while (scanner.hasNext()) {
  59. title = scanner.next();
  60. year = scanner.next();
  61. }
  62.  
  63. /*
  64. System.out.println(line);
  65. System.out.println(title);
  66. System.out.println(year);
  67. */
  68.  
  69. // insert values to MediaItems table
  70. String query = "INSERT INTO MEDIAITEMS (TITLE, PROD_YEAR) VALUES (?,?)";
  71.  
  72. PreparedStatement preparedStatement = this.conn.prepareStatement(query);
  73.  
  74. preparedStatement.setString(1, title);
  75. preparedStatement.setInt(2, Integer.parseInt(year));
  76.  
  77. preparedStatement.executeUpdate();
  78.  
  79. this.conn.commit();
  80.  
  81. preparedStatement.close();
  82. }
  83.  
  84. reader.close();
  85.  
  86. } catch (Exception e) {
  87. e.printStackTrace();
  88. }
  89. }
  90.  
  91. // 1.2.3
  92. // The function calculates the similarity between every pair of items in the MediaItems table
  93. public void calculateSimilarity() {
  94. try {
  95. int maxDistance = 0;
  96.  
  97. // find up to which index to loop through
  98. int startMIDLoop = 0;
  99. int endMIDLoop = 0;
  100.  
  101. String endMIDLoopQuery = "SELECT MAX (MID) FROM MEDIAITEMS";
  102. PreparedStatement preparedStatement = conn.prepareStatement(endMIDLoopQuery);
  103. ResultSet result = preparedStatement.executeQuery();
  104.  
  105. if(result.next()) {
  106. endMIDLoop = result.getInt(1);
  107. //System.out.println(result.getInt(1));
  108. }
  109.  
  110. result.close();
  111. preparedStatement.close();
  112.  
  113. // calculate max for the similarity function
  114. CallableStatement callableStatement = conn.prepareCall("{ ? = call MaximalDistance() }");
  115. callableStatement.registerOutParameter(1, OracleTypes.NUMBER);
  116. callableStatement.execute();
  117. maxDistance = callableStatement.getInt(1);
  118. //System.out.println(maxDistance);
  119.  
  120. callableStatement.close();
  121.  
  122. // calculate similarity between every pair using the similarity function
  123. for(int i = 0; i <= endMIDLoop; i++) {
  124. for(int j = 0; j <= endMIDLoop; j++)
  125. {
  126. if(i != j) {
  127. // calculate distance between i & j
  128. CallableStatement callableStatementSim = this.conn.prepareCall("{ ? = call SimCalculation(?,?,?) }");
  129. callableStatementSim.registerOutParameter(1, OracleTypes.FLOAT);
  130. callableStatementSim.setInt(2, i);
  131. callableStatementSim.setInt(3, j);
  132. callableStatementSim.setInt(4, maxDistance);
  133. callableStatementSim.execute();
  134. float distance = callableStatementSim.getFloat(1);
  135. callableStatementSim.close();
  136.  
  137. PreparedStatement preparedStatementSim = conn.prepareStatement("SELECT * FROM SIMILARITY WHERE MID1 = ? AND MID2 = ?");
  138. preparedStatementSim.setInt(1, i);
  139. preparedStatementSim.setInt(2, j);
  140. resultSim = preparedStatementSim.executeQuery();
  141. if(rs.next()) {
  142. //UPDATE THE SIMILARITY BETWEEN MID1 MID2
  143. PreparedStatement update = conn.prepareStatement("UPDATE SIMILARITY SET SIMILARITY = ? WHERE MID1 = ? AND MID2 = ?");
  144. update.setFloat(1, distance);
  145. update.setInt(2, i);
  146. update.setInt(3, j);
  147. update.executeUpdate();
  148. update.close();
  149. }
  150. else
  151. {
  152. //INSERT SIMILARITY
  153. PreparedStatement update = conn.prepareStatement("INSERT INTO SIMILARITY (MID1, MID2, SIMILARITY) VALUES (?,?,?)");
  154. update.setInt(1, i);
  155. update.setInt(2, j);
  156. update.setFloat(3, distance);
  157. update.executeUpdate();
  158. update.close();
  159. }
  160.  
  161. preparedStatementSim.close();
  162. rs.close();
  163. }
  164. }
  165. }
  166.  
  167. } catch (Exception e) {
  168. e.printStackTrace();;
  169. }
  170. /*
  171.  
  172. for(int i=0; i < maxInd; i++) {
  173. for(int j = i+1; j <= maxInd ; j++)
  174. {
  175. if(i == j)
  176. continue;
  177. CallableStatement csLoop = conn.prepareCall("{ ? = call SimCalculation(?,?,?) }");
  178. csLoop.registerOutParameter(1, OracleTypes.FLOAT);
  179. csLoop.setInt(2, i);
  180. csLoop.setInt(3, j);
  181. csLoop.setInt(4, maxDist);
  182. csLoop.execute();
  183. float midsDistance = csLoop.getFloat(1);
  184. csLoop.close();
  185.  
  186. PreparedStatement psLoop = conn.prepareStatement("SELECT * FROM SIMILARITY WHERE MID1 = ? AND MID2 = ?");
  187. psLoop.setInt(1, i);
  188. psLoop.setInt(2, j);
  189. rs = psLoop.executeQuery();
  190. if(rs.next()) {
  191. //UPDATE THE SIMILARITY BETWEEN MID1 MID2
  192. PreparedStatement update = conn.prepareStatement("UPDATE SIMILARITY SET SIMILARITY = ? WHERE MID1 = ? AND MID2 = ?");
  193. update.setFloat(1, midsDistance);
  194. update.setInt(2, i);
  195. update.setInt(3, j);
  196. update.executeUpdate();
  197. update.close();
  198. }
  199. else
  200. {
  201. //INSERT SIMILARITY
  202. PreparedStatement update = conn.prepareStatement("INSERT INTO SIMILARITY (MID1, MID2, SIMILARITY) VALUES (?,?,?)");
  203. update.setInt(1, i);
  204. update.setInt(2, j);
  205. update.setFloat(3, midsDistance);
  206. update.executeUpdate();
  207. update.close();
  208. }
  209. psLoop.close();
  210. rs.close();
  211. }
  212. }
  213. } catch (Exception e) {
  214. System.out.println("Exception Catched!");
  215. }*/
  216.  
  217. }
  218.  
  219. public static void main(String[] args) throws ClassNotFoundException, SQLException, IOException{
  220. final String username = "shacharo";
  221. final String password = "abcd";
  222. final String connectionUrl = "jdbc:oracle:thin:@ora1.ise.bgu.ac.il:1521/oracle";
  223.  
  224. Task1 task = new Task1(connectionUrl, username, password);
  225. // 1.2.1
  226. //task.fileToDataBase("d:\\documents\\users\\rinag\\Documents\\films.csv");
  227.  
  228. // 1.2.2
  229. task.calculateSimilarity();
  230. }
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement