forked from mkerr64/9bitDev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DatabaseManager.java
93 lines (81 loc) · 2.96 KB
/
DatabaseManager.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
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class DatabaseManager {
Connection c = null;
Statement statement = null;
public DatabaseManager(){
try{
Class.forName("com.mysql.jdbc.Driver");
}
catch (ClassNotFoundException e) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
}
}
//Inserts a transaction into the database
public void insert_transaction(Integer id, float amount, String category){
try{
//Opens a connection to the database file
c = DriverManager.getConnection("jdbc:sqlite:user_data.db");
c.setAutoCommit(false);
//Queries the SQL statement
statement = c.createStatement();
statement.executeUpdate("INSERT INTO Transactions (Id,Amount,Category) VALUES ("+id+", "+amount+", '"+category+"');");
//Commits the changes and closes the database connection
statement.close();
c.commit();
c.close();
}
catch(SQLException e){
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
}
}
//Inserts an account into the database
public void insert_account(String user, String password){
try{
//Opens a connection to the database file
c = DriverManager.getConnection("jdbc:sqlite:user_data.db");
c.setAutoCommit(false);
//Queries the SQL statement
statement = c.createStatement();
statement.executeUpdate("INSERT INTO Transactions (Username, Password) VALUES ('"+user+"', '"+password+"');");
//Commits the changes and closes the database connection
statement.close();
c.commit();
c.close();
}
catch(SQLException e){
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
}
}
//Returns a list of all the transaction associated with a user's ID
public void/*List<Transaction>*/ getTransactions(int id){
//List<Transaction> transaction_list = new ArrayList<Transaction>();
try{
//Opens a connection to the database file
c = DriverManager.getConnection("jdbc:sqlite:user_data.db");
c.setAutoCommit(false);
//Queries the SQL statement
statement = c.createStatement();
ResultSet results = statement.executeQuery("SELECT * FROM Transactions WHERE Id="+id+" ORDER BY Year DESC, Month DESC, Day DESC;");
while(results.next()){
float amount = results.getFloat("Amount");
String category = results.getString("Category");
int year = results.getInt("Year");
int month = results.getInt("Month");
int day = results.getInt("Day");
String date = Integer.toString(month)+"/"+Integer.toString(day)+"/"+Integer.toString(year);
//Transaction new_trans = new Transaction(amount, category, date);
//transaction_list.add(new_trans);
}
//Commits the changes and closes the database connection
results.close();
statement.close();
c.commit();
c.close();
}
catch(SQLException e){
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
}
}
}