-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresetdatabase.jsp
48 lines (39 loc) · 1.22 KB
/
resetdatabase.jsp
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
<%@ page import="java.sql.*" %>
<%@ page import="java.util.Scanner" %>
<%@ page import="java.io.File" %>
<%@ include file="jdbc.jsp" %>
<%
// Check if user is logged in
if (session.getAttribute("customerId") == null) {
response.sendRedirect("account.jsp?redirect=resetdatabase.jsp");
return; // So that no attempt is made to run the rest of the file
}
getConnection();
String fileName = "/usr/local/tomcat/webapps/shop/ddl/orderdb_sql.ddl";
try
{
// Drop the database
con.createStatement().execute("USE master; ALTER DATABASE orders SET SINGLE_USER WITH ROLLBACK IMMEDIATE; DROP DATABASE orders;");
// Create statement
Statement stmt = con.createStatement();
Scanner scanner = new Scanner(new File(fileName));
// Read commands separated by ;
scanner.useDelimiter(";");
while (scanner.hasNext()) {
String command = scanner.next();
if (command.trim().equals("") || command.trim().equals("go"))
continue;
try
{
stmt.execute(command);
}
catch (Exception e) {
out.print(e);
}
}
scanner.close();
} catch (Exception e) {
out.print(e);
}
response.sendRedirect("admin.jsp");
%>