Lauren Green 6 лет назад
Родитель
Сommit
224f41ca15
3 измененных файлов: 145 добавлений и 49 удалений
  1. 33
    33
      .idea/workspace.xml
  2. 112
    16
      src/main/java/AccountApp.java
  3. Двоичные данные
      target/classes/AccountApp.class

+ 33
- 33
.idea/workspace.xml Просмотреть файл

39
       </usages-collector>
39
       </usages-collector>
40
       <usages-collector id="statistics.file.extensions.edit">
40
       <usages-collector id="statistics.file.extensions.edit">
41
         <counts>
41
         <counts>
42
-          <entry key="java" value="78" />
42
+          <entry key="java" value="1939" />
43
           <entry key="xml" value="8" />
43
           <entry key="xml" value="8" />
44
         </counts>
44
         </counts>
45
       </usages-collector>
45
       </usages-collector>
46
       <usages-collector id="statistics.file.types.edit">
46
       <usages-collector id="statistics.file.types.edit">
47
         <counts>
47
         <counts>
48
-          <entry key="JAVA" value="78" />
48
+          <entry key="JAVA" value="1939" />
49
           <entry key="XML" value="8" />
49
           <entry key="XML" value="8" />
50
         </counts>
50
         </counts>
51
       </usages-collector>
51
       </usages-collector>
65
       <file pinned="false" current-in-tab="false">
65
       <file pinned="false" current-in-tab="false">
66
         <entry file="file://$PROJECT_DIR$/src/main/java/Account.java">
66
         <entry file="file://$PROJECT_DIR$/src/main/java/Account.java">
67
           <provider selected="true" editor-type-id="text-editor">
67
           <provider selected="true" editor-type-id="text-editor">
68
-            <state relative-caret-position="330">
68
+            <state relative-caret-position="375">
69
               <caret line="25" selection-start-line="25" selection-end-line="25" />
69
               <caret line="25" selection-start-line="25" selection-end-line="25" />
70
               <folding>
70
               <folding>
71
                 <element signature="imports" expanded="true" />
71
                 <element signature="imports" expanded="true" />
91
       <file pinned="false" current-in-tab="true">
91
       <file pinned="false" current-in-tab="true">
92
         <entry file="file://$PROJECT_DIR$/src/main/java/AccountApp.java">
92
         <entry file="file://$PROJECT_DIR$/src/main/java/AccountApp.java">
93
           <provider selected="true" editor-type-id="text-editor">
93
           <provider selected="true" editor-type-id="text-editor">
94
-            <state relative-caret-position="315">
95
-              <caret line="21" column="89" selection-start-line="21" selection-start-column="89" selection-end-line="21" selection-end-column="89" />
94
+            <state relative-caret-position="336">
95
+              <caret line="91" column="40" lean-forward="true" selection-start-line="91" selection-start-column="40" selection-end-line="91" selection-end-column="40" />
96
               <folding>
96
               <folding>
97
                 <element signature="imports" expanded="true" />
97
                 <element signature="imports" expanded="true" />
98
               </folding>
98
               </folding>
144
     </option>
144
     </option>
145
   </component>
145
   </component>
146
   <component name="ProjectFrameBounds">
146
   <component name="ProjectFrameBounds">
147
-    <option name="x" value="36" />
147
+    <option name="x" value="88" />
148
     <option name="y" value="23" />
148
     <option name="y" value="23" />
149
-    <option name="width" value="1621" />
150
-    <option name="height" value="958" />
149
+    <option name="width" value="1452" />
150
+    <option name="height" value="947" />
151
   </component>
151
   </component>
152
   <component name="ProjectView">
152
   <component name="ProjectView">
153
     <navigator proportions="" version="1">
153
     <navigator proportions="" version="1">
183
   <component name="PropertiesComponent">
183
   <component name="PropertiesComponent">
184
     <property name="WebServerToolWindowFactoryState" value="false" />
