瀏覽代碼

Commiting

chitraB 6 年之前
父節點
當前提交
ff5e8c900d

+ 13
- 0
.idea/compiler.xml 查看文件

1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<project version="4">
3
+  <component name="CompilerConfiguration">
4
+    <annotationProcessing>
5
+      <profile name="Maven default annotation processors profile" enabled="true">
6
+        <sourceOutputDir name="target/generated-sources/annotations" />
7
+        <sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
8
+        <outputRelativeToContentRoot value="true" />
9
+        <module name="SIMPLE_ACCOUNT" />
10
+      </profile>
11
+    </annotationProcessing>
12
+  </component>
13
+</project>

+ 0
- 13
.idea/libraries/Maven__com_j256_ormlite_ormlite_core_4_48.xml 查看文件

1
-<component name="libraryTable">
2
-  <library name="Maven: com.j256.ormlite:ormlite-core:4.48">
3
-    <CLASSES>
4
-      <root url="jar://$MAVEN_REPOSITORY$/com/j256/ormlite/ormlite-core/4.48/ormlite-core-4.48.jar!/" />
5
-    </CLASSES>
6
-    <JAVADOC>
7
-      <root url="jar://$MAVEN_REPOSITORY$/com/j256/ormlite/ormlite-core/4.48/ormlite-core-4.48-javadoc.jar!/" />
8
-    </JAVADOC>
9
-    <SOURCES>
10
-      <root url="jar://$MAVEN_REPOSITORY$/com/j256/ormlite/ormlite-core/4.48/ormlite-core-4.48-sources.jar!/" />
11
-    </SOURCES>
12
-  </library>
13
-</component>

+ 0
- 13
.idea/libraries/Maven__com_j256_ormlite_ormlite_jdbc_4_48.xml 查看文件

1
-<component name="libraryTable">
2
-  <library name="Maven: com.j256.ormlite:ormlite-jdbc:4.48">
3
-    <CLASSES>
4
-      <root url="jar://$MAVEN_REPOSITORY$/com/j256/ormlite/ormlite-jdbc/4.48/ormlite-jdbc-4.48.jar!/" />
5
-    </CLASSES>
6
-    <JAVADOC>
7
-      <root url="jar://$MAVEN_REPOSITORY$/com/j256/ormlite/ormlite-jdbc/4.48/ormlite-jdbc-4.48-javadoc.jar!/" />
8
-    </JAVADOC>
9
-    <SOURCES>
10
-      <root url="jar://$MAVEN_REPOSITORY$/com/j256/ormlite/ormlite-jdbc/4.48/ormlite-jdbc-4.48-sources.jar!/" />
11
-    </SOURCES>
12
-  </library>
13
-</component>

+ 0
- 13
.idea/libraries/Maven__mysql_mysql_connector_java_5_1_6.xml 查看文件

1
-<component name="libraryTable">
2
-  <library name="Maven: mysql:mysql-connector-java:5.1.6">
3
-    <CLASSES>
4
-      <root url="jar://$MAVEN_REPOSITORY$/mysql/mysql-connector-java/5.1.6/mysql-connector-java-5.1.6.jar!/" />
5
-    </CLASSES>
6
-    <JAVADOC>
7
-      <root url="jar://$MAVEN_REPOSITORY$/mysql/mysql-connector-java/5.1.6/mysql-connector-java-5.1.6-javadoc.jar!/" />
8
-    </JAVADOC>
9
-    <SOURCES>
10
-      <root url="jar://$MAVEN_REPOSITORY$/mysql/mysql-connector-java/5.1.6/mysql-connector-java-5.1.6-sources.jar!/" />
11
-    </SOURCES>
12
-  </library>
13
-</component>

+ 14
- 0
.idea/misc.xml 查看文件

1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<project version="4">
3
+  <component name="ExternalStorageConfigurationManager" enabled="true" />
4
+  <component name="MavenProjectsManager">
5
+    <option name="originalFiles">
6
+      <list>
7
+        <option value="$PROJECT_DIR$/pom.xml" />
8
+      </list>
9
+    </option>
10
+  </component>
11
+  <component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
12
+    <output url="file://$PROJECT_DIR$/out" />
13
+  </component>
14
+</project>

