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

55 lines
2.1 KiB
Java

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Scanner;
public class Question1 {
public static void main(String[] args) {
Question1 question1 = new Question1();
Connection conn = question1.openConnection();
Scanner sc = new Scanner(System.in);
System.out.print("What is the name of the student?: ");
String studentName = sc.nextLine();
System.out.print("What is the email address of the student?: ");
String emailAddress = sc.nextLine();
System.out.print("What is the high school average of the student?: ");
String highSchoolAverage = sc.nextLine();
try {
String query = "{CALL AcceptStudent(?, ?, ?)}";
CallableStatement statement = conn.prepareCall(query);
statement.setString(1, studentName);
statement.setString(2, emailAddress);
statement.setString(3, highSchoolAverage);
statement.executeQuery();
} catch (SQLException sqlException) {
System.err.println(sqlException.getMessage());
}
System.out.println(studentName + " with email address: " + emailAddress + " and a high school average of: " + highSchoolAverage + " added");
question1.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());
}
}
}