184
     <property name="WebServerToolWindowFactoryState" value="false" />
185
     <property name="aspect.path.notification.shown" value="true" />
185
     <property name="aspect.path.notification.shown" value="true" />
186
-    <property name="com.android.tools.idea.instantapp.provision.ProvisionBeforeRunTaskProvider.myTimeStamp" value="1542292743298" />
186
+    <property name="com.android.tools.idea.instantapp.provision.ProvisionBeforeRunTaskProvider.myTimeStamp" value="1542312292430" />
187
     <property name="nodejs_interpreter_path.stuck_in_default_project" value="undefined stuck path" />
187
     <property name="nodejs_interpreter_path.stuck_in_default_project" value="undefined stuck path" />
188
     <property name="nodejs_npm_path_reset_for_default_project" value="true" />
188
     <property name="nodejs_npm_path_reset_for_default_project" value="true" />
189
     <property name="project.structure.last.edited" value="Modules" />
189
     <property name="project.structure.last.edited" value="Modules" />
228
       <updated>1542232515260</updated>
228
       <updated>1542232515260</updated>
229
       <workItem from="1542232517079" duration="2272000" />
229
       <workItem from="1542232517079" duration="2272000" />
230
       <workItem from="1542235072407" duration="35000" />
230
       <workItem from="1542235072407" duration="35000" />
231
-      <workItem from="1542242747938" duration="311000" />
231
+      <workItem from="1542242747938" duration="7409000" />
232
     </task>
232
     </task>
233
     <servers />
233
     <servers />
234
   </component>
234
   </component>
235
   <component name="TimeTrackingManager">
235
   <component name="TimeTrackingManager">
236
-    <option name="totallyTimeSpent" value="2618000" />
236
+    <option name="totallyTimeSpent" value="9716000" />
237
   </component>
237
   </component>
238
   <component name="ToolWindowManager">
238
   <component name="ToolWindowManager">
239
-    <frame x="36" y="23" width="1621" height="958" extended-state="0" />
239
+    <frame x="88" y="23" width="1452" height="947" extended-state="0" />
240
     <editor active="true" />
240
     <editor active="true" />
241
     <layout>
241
     <layout>
242
-      <window_info active="true" content_ui="combo" id="Project" order="0" visible="true" weight="0.25585812" />
242
+      <window_info content_ui="combo" id="Project" order="0" visible="true" weight="0.28652483" />
243
       <window_info id="Structure" order="1" side_tool="true" weight="0.25" />
243
       <window_info id="Structure" order="1" side_tool="true" weight="0.25" />
244
       <window_info id="Image Layers" order="2" />
244
       <window_info id="Image Layers" order="2" />
245
       <window_info id="Designer" order="3" />
245
       <window_info id="Designer" order="3" />
248
       <window_info id="Favorites" order="6" side_tool="true" />
248
       <window_info id="Favorites" order="6" side_tool="true" />
249
       <window_info anchor="bottom" id="Message" order="0" />
249
       <window_info anchor="bottom" id="Message" order="0" />
250
       <window_info anchor="bottom" id="Find" order="1" />
250
       <window_info anchor="bottom" id="Find" order="1" />
251
-      <window_info anchor="bottom" id="Run" order="2" visible="true" weight="0.3290993" />
251
+      <window_info active="true" anchor="bottom" id="Run" order="2" visible="true" weight="0.33333334" />
252
       <window_info anchor="bottom" id="Debug" order="3" weight="0.4" />
252
       <window_info anchor="bottom" id="Debug" order="3" weight="0.4" />
253
       <window_info anchor="bottom" id="Cvs" order="4" weight="0.25" />
253
       <window_info anchor="bottom" id="Cvs" order="4" weight="0.25" />
254
       <window_info anchor="bottom" id="Inspection" order="5" weight="0.4" />
254
       <window_info anchor="bottom" id="Inspection" order="5" weight="0.4" />