+ 0
- 211
README.md 查看文件

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
-
18
-2 Update the pom.xml file by adding the following dependencies. These are the dependencies for the ORM library we will be using.
19
-
20
-```
21
-        <!-- https://mvnrepository.com/artifact/com.j256.ormlite/ormlite-core -->
22
-        <dependency>
23
-            <groupId>com.j256.ormlite</groupId>
24
-            <artifactId>ormlite-core</artifactId>
25
-            <version>4.48</version>
26
-        </dependency>
27
-
28
-        <!-- https://mvnrepository.com/artifact/com.j256.ormlite/ormlite-jdbc -->
29
-        <dependency>
30
-            <groupId>com.j256.ormlite</groupId>
31
-            <artifactId>ormlite-jdbc</artifactId>
32
-            <version>4.48</version>
33
-        </dependency>
34
-```
35
-
36
-
37
-3 Then also add the following. This is a dependency that allows us to connect our Java code to MySql
38
-
39
-```
40
-        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
41
-        <dependency>
42
-            <groupId>mysql</groupId>
43
-            <artifactId>mysql-connector-java</artifactId>
44
-            <version>5.1.6</version>
45
-        </dependency>
46
-
47
-```
48
-## Create Application Model
49
-Create a new Java class name 'Account'
50
-
51
-```
52
-import com.j256.ormlite.field.DatabaseField;
53
-import com.j256.ormlite.table.DatabaseTable;
54
-
55
-@DatabaseTable(tableName = "account")
56
-public class Account {
57
-
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", canBeNull = false)
65
-    private String name;
66
-
67
-    @DatabaseField(columnName = "password")
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
-The things to notice here are the annotations. These annotations tell OrmLite how to relate our Java Objects to the records in MySQL
118
-
119
-## Implement the ORM
120
-
121
-Create a Java class name 'AccountApp'
122
-
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
-
129
-public class AccountApp {
130
-
131
-    // we are using a MySQl database
132
-    private final static String DATABASE_URL = "jdbc:mysql://localhost:3306/orm_lab?useUnicode=true";
133
-
134
-    private Dao<Account, Integer> accountDao;
135
-
136
-    public static void main(String[] args) throws Exception {
137
-        // turn our static method into an instance of Main
138
-        new AccountApp().doMain(args);
139
-    }
140
-
141
-    private void doMain(String[] args) throws Exception {
142
-        ConnectionSource connectionSource = null;
143
-        try {
144
-            // create our data-source for the database
145
-            connectionSource = new JdbcConnectionSource(DATABASE_URL, YOURUSERNAME, YOURPASSWORD);
146
-            // setup our  DAOs
147
-            setupDao(connectionSource);
148
-            // read, write and delete some data
149
-            processData();
150
-
151
-            System.out.println("\n\nIt seems to have worked\n\n");
152
-        } finally {
153
-            // destroy the data source which should close underlying connections
154
-            if (connectionSource != null) {
155
-                connectionSource.close();
156
-            }
157
-        }
158
-    }
159
-
160
-    /**
161
-     * Read and write some example data.
162
-     */
163
-    private void processData() throws Exception {
164
-        // create an instance of Account
165
-        String name = "Jim Coakley";
166
-        Account account = new Account(name);
167
-
168
-        // persist the account object to the database
169
-        accountDao.create(account);
170
-        int id = account.getId();
171
-        System.out.println(id);
172
-        // assign a password
173
-         account.setPassword("_secret");
174
-        // update the database after changing the object
175
-        accountDao.update(account);
176
-        // delete the account
177
-        //accountDao.deleteById(id);
178
-    }
179
-
180
-    /**
181
-     * Setup our  DAOs
182
-     */
183
-    private void setupDao(ConnectionSource connectionSource) throws Exception {
184
-
185
-        accountDao = DaoManager.createDao(connectionSource, Account.class);
186
-
187
-    }
188
-}
189
-```
190
-Some of the things in this class to take notice are the objects that the imported libraries offer us:
191
-
192
-* JdbcConnectionSource from the JDBC library
193
-* Dao and DaoManager from the OrmLite
194
-
195
-! Links to the above APIs are listed below
196
-
197
-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
198
-
199
-! Remember MySQL needs to be up and running
200
-
201
-# Part 3
202
-
203
-If you have made it this far and you're able to run the program properly, Congratulations!
204
-
205
-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
206
-
207
-### References
208
-* [OrmLite core API](http://ormlite.com/javadoc/ormlite-core/)
209
-* [OrmLite JDBC](http://ormlite.com/javadoc/ormlite-jdbc/)
210
-* [JdbcConnectionSource](http://ormlite.com/javadoc/ormlite-jdbc/com/j256/ormlite/jdbc/JdbcConnectionSource.html)
211
-* [What is CRUD](https://en.wikipedia.org/wiki/Create,_read,_update_and_delete)

+ 1
- 3
SIMPLE_ACCOUNT.iml 查看文件

1
 <?xml version="1.0" encoding="UTF-8"?>
1
 <?xml version="1.0" encoding="UTF-8"?>
2
-<module version="4">
3
-  <component name="ExternalSystem" externalSystem="Maven" />
4
-</module>
2
+<module type="JAVA_MODULE" version="4" />

+ 34
- 0
pom.xml 查看文件

1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<project xmlns="http://maven.apache.org/POM/4.0.0"
3
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5
+    <modelVersion>4.0.0</modelVersion>
6
+
7
+    <groupId>ORM-PROJECT</groupId>
8
+    <artifactId>SIMPLE_ACCOUNT</artifactId>
9
+    <version>1.0-SNAPSHOT</version>
10
+
11
+    <dependencies>
12
+
13
+        <dependency>
14
+            <groupId>com.j256.ormlite</groupId>
15
+            <artifactId>ormlite-core</artifactId>
16
+            <version>4.48</version>
17
+        </dependency>
18
+
19
+        <dependency>
20
+            <groupId>com.j256.ormlite</groupId>
21
+            <artifactId>ormlite-jdbc</artifactId>
22
+            <version>4.48</version>
23
+        </dependency>
24
+
25
+        <dependency>
26
+            <groupId>mysql</groupId>
27
+            <artifactId>mysql-connector-java</artifactId>
28
+            <version>5.1.6</version>
29
+        </dependency>
30
+
31
+    </dependencies>
32
+
33
+
34
+</project>

+ 71
- 0
src/main/java/Account.java 查看文件

1
+
2
+import com.j256.ormlite.field.DatabaseField;
3
+import com.j256.ormlite.table.DatabaseTable;
4
+
5
+
6
+public class Account {
7
+
8
+    public static final String NAME_FIELD_NAME = "name";
9
+    public static final String PASSWORD_FIELD_NAME = "password";
10
+
11
+
12
+    @DatabaseField(columnName = "id", generatedId = true)
13
+    private int id;
14
+
15
+    @DatabaseField(columnName = "name", canBeNull = false)
16
+    private String name;
17
+
18
+    @DatabaseField(columnName = "password")
19
+    private String password;
20
+
21
+    public Account(){
22
+
23
+    }
24
+
25
+    Account(int id) {
26
+        // all persisted classes must define a no-arg constructor with at least package visibility
27
+        this.id = id;
28
+    }
29
+
30
+    public Account(String name) {
31
+        this.name = name;
32
+    }
33
+
34
+    public Account(String name, String password) {
35
+        this.name = name;
36
+        this.password = password;
37
+    }
38
+
39
+    public int getId() {
40
+        return id;
41
+    }
42
+
43
+    public String getName() {
44
+        return name;
45
+    }
46
+
47
+    public void setName(String name) {
48
+        this.name = name;
49
+    }
50
+
51
+    public String getPassword() {
52
+        return password;
53
+    }
54
+
55
+    public void setPassword(String password) {
56
+        this.password = password;
57
+    }
58
+
59
+    @Override
60
+    public int hashCode() {
61
+        return name.hashCode();
62
+    }
63
+
64
+    @Override
65
+    public boolean equals(Object other) {
66
+        if (other == null || other.getClass() != getClass()) {
67
+            return false;
68
+        }
69
+        return name.equals(((Account) other).name);
70
+    }
71
+}

+ 66
- 0
src/main/java/AccountApp.java 查看文件

1
+import com.j256.ormlite.dao.Dao;
2
+import com.j256.ormlite.dao.DaoManager;
3
+import com.j256.ormlite.jdbc.JdbcConnectionSource;
4
+import com.j256.ormlite.support.ConnectionSource;
5
+
6
+public class AccountApp {
7
+
8
+    // we are using a MySQl database
9
+    private final static String DATABASE_URL = "jdbc:mysql://localhost:3306/orm_lab?useUnicode=true";
10
+
11
+    private Dao<Account, Integer> accountDao;
12
+
13
+
14
+    public static void main(String[] args) throws Exception {
15
+        // turn our static method into an instance of Main
16
+        new AccountApp().doMain(args);
17
+    }
18
+
19
+    private void doMain(String[] args) throws Exception {
20
+        ConnectionSource connectionSource = null;
21
+        try {
22
+            // create our data-source for the database
23
+            connectionSource = new JdbcConnectionSource(DATABASE_URL, "root", "");
24
+            // setup our  DAOs
25
+            setupDao(connectionSource);
26
+            // read, write and delete some data
27
+            processData();
28
+
29
+            System.out.println("\n\nIt seems to have worked\n\n");
30
+        } finally {
31
+            // destroy the data source which should close underlying connections
32
+            if (connectionSource != null) {
33
+                connectionSource.close();
34
+            }
35
+        }
36
+    }
37
+
38
+    /**
39
+     * Read and write some example data.
40
+     */
41
+    private void processData() throws Exception {
42
+        // create an instance of Account
43
+        String name = "Jim Coakley";
44
+        Account account = new Account(name);
45
+
46
+        // persist the account object to the database
47
+        accountDao.create(account);
48
+        int id = account.getId();
49
+        System.out.println(id);
50
+        // assign a password
51
+        account.setPassword("_secret");
52
+        // update the database after changing the object
53
+        accountDao.update(account);
54
+        // delete the account
55
+        //accountDao.deleteById(id);
56
+    }
57
+
58
+    /**
59
+     * Setup our  DAOs
60
+     */
61
+    private void setupDao(ConnectionSource connectionSource) throws Exception {
62
+
63
+        accountDao = DaoManager.createDao(connectionSource, Account.class);
64
+
65
+    }
66
+}