CS1103/Labs/Lab8/IsaacShoebottom_CS1103_Lab8/Question2.java
2022-10-07 00:48:09 -03:00

49 lines
1.7 KiB
Java

import java.sql.*;
public class Question2 {
public static void main(String[] args) {
if(args.length < 1) {
System.err.println("Usage: Question2 <StudentID>");
System.exit(-1);
}
Question2 question2 = new Question2();
Connection conn = question2.openConnection();
String studentID = args[0];
try {
String query = "{CALL studentTranscript(?)}";
CallableStatement statement = conn.prepareCall(query);
statement.setString(1, studentID);
ResultSet rs = statement.executeQuery();
while(rs.next()) {
String row = rs.getString(1) + ": " + rs.getString(2);
System.out.println(row);
}
} catch (SQLException sqlException) {
System.err.println(sqlException.getMessage());
}
question2.closeConnection(conn);
}
private Connection openConnection() {
final String url = "jdbc:mysql://cs1103.cs.unb.ca:3306/ishoebot";
final String user = "ishoebot";
final String password = "k7FOLH5B";
Connection conn = null;
try {
conn = DriverManager.getConnection(url, user, password);
}
catch (Exception exception) {
System.err.printf("Couldn't open a connection: (%s)", exception.getMessage());
}
return conn;
}
private void closeConnection(Connection conn) {
try {
conn.close();
}
catch (Exception exception) {
System.err.printf("Couldn't close connection: (%s)", exception.getMessage());
}
}
}