add db002 subdirectory
[se-panther.git] / db002 / src / DB002.java
blob32e0244a75e627be3bbd203654885ae121bceaf7
1 import java.sql.*;
2 import java.util.Date;
4 public class DB002
6 private final static String DB_URL = "jdbc:mysql://robocomp.dyndns.org/team3";
7 private final static String DB_USER = "team3";
8 private final static String DB_PASS = "team3";
10 private final static String STUDENT = "Thompson";
12 private int id_student;
13 private RepetoireList list_repetoire;
14 private ScalesList list_scales;
15 private SightreadingList list_sightreading;
17 public DB002()
19 Connection conn = null;
21 try {
22 conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
23 System.out.println("Database connected");
25 id_student = get_student_id(conn, STUDENT);
26 list_repetoire = get_student_repetoire(conn, id_student);
27 list_scales = get_student_scales(conn, id_student);
28 list_sightreading = get_student_sightreading(conn, id_student);
30 catch (SQLException se) {
31 se.printStackTrace();
33 finally {
34 try {
35 if (conn != null) {
36 conn.close();
37 System.out.println("Database disconnected");
40 catch (Throwable t)
41 { }
45 public String toString()
47 StringBuilder sb = new StringBuilder();
49 sb.append(STUDENT).append("\n");
51 sb.append(list_repetoire.get(0)).append("\n").
52 append(list_scales.get(0)).append("\n").
53 append(list_sightreading.get(0));
55 return sb.toString();
58 public static void main(String[] args)
60 DB002 app = new DB002();
62 System.out.println(app);
65 /* *note - in the event that there are > 1 students with the same last name,
66 * we just return the first id in the database. */
67 private int get_student_id(Connection conn, String lname)
68 throws SQLException
70 if (conn == null) {
71 return -1;
74 Statement st = conn.createStatement();
75 st.executeQuery("select * from student where LName=\'" + lname +
76 "\';");
78 ResultSet result_set = st.getResultSet();
79 if (!result_set.next()) {
80 return -1;
83 int id = result_set.getInt("idStudent");
84 return id;
87 private RepetoireList get_student_repetoire(Connection conn, int sid)
88 throws SQLException
90 if (conn == null) {
91 return null;
94 Statement st = conn.createStatement();
95 st.executeQuery("select * from repetoire where Student_idStudent=" +
96 sid + ";");
98 return new RepetoireList(st.getResultSet());
101 private ScalesList get_student_scales(Connection conn, int sid)
102 throws SQLException
104 if (conn == null) {
105 return null;
108 Statement st = conn.createStatement();
109 st.executeQuery("select * from scales where Student_idStudent=" +
110 sid + ";");
112 return new ScalesList(st.getResultSet());
115 private SightreadingList get_student_sightreading(Connection conn, int sid)
116 throws SQLException
118 if (conn == null) {
119 return null;
122 Statement st = conn.createStatement();
123 st.executeQuery("select * from sightreading where Student_idStudent=" +
124 sid + ";");
126 return new SightreadingList(st.getResultSet());