255
       <window_info anchor="bottom" id="TODO" order="6" />
255
       <window_info anchor="bottom" id="TODO" order="6" />
256
       <window_info anchor="bottom" id="Version Control" order="7" />
256
       <window_info anchor="bottom" id="Version Control" order="7" />
257
       <window_info anchor="bottom" id="Database Changes" order="8" show_stripe_button="false" />
257
       <window_info anchor="bottom" id="Database Changes" order="8" show_stripe_button="false" />
258
-      <window_info anchor="bottom" id="Terminal" order="9" />
258
+      <window_info anchor="bottom" id="Terminal" order="9" weight="0.3290993" />
259
       <window_info anchor="bottom" id="Event Log" order="10" side_tool="true" weight="0.3290993" />
259
       <window_info anchor="bottom" id="Event Log" order="10" side_tool="true" weight="0.3290993" />
260
-      <window_info anchor="bottom" id="Messages" order="11" weight="0.3290993" />
260
+      <window_info anchor="bottom" id="Messages" order="11" weight="0.32865497" />
261
       <window_info anchor="right" id="Commander" internal_type="SLIDING" order="0" type="SLIDING" weight="0.4" />
261
       <window_info anchor="right" id="Commander" internal_type="SLIDING" order="0" type="SLIDING" weight="0.4" />
262
       <window_info anchor="right" id="Ant Build" order="1" weight="0.25" />
262
       <window_info anchor="right" id="Ant Build" order="1" weight="0.25" />
263
       <window_info anchor="right" content_ui="combo" id="Hierarchy" order="2" weight="0.25" />
263
       <window_info anchor="right" content_ui="combo" id="Hierarchy" order="2" weight="0.25" />
276
     <option name="myLimit" value="2678400000" />
276
     <option name="myLimit" value="2678400000" />
277
   </component>
277
   </component>
278
   <component name="editorHistoryManager">
278
   <component name="editorHistoryManager">
279
+    <entry file="jar://$MAVEN_REPOSITORY$/mysql/mysql-connector-java/5.1.6/mysql-connector-java-5.1.6.jar!/com/mysql/jdbc/Util.class">
280
+      <provider selected="true" editor-type-id="text-editor">
281
+        <state relative-caret-position="3075">
282
+          <caret line="233" selection-start-line="233" selection-end-line="233" />
283
+        </state>
284
+      </provider>
285
+    </entry>
286
+    <entry file="file://$PROJECT_DIR$/pom.xml">
287
+      <provider selected="true" editor-type-id="text-editor">
288
+        <state relative-caret-position="390">
289
+          <caret line="26" column="16" lean-forward="true" selection-start-line="26" selection-start-column="16" selection-end-line="26" selection-end-column="16" />
290
+        </state>
291
+      </provider>
292
+    </entry>
279
     <entry file="file://$PROJECT_DIR$/src/main/java/Account.java">
293
     <entry file="file://$PROJECT_DIR$/src/main/java/Account.java">
280
       <provider selected="true" editor-type-id="text-editor">
294
       <provider selected="true" editor-type-id="text-editor">
281
-        <state relative-caret-position="330">
295
+        <state relative-caret-position="375">
282
           <caret line="25" selection-start-line="25" selection-end-line="25" />
296
           <caret line="25" selection-start-line="25" selection-end-line="25" />
283
           <folding>
297
           <folding>
284
             <element signature="imports" expanded="true" />
298
             <element signature="imports" expanded="true" />
300
         </state>
314
         </state>
301
       </provider>
315
       </provider>
302
     </entry>
316
     </entry>
303
-    <entry file="jar://$MAVEN_REPOSITORY$/mysql/mysql-connector-java/5.1.6/mysql-connector-java-5.1.6.jar!/com/mysql/jdbc/Util.class">
304
-      <provider selected="true" editor-type-id="text-editor">
305
-        <state relative-caret-position="3075">
306
-          <caret line="233" selection-start-line="233" selection-end-line="233" />
307
-        </state>
308
-      </provider>
309
-    </entry>
310
-    <entry file="file://$PROJECT_DIR$/pom.xml">
311
-      <provider selected="true" editor-type-id="text-editor">
312
-        <state relative-caret-position="390">
313
-          <caret line="26" column="16" lean-forward="true" selection-start-line="26" selection-start-column="16" selection-end-line="26" selection-end-column="16" />
314
-        </state>
315
-      </provider>
316
-    </entry>
317
     <entry file="file://$PROJECT_DIR$/src/main/java/AccountApp.java">
317
     <entry file="file://$PROJECT_DIR$/src/main/java/AccountApp.java">
318
       <provider selected="true" editor-type-id="text-editor">
318
       <provider selected="true" editor-type-id="text-editor">
319
-        <state relative-caret-position="315">
320
-          <caret line="21" column="89" selection-start-line="21" selection-start-column="89" selection-end-line="21" selection-end-column="89" />
319
+        <state relative-caret-position="336">
320
+          <caret line="91" column="40" lean-forward="true" selection-start-line="91" selection-start-column="40" selection-end-line="91" selection-end-column="40" />
321
           <folding>
321
           <folding>
322
             <element signature="imports" expanded="true" />
322
             <element signature="imports" expanded="true" />
323
           </folding>
323
           </folding>

+ 112
- 16
src/main/java/AccountApp.java Просмотреть файл

2
 import com.j256.ormlite.dao.DaoManager;
2
 import com.j256.ormlite.dao.DaoManager;
3
 import com.j256.ormlite.jdbc.JdbcConnectionSource;
3
 import com.j256.ormlite.jdbc.JdbcConnectionSource;
4
 import com.j256.ormlite.support.ConnectionSource;
4
 import com.j256.ormlite.support.ConnectionSource;
5
+import com.sun.javafx.binding.StringFormatter;
6
+
7
+import java.util.Scanner;
5
 
8
 
