Advertisement
Guest User

Untitled

a guest
Nov 15th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.85 KB | None | 0 0
  1. import java.awt.List;
  2. import java.io.BufferedReader;
  3. import java.io.FileReader;
  4. import java.io.IOException;
  5. import java.sql.CallableStatement;
  6. import java.sql.Connection;
  7. import java.sql.DriverManager;
  8. import java.sql.PreparedStatement;
  9. import java.sql.ResultSet;
  10. import java.sql.SQLException;
  11. import java.sql.SQLSyntaxErrorException;
  12. import java.sql.Statement;
  13. import java.util.ArrayList;
  14. import java.util.HashMap;
  15. import java.util.LinkedList;
  16. import java.util.Scanner;
  17.  
  18. import oracle.jdbc.OracleTypes;
  19.  
  20. public class Task1 {
  21.  
  22. private Connection conn = null;
  23. private String driver = "oracle.jdbc.driver.OracleDriver";
  24.  
  25. // 1.2.1
  26. // constructor
  27. public Task1(String connectionUrl, String username, String password) throws ClassNotFoundException, SQLException {
  28. connect(connectionUrl, username, password);
  29. }
  30.  
  31. private void connect(String connectionUrl, String username, String password) {
  32. try {
  33. Class.forName(this.driver);
  34. this.conn = DriverManager.getConnection(connectionUrl, username, password);
  35. this.conn.setAutoCommit(false);
  36. }
  37. catch(Exception e){
  38. e.printStackTrace();
  39. }
  40. }
  41.  
  42. // 1.2.2
  43. // The function reads the file content and inserts it to the MediaItems table
  44. public void fileToDataBase(String csvFilePath) throws IOException, SQLException, SQLSyntaxErrorException {
  45. try {
  46. // csv file: [title, production year]
  47. BufferedReader reader = new BufferedReader(new FileReader(csvFilePath));
  48.  
  49. String line = null;
  50. Scanner scanner = null;
  51.  
  52. while ((line = reader.readLine()) != null) {
  53. // comma separated values
  54. scanner = new Scanner(line);
  55. scanner.useDelimiter(",");
  56.  
  57. // extract title and year from media item
  58. String title = null;
  59. String year = null;
  60.  
  61. while (scanner.hasNext()) {
  62. title = scanner.next();
  63. year = scanner.next();
  64. }
  65.  
  66. /*
  67. System.out.println(line);
  68. System.out.println(title);
  69. System.out.println(year);
  70. */
  71.  
  72. // insert values to MediaItems table
  73. String query = ""
  74. + "INSERT INTO MEDIAITEMS "
  75. + "(TITLE, PROD_YEAR) "
  76. + "VALUES (?,?)";
  77.  
  78. PreparedStatement preparedStatement = this.conn.prepareStatement(query);
  79.  
  80. preparedStatement.setString(1, title);
  81. preparedStatement.setInt(2, Integer.parseInt(year));
  82.  
  83. preparedStatement.executeUpdate();
  84.  
  85. this.conn.commit();
  86.  
  87. preparedStatement.close();
  88. }
  89.  
  90. reader.close();
  91.  
  92. //System.out.println("Successfully added CSV rows to database!");
  93. } catch (Exception e) {
  94. e.printStackTrace();
  95. }
  96. }
  97.  
  98. // 1.2.3
  99. // The function calculates the similarity between every pair of items in the MediaItems table
  100. public void calculateSimilarity() {
  101. try {
  102. // maximum distance between a pair of MIDs (used in the SimCalculation formula)
  103. int maxDistance = 0;
  104.  
  105. // up to which index to loop through (max MID)
  106. int endMIDLoop = 0;
  107.  
  108. // find endMIDLoop value
  109. String endMIDLoopQuery =
  110. "SELECT MAX (MID) "
  111. + "FROM MEDIAITEMS";
  112.  
  113. PreparedStatement preparedStatement = this.conn.prepareStatement(endMIDLoopQuery);
  114. ResultSet result = preparedStatement.executeQuery();
  115.  
  116. if(result.next()) {
  117. endMIDLoop = result.getInt(1);
  118. //System.out.println(result.getInt(1));
  119. }
  120.  
  121. result.close();
  122. preparedStatement.close();
  123.  
  124. // calculate max for the similarity function
  125. CallableStatement callableStatement = this.conn.prepareCall("{ ? = call MaximalDistance() }");
  126.  
  127. // MaximalDistance output value
  128. callableStatement.registerOutParameter(1, OracleTypes.NUMBER);
  129.  
  130. callableStatement.execute();
  131.  
  132. maxDistance = callableStatement.getInt(1);
  133. //System.out.println(maxDistance);
  134.  
  135. callableStatement.close();
  136.  
  137. // calculate similarity between every MID pair using the similarity function
  138. for(int i = 0; i <= endMIDLoop; i++) {
  139. for(int j = i + 1; j <= endMIDLoop; j++)
  140. {
  141. // don't compare identical MIDs
  142. if(i != j) {
  143. // calculate distance between MIDi & MIDj
  144. float MIDiMIDjDistance = 0;
  145. CallableStatement callableStatementSim = this.conn.prepareCall("{ ? = call SimCalculation(?,?,?) }");
  146.  
  147. // SimCalculation output value
  148. callableStatementSim.registerOutParameter(1, OracleTypes.FLOAT);
  149.  
  150. // SimCalculation arguments: MIDi, MIDj, maxDistance
  151. callableStatementSim.setInt(2, i);
  152. callableStatementSim.setInt(3, j);
  153. callableStatementSim.setInt(4, maxDistance);
  154.  
  155. callableStatementSim.execute();
  156.  
  157. MIDiMIDjDistance = callableStatementSim.getFloat(1);
  158.  
  159. callableStatementSim.close();
  160.  
  161. // get current similarity value of MIDi and MIDj in Similarity table
  162. PreparedStatement preparedStatementSim = this.conn.prepareStatement(
  163. "SELECT * "
  164. + "FROM SIMILARITY "
  165. + "WHERE MID1 = ? AND MID2 = ?");
  166.  
  167. preparedStatementSim.setInt(1, i);
  168. preparedStatementSim.setInt(2, j);
  169.  
  170. ResultSet resultSim = preparedStatementSim.executeQuery();
  171.  
  172. // if there already is an out-dated value in the table, then update it according to MIDiMIDjDistance
  173. if(resultSim.next()) {
  174. PreparedStatement update = this.conn.prepareStatement(
  175. "UPDATE SIMILARITY "
  176. + "SET SIMILARITY = ? "
  177. + "WHERE MID1 = ? AND MID2 = ?");
  178.  
  179. update.setFloat(1, MIDiMIDjDistance);
  180. update.setInt(2, i);
  181. update.setInt(3, j);
  182.  
  183. update.executeUpdate();
  184.  
  185. update.close();
  186.  
  187. //System.out.println("Successfully updated database for MID" + i + " and MID " + j +"!");
  188. }
  189. // otherwise, just add MIDiMIDjDistance to the table
  190. else
  191. {
  192. PreparedStatement update = this.conn.prepareStatement(
  193. "INSERT INTO SIMILARITY "
  194. + "(MID1, MID2, SIMILARITY) "
  195. + "VALUES (?,?,?)");
  196.  
  197. update.setInt(1, i);
  198. update.setInt(2, j);
  199. update.setFloat(3, MIDiMIDjDistance);
  200.  
  201. update.executeUpdate();
  202.  
  203. update.close();
  204.  
  205. //System.out.println("Successfully added similarity of MID " + i + " MID " + j + " to database!");
  206. }
  207.  
  208. preparedStatementSim.close();
  209. resultSim.close();
  210. }
  211. }
  212. }
  213.  
  214. // overall 33*32 = 1056 rows
  215. this.conn.commit();
  216. }
  217. catch (Exception e) {
  218. e.printStackTrace();
  219. }
  220. }
  221.  
  222. /* The function retrieves in ascending order from the database all the items that the similarity
  223. between them and the given item is at least 0.3. */
  224. public void printSimilarItems(long mid) throws SQLException {
  225. // [MID, Similarity]
  226. HashMap<Integer, Float> similarities = new HashMap<Integer, Float>();
  227. ArrayList<Integer> orderedMIDs = new ArrayList<Integer>();
  228.  
  229. String query =
  230. "SELECT MID1, MID2, SIMILARITY "
  231. + "From Similarity "
  232. + "WHERE SIMILARITY >= 0.3 AND MID1 = ? OR MID2 = ? AND MID1 != MID2 "
  233. + "ORDER BY SIMILARITY ASC";
  234.  
  235. PreparedStatement preparedStatement = this.conn.prepareStatement(query);
  236. preparedStatement.setInt(1, (int)mid);
  237. preparedStatement.setInt(2, (int)mid);
  238.  
  239. ResultSet result = preparedStatement.executeQuery();
  240.  
  241. while(result.next()) {
  242. int mid1 = result.getInt(1);
  243. int mid2 = result.getInt(2);
  244. float sim = result.getFloat(3);
  245.  
  246. if(mid1 != (int)mid && !similarities.containsKey(mid1)) {
  247. similarities.put(mid1, sim);
  248. orderedMIDs.add(mid1);
  249. }
  250.  
  251. if(mid2 != (int)mid && !similarities.containsKey(mid2)) {
  252. similarities.put(mid2, sim);
  253. orderedMIDs.add(mid2);
  254. }
  255. }
  256.  
  257. preparedStatement.close();
  258. result.close();
  259.  
  260. // The function prints all the titles and the similarity value of the similar items
  261. for(int i = 0; i < orderedMIDs.size(); i++) {
  262. PreparedStatement preparedStatementTitles = conn.prepareStatement("SELECT TITLE FROM MEDIAITEMS WHERE MID = ?");
  263. preparedStatementTitles.setInt(1, orderedMIDs.get(i));
  264.  
  265. ResultSet resultTitles = preparedStatementTitles.executeQuery();
  266.  
  267. if(resultTitles.next()) {
  268. // print title of the MID & the similarity
  269. System.out.println(resultTitles.getString(1) + ", " + similarities.get(orderedMIDs.get(i)));
  270. }
  271.  
  272. preparedStatementTitles.close();
  273. resultTitles.close();
  274. }
  275.  
  276. //System.out.println(similarities);
  277. }
  278.  
  279. public static void main(String[] args) throws ClassNotFoundException, SQLException, IOException{
  280. final String username = "shacharo";
  281. final String password = "abcd";
  282. final String connectionUrl = "jdbc:oracle:thin:@ora1.ise.bgu.ac.il:1521/oracle";
  283.  
  284. Task1 task = new Task1(connectionUrl, username, password);
  285. // 1.2.1
  286. //task.fileToDataBase("d:\\documents\\users\\rinag\\Documents\\films.csv");
  287.  
  288. // 1.2.2
  289. //task.calculateSimilarity();
  290.  
  291. //1.2.3
  292. //task.printSimilarItems(1);
  293. }
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement