|
@@ -0,0 +1,197 @@
|
|
1
|
+# Part 1 - Create MySQL Database
|
|
2
|
+1. Start MySQL from console
|
|
3
|
+2. Create a new Database named 'orm-lab'
|
|
4
|
+3. Inside that database create a new table name 'account'
|
|
5
|
+4. the schema should be as folllow
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+ * id INT NOT NULL AUTO_INCREMENT
|
|
9
|
+ * name VARCHAR(20)
|
|
10
|
+ * password VARCHAR(20)
|
|
11
|
+ * PRIMARY KEY = id
|
|
12
|
+
|
|
13
|
+# PART 2 - CREATE JAVA APP
|
|
14
|
+
|
|
15
|
+## Adding dependencies
|
|
16
|
+1 Create a new Java Maven project
|
|
17
|
+2 Update the pom.xml file by adding the following dependencies
|
|
18
|
+```
|
|
19
|
+ <!-- https://mvnrepository.com/artifact/com.j256.ormlite/ormlite-core -->
|
|
20
|
+ <dependency>
|
|
21
|
+ <groupId>com.j256.ormlite</groupId>
|
|
22
|
+ <artifactId>ormlite-core</artifactId>
|
|
23
|
+ <version>4.48</version>
|
|
24
|
+ </dependency>
|
|
25
|
+
|
|
26
|
+ <!-- https://mvnrepository.com/artifact/com.j256.ormlite/ormlite-jdbc -->
|
|
27
|
+ <dependency>
|
|
28
|
+ <groupId>com.j256.ormlite</groupId>
|
|
29
|
+ <artifactId>ormlite-jdbc</artifactId>
|
|
30
|
+ <version>4.48</version>
|
|
31
|
+ </dependency>
|
|
32
|
+ ```
|
|
33
|
+These are the dependencies for the ORM library we will be using
|
|
34
|
+
|
|
35
|
+3 Then also add the following
|
|
36
|
+
|
|
37
|
+```
|
|
38
|
+ <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
|
|
39
|
+ <dependency>
|
|
40
|
+ <groupId>mysql</groupId>
|
|
41
|
+ <artifactId>mysql-connector-java</artifactId>
|
|
42
|
+ <version>5.1.6</version>
|
|
43
|
+ </dependency>
|
|
44
|
+
|
|
45
|
+```
|
|
46
|
+## Create Application Model
|
|
47
|
+Create a new Java class name 'Account'
|
|
48
|
+
|
|
49
|
+```
|
|
50
|
+
|
|
51
|
+import com.j256.ormlite.field.DatabaseField;
|
|
52
|
+import com.j256.ormlite.table.DatabaseTable;
|
|
53
|
+
|
|
54
|
+@DatabaseTable(tableName = "account")
|
|
55
|
+public class Account {
|
|
56
|
+
|
|
57
|
+ // for QueryBuilder to be able to find the fields
|
|
58
|
+ public static final String NAME_FIELD_NAME = "name";
|
|
59
|
+ public static final String PASSWORD_FIELD_NAME = "password";
|
|
60
|
+
|
|
61
|
+ @DatabaseField(columnName = "id", generatedId = true)
|
|
62
|
+ private int id;
|
|
63
|
+
|
|
64
|
+ @DatabaseField(columnName = NAME_FIELD_NAME, canBeNull = false)
|
|
65
|
+ private String name;
|
|
66
|
+
|
|
67
|
+ @DatabaseField(columnName = PASSWORD_FIELD_NAME)
|
|
68
|
+ private String password;
|
|
69
|
+
|
|
70
|
+ Account() {
|
|
71
|
+ // all persisted classes must define a no-arg constructor with at least package visibility
|
|
72
|
+ }
|
|
73
|
+
|
|
74
|
+ public Account(String name) {
|
|
75
|
+ this.name = name;
|
|
76
|
+ }
|
|
77
|
+
|
|
78
|
+ public Account(String name, String password) {
|
|
79
|
+ this.name = name;
|
|
80
|
+ this.password = password;
|
|
81
|
+ }
|
|
82
|
+
|
|
83
|
+ public int getId() {
|
|
84
|
+ return id;
|
|
85
|
+ }
|
|
86
|
+
|
|
87
|
+ public String getName() {
|
|
88
|
+ return name;
|
|
89
|
+ }
|
|
90
|
+
|
|
91
|
+ public void setName(String name) {
|
|
92
|
+ this.name = name;
|
|
93
|
+ }
|
|
94
|
+
|
|
95
|
+ public String getPassword() {
|
|
96
|
+ return password;
|
|
97
|
+ }
|
|
98
|
+
|
|
99
|
+ public void setPassword(String password) {
|
|
100
|
+ this.password = password;
|
|
101
|
+ }
|
|
102
|
+
|
|
103
|
+ @Override
|
|
104
|
+ public int hashCode() {
|
|
105
|
+ return name.hashCode();
|
|
106
|
+ }
|
|
107
|
+
|
|
108
|
+ @Override
|
|
109
|
+ public boolean equals(Object other) {
|
|
110
|
+ if (other == null || other.getClass() != getClass()) {
|
|
111
|
+ return false;
|
|
112
|
+ }
|
|
113
|
+ return name.equals(((Account) other).name);
|
|
114
|
+ }
|
|
115
|
+}
|
|
116
|
+```
|
|
117
|
+## Implement the ORM
|
|
118
|
+
|
|
119
|
+Create a Java class name 'AccountApp'
|
|
120
|
+
|
|
121
|
+```
|
|
122
|
+public class AccountApp {
|
|
123
|
+
|
|
124
|
+ // we are using a MySQl database
|
|
125
|
+ private final static String DATABASE_URL = "jdbc:mysql://localhost:3306/orm-lab?useUnicode=true";
|
|
126
|
+
|
|
127
|
+ private Dao<Account, Integer> accountDao;
|
|
128
|
+
|
|
129
|
+ public static void main(String[] args) throws Exception {
|
|
130
|
+ // turn our static method into an instance of Main
|
|
131
|
+ new AccountApp().doMain(args);
|
|
132
|
+ }
|
|
133
|
+
|
|
134
|
+ private void doMain(String[] args) throws Exception {
|
|
135
|
+ ConnectionSource connectionSource = null;
|
|
136
|
+ try {
|
|
137
|
+ // create our data-source for the database
|
|
138
|
+ connectionSource = new JdbcConnectionSource(DATABASE_URL, "root", "");
|
|
139
|
+ // setup our DAOs
|
|
140
|
+ setupDao(connectionSource);
|
|
141
|
+ // read, write and delete some data
|
|
142
|
+ processData();
|
|
143
|
+
|
|
144
|
+ System.out.println("\n\nIt seems to have worked\n\n");
|
|
145
|
+ } finally {
|
|
146
|
+ // destroy the data source which should close underlying connections
|
|
147
|
+ if (connectionSource != null) {
|
|
148
|
+ connectionSource.close();
|
|
149
|
+ }
|
|
150
|
+ }
|
|
151
|
+ }
|
|
152
|
+
|
|
153
|
+ /**
|
|
154
|
+ * Read and write some example data.
|
|
155
|
+ */
|
|
156
|
+ private void processData() throws Exception {
|
|
157
|
+ // create an instance of Account
|
|
158
|
+ String name = "Jim Coakley";
|
|
159
|
+ Account account = new Account(name);
|
|
160
|
+
|
|
161
|
+ // persist the account object to the database
|
|
162
|
+ accountDao.create(account);
|
|
163
|
+ int id = account.getId();
|
|
164
|
+ System.out.println(id);
|
|
165
|
+ // assign a password
|
|
166
|
+ account.setPassword("_secret");
|
|
167
|
+ // update the database after changing the object
|
|
168
|
+ accountDao.update(account);
|
|
169
|
+ // delete the account
|
|
170
|
+ //accountDao.deleteById(id);
|
|
171
|
+ }
|
|
172
|
+
|
|
173
|
+ /**
|
|
174
|
+ * Setup our DAOs
|
|
175
|
+ */
|
|
176
|
+ private void setupDao(ConnectionSource connectionSource) throws Exception {
|
|
177
|
+
|
|
178
|
+ accountDao = DaoManager.createDao(connectionSource, Account.class);
|
|
179
|
+
|
|
180
|
+ }
|
|
181
|
+}
|
|
182
|
+```
|
|
183
|
+
|
|
184
|
+Now if we run this we should see some info in the console of IntelliJ telling us that the program ran ok. And if we look over to MySQL we can query the table and see that our code has create and updated a record in out database
|
|
185
|
+
|
|
186
|
+! Remember MySQL needs to be up and running
|
|
187
|
+
|
|
188
|
+# Part 3
|
|
189
|
+
|
|
190
|
+If you have made it this far and you're able to run the program properly, Congratulations!
|
|
191
|
+
|
|
192
|
+The program this far only runs through a few lines of code before it exits. Upgrade the program so that it enters a control loop and allows the user to interact with your database until they quit the program. You will need to impletment CRUD functionality in the form of an account service. Dont forget to visit the ORMLite api as a reference for what you can do with the library
|
|
193
|
+
|
|
194
|
+### References
|
|
195
|
+* [OrmLite core API](http://ormlite.com/javadoc/ormlite-core/)
|
|
196
|
+* [OrmLite JDBC](http://ormlite.com/javadoc/ormlite-jdbc/)
|
|
197
|
+* [What is CRUD](https://en.wikipedia.org/wiki/Create,_read,_update_and_delete)
|