-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDBI.java
More file actions
84 lines (79 loc) · 3.72 KB
/
DBI.java
File metadata and controls
84 lines (79 loc) · 3.72 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
import java.io.*;
import java.util.*;
import java.sql.*;
class DBI {
// Method to save download history to database
public static void saveDownloadHistory(String url, String fileName, long size, java.util.Date startTime, java.util.Date endTime, boolean success) {
Connection con = null;
Statement st = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/idm?characterEncoding=utf8", "root", "");
st = con.createStatement();
// Create the SQL query with proper formatting of values
String sql = "INSERT INTO download (url, file_name, size, start_time, end_time, success) VALUES (" +
"'" + url + "', '" + fileName + "', " + size + ", '" +
new java.sql.Timestamp(startTime.getTime()) + "', '" +
new java.sql.Timestamp(endTime.getTime()) + "', " + (success ? 1 : 0) + ")";
// Execute the SQL query
int rowsAffected = st.executeUpdate(sql);
// Check if the insertion was successful
if (rowsAffected > 0) {
System.out.println("\nDownload history stored successfully.");
} else {
System.out.println("Failed to store download history. No rows affected.");
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
System.out.println("Failed to store download history due to exception: " + e.getMessage());
} finally {
// Close the connection and statement
try {
if (st != null) {
st.close();
}
if (con != null) {
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void getHistory() {
Connection con = null;
Statement st = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/idm?characterEncoding=utf8", "root", "");
st = con.createStatement();
String str = "select * from download";
rs = st.executeQuery(str);
System.out.println("URL | File Name | Size | Start Time | End Time | Success");
System.out.println("------------------------------+--------------------+-----------+-------------------------+-------------------------+--------");
while (rs.next()) {
System.out.println(rs.getString("url") + " | " + rs.getString("file_name") + " | " + rs.getLong("size") +
" " + rs.getTimestamp("start_time") + " | " + rs.getTimestamp("end_time") + " | " + rs.getBoolean("success"));
System.out.println("------------------------------+--------------------+-----------+-------------------------+-------------------------+--------");
}
} catch (Exception e) {
System.out.println("Error " + e);
} finally {
// Close the connection, statement, and result set
try {
if (rs != null) {
rs.close();
}
if (st != null) {
st.close();
}
if (con != null) {
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}