· 9 years ago · Jul 22, 2017, 02:06 AM
1import java.sql.*;
2import java.util.*;
3
4public class Jdbc {
5 /**
6 * Main program for JDBC Degrees of Seperation
7 */
8 public static void main(String[] argv) throws Exception{
9 getConnection();
10 degreesOfSeperation(1);
11 }
12
13 //Creates a Connection to the Database
14 public static Connection getConnection() throws Exception{
15 // Load the driver.
16 Class.forName("com.mysql.jdbc.Driver");
17 // The users username, in this case it is manually put in below
18 String user = "lernerb";
19 // The users password, in this case it is manually put in below
20 String pw = "lernerb";
21 // Make a connection to the server.
22 Connection c = DriverManager.getConnection
23 ("jdbc:mysql://db.voltagepcs.com/lernerb", user, pw);
24 return c;
25 }
26
27 //Method that finds the degrees of seperation from the musician
28 //with the the given musid
29 public static int degreesOfSeperation(int musid) throws Exception{
30 int deg = 0;
31
32 //Statement to create the temporary table if it does not exist
33 Statement t = getConnection().createStatement();
34 t.execute("Create table if not exists degree(" +
35 "id int primary key, deg int, name varchar(255));");
36
37 //Put everyone's name and ID into the table, with DEG as 1
38 PreparedStatement ps = getConnection().prepareStatement
39 ("select distinct p.name, p.id from Person p, memberOf m, Band b where " +
40 "? = m.person and m.band = b.id");
41 //("Select person From memberOf Where band = " +
42 // "(Select band From memberOf Where person = ?)");
43 //The comment above works if a person is only part of one band, which is not true.
44
45 //Set the parameter.
46 ps.setInt(1, musid);
47 //Execute the query.
48 ResultSet rs = ps.executeQuery();
49 while (rs.next()) {
50 //System.out.println(rs.getString("name"));
51
52 }
53 return 2;
54
55
56
57
58
59 }
60}