-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabase.java
More file actions
86 lines (78 loc) · 3.71 KB
/
Database.java
File metadata and controls
86 lines (78 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import java.sql.*;
/**
* The Database class contains helper functions to connect and test your
* connection with the database.
*/
public class Database {
/**
* Creates a connection to the database using environment variables. The following environment
* variables must be set prior to running your program:
*
* <ul>
* <li>CS410_PORT - The database management system port
* <li>CS410_HOST - The database management system host
* <li>CS410_USERNAME - The database management system username
* <li>CS410_PASSWORD - The database management system user's password
* <li>CS410_DATABASE - The name of the database in the database management system
* </ul>
*
* For more information on environment variables see:
* <a href="https://docs.oracle.com/javase/tutorial/essential/environment/env.html">
* https://docs.oracle.com/javase/tutorial/essential/environment/env.html
* </a>
* @return java.sql.Connection
* @throws SQLException
*/
public static Connection getDatabaseConnection() throws SQLException {
int databasePort = Integer.parseInt(System.getenv("CS410_PORT"));
String databaseHost = System.getenv("CS410_HOST");
String databaseUsername = System.getenv("CS410_USERNAME");
String databasePassword = System.getenv("CS410_PASSWORD");
String databaseName = System.getenv("CS410_DATABASE");
String databaseURL = String.format(
"jdbc:mysql://%s:%s/%s?verifyServerCertificate=false&useSSL=false&serverTimezone=UTC",
databaseHost,
databasePort,
databaseName);
try {
return DriverManager.getConnection(databaseURL, databaseUsername, databasePassword);
} catch (SQLException sqlException) {
System.out.printf("SQLException was thrown while trying to connection to database: %s%n", databaseURL);
System.out.println(sqlException.getMessage());
throw sqlException;
}
}
/**
* Tests the connection to your database. If your connection fails, please check:
* <ul>
* <li>Database is running
* <li>Environment variables are set and being read in properly
* <li>Database Driver is in the CLASSPATH.
* <li>SSH port forwarding is properly setup
* </ul>
*/
public static void testConnection() {
System.out.println("Attempting to connect to MySQL database using:");
/*System.out.printf("CS410_HOST: %s%n", System.getenv("CS410_HOST"));
System.out.printf("CS410_PORT: %s%n", System.getenv("CS410_PORT"));
System.out.printf("CS410_USERNAME: %s%n", System.getenv("CS410_USERNAME"));
System.out.printf("CS410_PASSWORD: %s%n", System.getenv("CS410_PASSWORD"));
System.out.printf("CS410_DATABASE: %s%n", System.getenv("CS410_DATABASE"));
*/
Connection connection = null;
ResultSet resultSet = null;
try{
connection = getDatabaseConnection();
Statement sqlStatement = connection.createStatement();
String sql = "SELECT VERSION();";
resultSet = sqlStatement.executeQuery(sql);
resultSet.next();
System.out.printf("Connected SUCCESS! Database Version: %s%n", resultSet.getString(1));
} catch (SQLException sql){
System.out.println("Failed to connect to database! Please make sure your Environment variables are set!");
} finally {
try { resultSet.close(); } catch (Exception e) { /* ignored */ }
try { connection.close(); } catch (Exception e) { /* ignored */ }
}
}
}