-
Notifications
You must be signed in to change notification settings - Fork 0
/
InitialiseDB.java
141 lines (109 loc) · 4.35 KB
/
InitialiseDB.java
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.sql.*;
/**
* Initialises the 'movies' database by reading the DDL script
* to create the relational schema.
* Deletes the database file if it has already been created
*/
public class InitialiseDB {
/**
* Checks if the database file has already been created
* if so, it is deleted
* Creates the tables in the database and prints out the success message
*
* @param dbFileName the name of the database file to be created
* @throws SQLException if createTables throws SQL exception
*/
public static void initialiseDB(String dbFileName) throws SQLException, IOException {
// Create a File object to check that the database file exists
String dbUrl = "jdbc:sqlite:" + dbFileName;
//checks if the database already exists, if so deletes it
if (databaseExists(dbUrl)) {
if (deleteDatabase(dbUrl))
System.out.println("Database deleted successfully");
else
System.out.println("Failed to delete database");
}
//tries to establish a conneciton to the SQLite database
try (Connection connection = DriverManager.getConnection(dbUrl)) {
//creates the tables in the database
createTables(connection);
// testing function that checks that everything was created properly
} catch (SQLException | IOException e) {
e.printStackTrace();
}
}
/**
* Checks if the database file already exists
* @param dbUrl the JDBC url to the SQLite database
* @return returns true if the database already exists, otherwise returns false
*/
public static boolean databaseExists(String dbUrl) {
File dbFile = new File(dbUrl.replace("jdbc:sqlite:", ""));
return dbFile.exists();
}
/**
* Deletes the database file
* @param dbUrl the JDBC url to the SQLite database
* @return true if the database was deleted successfully, otherwise returns false
*/
public static boolean deleteDatabase(String dbUrl) {
File dbFile = new File(dbUrl.replace("jdbc:sqlite:", ""));
return dbFile.delete();
}
/**
* Creates the tables in the database
* @param conn conneciton to the JDBC database
* @throws SQLException,IOException if there is an SQL error in the DDL script or
* the file is unreadable or does not exist
*/
private static void createTables(Connection conn) throws SQLException, IOException {
Statement statement = conn.createStatement();
String ddlFileName = "ddl.sql";
try {
//reads the DDL file
String ddlString = readScript(ddlFileName);
statement.executeUpdate(ddlString);
statement.close();
System.out
.println("Successfully created tables: actors, movies, movie_industry, awards, directors, ratings");
} catch (SQLException | IOException e) {
e.printStackTrace();
}
}
/**
* Reads each line of a file
* returns the whole file as a String object
* @param filepath the filepath of the file to be read from
* @return String of the whole file
* @throws IOException if the file is inaccessible or does not exist
*/
public static String readScript(String filepath) throws IOException {
StringBuilder sb = new StringBuilder();
try (BufferedReader reader = new BufferedReader(new FileReader(filepath))) {
String line;
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
return sb.toString();
}
}
/**
* Main method to read the command-line arguments
* and initialise the database
* @param args command-line arguments
* @throws SQLException if there is an SQL error while initialization
*/
public static void main(String[] args) throws SQLException, IOException {
if(args.length < 1) {
System.out.println("Usage: java InitialiseDB <database_file_name>");
System.exit(0);
}
//sets the database file name to the first argument
String dbFileName = args[0];
initialiseDB(dbFileName);
}
}