|
@@ -1,22 +1,16 @@
|
1
|
|
-import com.j256.ormlite.dao.Dao;
|
2
|
|
-import com.j256.ormlite.dao.DaoManager;
|
|
1
|
+
|
3
|
2
|
import com.j256.ormlite.jdbc.JdbcConnectionSource;
|
4
|
3
|
import com.j256.ormlite.support.ConnectionSource;
|
5
|
4
|
|
6
|
|
-import java.util.List;
|
7
|
|
-import java.util.Scanner;
|
8
|
5
|
|
9
|
6
|
public class AccountApp {
|
10
|
7
|
|
11
|
8
|
// we are using a MySQl database
|
12
|
9
|
private final static String DATABASE_URL = "jdbc:mysql://localhost:3306/orm_lab?useUnicode=true";
|
13
|
|
- // Scanner
|
14
|
|
- Console c = new Console();
|
15
|
|
-
|
16
|
|
- private Dao<Account, Integer> accountDao;
|
|
10
|
+ private Console c = new Console();
|
17
|
11
|
|
18
|
12
|
public static void main(String[] args) throws Exception {
|
19
|
|
- // turn our static method into an instance of Main
|
|
13
|
+
|
20
|
14
|
new AccountApp().doMain(args);
|
21
|
15
|
}
|
22
|
16
|
|
|
@@ -25,33 +19,23 @@ public class AccountApp {
|
25
|
19
|
try {
|
26
|
20
|
// create our data-source for the database
|
27
|
21
|
connectionSource = new JdbcConnectionSource(DATABASE_URL, "root", "");
|
28
|
|
- // setup our DAOs
|
29
|
22
|
|
30
|
|
- setupDao(connectionSource);
|
31
|
|
- DatabaseConnection dbc = new MysqlDriver(connectionSource);
|
32
|
|
- MysqlDriver a = new MysqlDriver(connectionSource);;
|
33
|
|
- // read, write and delete some data
|
|
23
|
+ MysqlDriver a = new MysqlDriver(connectionSource);
|
|
24
|
+
|
34
|
25
|
processData(a);
|
35
|
26
|
|
36
|
|
- System.out.println("\n\nIt seems to have worked\n\n");
|
37
|
27
|
} finally {
|
38
|
|
- // destroy the data source which should close underlying connections
|
|
28
|
+
|
|
29
|
+ c.print("Good Bye!");
|
39
|
30
|
if (connectionSource != null) {
|
40
|
31
|
connectionSource.close();
|
41
|
32
|
}
|
42
|
33
|
}
|
43
|
34
|
}
|
44
|
35
|
|
45
|
|
- /**
|
46
|
|
- * Read and write some example data.
|
47
|
|
- */
|
48
|
36
|
private void processData(MysqlDriver a) throws Exception {
|
49
|
|
- // Service a = new Service (connectionSource){
|
50
|
|
-
|
51
|
37
|
|
52
|
38
|
boolean continueMenu = true;
|
53
|
|
- // Create an account interface for a user to interact with the app
|
54
|
|
- // CRUD
|
55
|
39
|
System.out.println("Welcome to the account database manager!\n");
|
56
|
40
|
|
57
|
41
|
while (continueMenu) {
|
|
@@ -69,57 +53,15 @@ public class AccountApp {
|
69
|
53
|
a.inputUpdateMenu();
|
70
|
54
|
|
71
|
55
|
} else if (input.equals("4")) {
|
72
|
|
- // destroy
|
73
|
|
- break;
|
|
56
|
+ a.readAllAccounts(a.read());
|
|
57
|
+ int destroyMe = a.menuDestroy();
|
|
58
|
+ a.destroy(a.getById(destroyMe));
|
74
|
59
|
|
75
|
60
|
} else {
|
76
|
61
|
continueMenu = false;
|
77
|
62
|
}
|
78
|
63
|
|
79
|
64
|
}
|
80
|
|
-
|
81
|
|
- // create an instance of Account
|
82
|
|
- String name = "Jim Coakley";
|
83
|
|
- Account account = new Account(name);
|
84
|
|
-
|
85
|
|
- // persist the account object to the database
|
86
|
|
- accountDao.create(account);
|
87
|
|
- int id = account.getId();
|
88
|
|
- System.out.println(id);
|
89
|
|
- // assign a password
|
90
|
|
- account.setPassword("_secret");
|
91
|
|
- // update the database after changing the object
|
92
|
|
- accountDao.update(account);
|
93
|
|
- // delete the account
|
94
|
|
- accountDao.deleteById(id);
|
95
|
|
- }
|
96
|
|
-
|
97
|
|
- /**
|
98
|
|
- * Setup our DAOs
|
99
|
|
- */
|
100
|
|
- private void setupDao(ConnectionSource connectionSource) throws Exception {
|
101
|
|
-
|
102
|
|
- accountDao = DaoManager.createDao(connectionSource, Account.class);
|
103
|
|
-
|
104
|
|
- }
|
105
|
|
-
|
106
|
|
- private void updateSpecificAccount() {
|
107
|
|
-
|
108
|
|
- // Grabs the account and sets either a new name or password for the account
|
109
|
|
-
|
110
|
|
- }
|
111
|
|
-
|
112
|
|
- private void updateName(int id) {
|
113
|
|
-
|
114
|
65
|
}
|
115
|
66
|
|
116
|
|
- private void updatePassword(int id) {
|
117
|
|
-
|
118
|
|
- }
|
119
|
|
-
|
120
|
|
- private void deleteAnAccount() {
|
121
|
|
-
|
122
|
|
- // removes the account from the database by ID
|
123
|
|
-
|
124
|
|
- }
|
125
|
67
|
}
|