6
 public class AccountApp {
9
 public class AccountApp {
7
 
10
 
11
+    Scanner scanner = new Scanner(System.in);
8
     // we are using a MySQl database
12
     // we are using a MySQl database
9
     private final static String DATABASE_URL = "jdbc:mysql://localhost:3306/orm_lab?useUnicode=true";
13
     private final static String DATABASE_URL = "jdbc:mysql://localhost:3306/orm_lab?useUnicode=true";
10
 
14
 
25
             // read, write and delete some data
29
             // read, write and delete some data
26
             processData();
30
             processData();
27
 
31
 
28
-            System.out.println("\n\nIt seems to have worked\n\n");
29
         } finally {
32
         } finally {
30
-            // destroy the data source which should close underlying connections
33
+            // destroy the data source which should close underlying connectionsgit
31
             if (connectionSource != null) {
34
             if (connectionSource != null) {
32
                 connectionSource.close();
35
                 connectionSource.close();
33
             }
36
             }
38
      * Read and write some example data.
41
      * Read and write some example data.
39
      */
42
      */
40
     private void processData() throws Exception {
43
     private void processData() throws Exception {
41
-        // create an instance of Account
42
-        String name = "Jim Coakley";
43
-        Account account = new Account(name);
44
-
45
-        // persist the account object to the database
46
-        accountDao.create(account);
47
-        int id = account.getId();
48
-        System.out.println(id);
49
-        // assign a password
50
-        account.setPassword("_secret");
51
-        // update the database after changing the object
52
-        accountDao.update(account);
53
-        // delete the account
54
-        //accountDao.deleteById(id);
44
+        try {
45
+            boolean crud = true;
46
+            while (crud) {
47
+                System.out.println("What would you like to do?  Please select a number from below:\n1.Create\n2.Read\n3.Update\n4.Delete\n5.Quit");
48
+                int choice = scanner.nextInt();
49
+
50
+                switch (choice) {
51
+                    case 1:
52
+                        createMethod();
53
+                        break;
54
+                    case 2:
55
+                        readMethod();
56
+                        break;
57
+                    case 3:
58
+                        updateMethod();
59
+                        break;
60
+                    case 4:
61
+                        deleteMethod();
62
+                        break;
63
+                    case 5:
64
+                        crud = false;
65
+                        System.out.println("Thanks for using our database!  Have a fantastic day!");
66
+                        break;
67
+                }
68
+
69
+            }
70
+        } catch (Exception e) {
71
+            System.out.println("Error");
72
+
73
+        }
74
+    }
75
+
76
+    private void readMethod() {
77
+        try {
78
+            System.out.println("What is the id for the row you would you like to read?  Type 0 to read all.");
79
+            int row = scanner.nextInt();
80
+            if(row == 0) {
81
+                String result = String.format("| %6s | %20s | %20s |", "id", "name", "password") + "\n";
82
+                for(Account account : accountDao.queryForAll()) {
83
+                    result += String.format("| %6d | %20s | %20s |", account.getId(), account.getName(), account.getPassword()) + "\n";
84
+                }
85
+                System.out.println(result);
86
+            } else {
87
+                Account thisAccount = accountDao.queryForId(row);
88
+                System.out.println(String.format("| %6s | %20s | %20s |", "id", "name", "password"));
89
+                System.out.println(String.format("| %6d | %20s | %20s |", thisAccount.getId(), thisAccount.getName(), thisAccount.getPassword()));
90
+            }
91
+        } catch (Exception e) {
92
+            System.out.println("Error");
93
+        }
94
+    }
95
+
96
+    private void deleteMethod() {
97
+        try {
98
+            System.out.println("Please enter the id of the account you would like to delete");
99
+            int id = scanner.nextInt();
100
+            accountDao.deleteById(id);
101
+        } catch (Exception e) {
102
+            System.out.println("Error");
103
+        }
104
+    }
105
+
106
+    private void updateMethod() {
107
+        try {
108
+            System.out.println("What is the id of the user you would you like to update?");
109
+            int row = scanner.nextInt();
110
+            Account account = accountDao.queryForId(row);
111
+            System.out.println("What would you like to update?\n1.Name\n2.Password");
112
+            int choice3 = scanner.nextInt();
113
+            if (choice3 == 1) {
114
+                System.out.println("Enter new name");
115
+                Scanner scanner2 = new Scanner(System.in);
116
+                String newName = scanner2.nextLine();
117
+                account.setName(newName);
118
+                accountDao.update(account);
119
+            } else {
120
+                System.out.println("Enter new password");
121
+                Scanner scanner3 = new Scanner(System.in);
122
+                String newPassword = scanner3.nextLine();
123
+                account.setName(newPassword);
124
+                accountDao.update(account);
125
+            }
126
+        } catch (Exception e) {
127
+            System.out.println("Error");
128
+        }
129
+    }
130
+
131
+    private void createMethod() {
132
+
133
+        try {
134
+            System.out.print("Please enter the name on the account: ");
135
+            Scanner scanner4 = new Scanner(System.in);
136
+            String name = scanner4.nextLine();
137
+            Account account = new Account(name);
138
+            accountDao.create(account);
139
+            int id = account.getId();
140
+            System.out.println("User id is: " + id);
141
+            System.out.print("Please enter a password for the account: ");
142
+            Scanner scanner5 = new Scanner(System.in);
143
+            String password = scanner5.nextLine();
144
+            account.setPassword(password);
145
+            accountDao.update(account);
146
+        } catch (Exception e) {
147
+            System.out.println("Error");
148
+        }
149
+
150
+
55
     }
151
     }
56
 
152
 
57
     /**
153
     /**

Двоичные данные
target/classes/AccountApp.class Просмотреть файл