Pārlūkot izejas kodu

work in progress

Mexi 6 gadus atpakaļ
vecāks
revīzija
1c19f08fe0
1 mainītis faili ar 44 papildinājumiem un 14 dzēšanām
  1. 44
    14
      src/main/java/AccountApp.java

+ 44
- 14
src/main/java/AccountApp.java Parādīt failu

@@ -2,6 +2,9 @@ import com.j256.ormlite.dao.Dao;
2 2
 import com.j256.ormlite.dao.DaoManager;
3 3
 import com.j256.ormlite.jdbc.JdbcConnectionSource;
4 4
 import com.j256.ormlite.support.ConnectionSource;
5
+import java.util.*;
6
+
7
+import java.sql.SQLException;
5 8
 
6 9
 public class AccountApp {
7 10
 
@@ -23,7 +26,7 @@ public class AccountApp {
23 26
             // setup our  DAOs
24 27
             setupDao(connectionSource);
25 28
             // read, write and delete some data
26
-            processData();
29
+
27 30
 
28 31
             System.out.println("\n\nIt seems to have worked\n\n");
29 32
         } finally {
@@ -37,23 +40,50 @@ public class AccountApp {
37 40
     /**
38 41
      * Read and write some example data.
39 42
      */
40
-    private void processData() throws Exception {
41
-        // create an instance of Account
42
-        String name = "Jim Coakley";
43
+    // create, read, update, delete;
44
+
45
+
46
+
47
+
48
+
49
+    private void createAccount(String name, String password) throws SQLException {
43 50
         Account account = new Account(name);
51
+        account.setPassword(password);
52
+        accountDao.create(account); //Create;
53
+    }
54
+
55
+    private void readInfo() throws SQLException{
56
+
57
+       List<Account> accounts = accountDao.queryForAll();
58
+
59
+        for (Account acc : accounts){
60
+            System.out.print("ID " + acc.getId() + "Name " + acc.getName());
61
+        }
62
+     }
63
+
64
+
65
+     private void updateInfo(int id, String name, String password) throws Exception {
66
+        if (accountDao.idExists(id)){
67
+            Account account = accountDao.queryForId(id);
68
+            account.setName(name);
69
+            account.setPassword(password);
70
+            accountDao.update(account);
71
+
72
+           }
73
+
74
+        }
75
+
76
+    private void deleteInfo(int id, String name, String password) throws Exception {
77
+        if (accountDao.idExists(id)){
78
+            accountDao.deleteById(id);
79
+
80
+
81
+        }
82
+
44 83
 
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);
55 84
     }
56 85
 
86
+
57 87
     /**
58 88
      * Setup our  DAOs
59 89
      */