|
@@ -1,6 +1,6 @@
|
1
|
1
|
# Part 1 - Create MySQL Database
|
2
|
2
|
1. Start MySQL from console
|
3
|
|
-2. Create a new Database named 'orm-lab'
|
|
3
|
+2. Create a new Database named 'orm_lab'
|
4
|
4
|
3. Inside that database create a new table name 'account'
|
5
|
5
|
4. the schema should be as folllow
|
6
|
6
|
|
|
@@ -14,6 +14,7 @@
|
14
|
14
|
|
15
|
15
|
## Adding dependencies
|
16
|
16
|
1 Create a new Java Maven project
|
|
17
|
+
|
17
|
18
|
2 Update the pom.xml file by adding the following dependencies. These are the dependencies for the ORM library we will be using.
|
18
|
19
|
|
19
|
20
|
```
|
|
@@ -48,24 +49,22 @@
|
48
|
49
|
Create a new Java class name 'Account'
|
49
|
50
|
|
50
|
51
|
```
|
51
|
|
-
|
52
|
52
|
import com.j256.ormlite.field.DatabaseField;
|
53
|
53
|
import com.j256.ormlite.table.DatabaseTable;
|
54
|
54
|
|
55
|
55
|
@DatabaseTable(tableName = "account")
|
56
|
56
|
public class Account {
|
57
|
57
|
|
58
|
|
- // for QueryBuilder to be able to find the fields
|
59
|
58
|
public static final String NAME_FIELD_NAME = "name";
|
60
|
59
|
public static final String PASSWORD_FIELD_NAME = "password";
|
61
|
60
|
|
62
|
61
|
@DatabaseField(columnName = "id", generatedId = true)
|
63
|
62
|
private int id;
|
64
|
63
|
|
65
|
|
- @DatabaseField(columnName = NAME_FIELD_NAME, canBeNull = false)
|
|
64
|
+ @DatabaseField(columnName = "name", canBeNull = false)
|
66
|
65
|
private String name;
|
67
|
66
|
|
68
|
|
- @DatabaseField(columnName = PASSWORD_FIELD_NAME)
|
|
67
|
+ @DatabaseField(columnName = "password")
|
69
|
68
|
private String password;
|
70
|
69
|
|
71
|
70
|
Account() {
|
|
@@ -122,10 +121,15 @@ The things to notice here are the annotations. These annotations tell OrmLite ho
|
122
|
121
|
Create a Java class name 'AccountApp'
|
123
|
122
|
|
124
|
123
|
```
|
|
124
|
+import com.j256.ormlite.dao.Dao;
|
|
125
|
+import com.j256.ormlite.dao.DaoManager;
|
|
126
|
+import com.j256.ormlite.jdbc.JdbcConnectionSource;
|
|
127
|
+import com.j256.ormlite.support.ConnectionSource;
|
|
128
|
+
|
125
|
129
|
public class AccountApp {
|
126
|
130
|
|
127
|
131
|
// we are using a MySQl database
|
128
|
|
- private final static String DATABASE_URL = "jdbc:mysql://localhost:3306/orm-lab?useUnicode=true";
|
|
132
|
+ private final static String DATABASE_URL = "jdbc:mysql://localhost:3306/orm_lab?useUnicode=true";
|
129
|
133
|
|
130
|
134
|
private Dao<Account, Integer> accountDao;
|
131
|
135
|
|
|
@@ -183,7 +187,7 @@ public class AccountApp {
|
183
|
187
|
}
|
184
|
188
|
}
|
185
|
189
|
```
|
186
|
|
-Some of the things to take notice of are the objects that libraries offer use:
|
|
190
|
+Some of the things in this class to take notice are the objects that the imported libraries offer us:
|
187
|
191
|
|
188
|
192
|
* JdbcConnectionSource from the JDBC library
|
189
|
193
|
* Dao and DaoManager from the OrmLite
|