Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. //Method that finds the degrees of seperation from the musician
  2. //with the the given musid. Since I couldn't figure out how to
  3. public static int degreesOfSeperation(int musid) throws Exception{
  4. return degreesOfSeperationHelper(musid, 0);
  5. }
  6.  
  7. //Helper method to count the degrees of seperation
  8. public static int degreesOfSeperationHelper(int musid, int deg) throws Exception{
  9.  
  10. //Statement to create the temporary table if it doesn't exist
  11. PreparedStatement t = getConnection().prepareStatement(
  12. "Create table if not exists degreeofsep(" +
  13. "id int primary key, deg int, name varchar(255));");
  14. t.execute();
  15.  
  16. //Statement to get the name of the person who has the musid
  17. PreparedStatement getName = getConnection().prepareStatement(
  18. ("Select p.name From Person p Where p.id = ?"));
  19. getName.setInt(1, musid);
  20. ResultSet theName = getName.executeQuery();
  21.  
  22. while (theName.next()) {
  23. //Insert initial data into the table
  24. PreparedStatement start = getConnection().prepareStatement(
  25. "Insert into degreeofsep values(?, ?," + theName.getString("name"));
  26.  
  27. start.setInt(1, musid);
  28. start.setInt(2, deg);
  29. start.execute();
  30. }
  31.  
  32.  
  33. //Query everyone that exists in a band.
  34. PreparedStatement ps = getConnection().prepareStatement
  35. ("select distinct p.name, p.id from Person p, memberOf m, Band b where " +
  36. "? = m.person and m.band = b.id");
  37. //Set the parameter.
  38. ps.setInt(1, musid);
  39.  
  40.  
  41.  
  42. //("Select person From memberOf Where band = " +
  43. // "(Select band From memberOf Where person = ?)");
  44. //The comment above works if a person is only part of one band, which is not true.
  45.  
  46. //Execute the query.
  47. ////ResultSet rs = ps.executeQuery();
  48. //while (rs.next()) {
  49. //System.out.println(rs.getString("name"));
  50. //}
  51. return 2;
  52. //}
  53. }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement