Aleena Rose-Mathew 6 лет назад
Родитель
Сommit
0048d72222
9 измененных файлов: 170 добавлений и 0 удалений
  1. Двоичные данные
      .DS_Store
  2. 2
    0
      Orm_lab.iml
  3. 38
    0
      pom.xml
  4. Двоичные данные
      src/.DS_Store
  5. Двоичные данные
      src/main/.DS_Store
  6. 64
    0
      src/main/java/Account.java
  7. 66
    0
      src/main/java/AccountApp.java
  8. Двоичные данные
      target/classes/Account.class
  9. Двоичные данные
      target/classes/AccountApp.class

Двоичные данные
.DS_Store Просмотреть файл


+ 2
- 0
Orm_lab.iml Просмотреть файл

@@ -0,0 +1,2 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<module type="JAVA_MODULE" version="4" />

+ 38
- 0
pom.xml Просмотреть файл

@@ -0,0 +1,38 @@
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_lab</groupId>
8
+    <artifactId>1</artifactId>
9
+    <version>1.0-SNAPSHOT</version>
10
+
11
+    <!-- https://mvnrepository.com/artifact/com.j256.ormlite/ormlite-core -->
12
+    <dependencies>
13
+
14
+
15
+    <!-- https://mvnrepository.com/artifact/com.j256.ormlite/ormlite-jdbc -->
16
+    <dependency>
17
+        <groupId>com.j256.ormlite</groupId>
18
+        <artifactId>ormlite-jdbc</artifactId>
19
+        <version>4.48</version>
20
+    </dependency>
21
+
22
+    <dependency>
23
+        <groupId>mysql</groupId>
24
+        <artifactId>mysql-connector-java</artifactId>
25
+        <version>5.1.6</version>
26
+    </dependency>
27
+
28
+        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
29
+        <dependency>
30
+            <groupId>mysql</groupId>
31
+            <artifactId>mysql-connector-java</artifactId>
32
+            <version>5.1.6</version>
33
+        </dependency>
34
+
35
+    </dependencies>
36
+
37
+
38
+</project>

Двоичные данные
src/.DS_Store Просмотреть файл


Двоичные данные
src/main/.DS_Store Просмотреть файл


+ 64
- 0
src/main/java/Account.java Просмотреть файл

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

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

@@ -0,0 +1,66 @@
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
+    public static void main(String[] args) throws Exception {
14
+        // turn our static method into an instance of Main
15
+        new AccountApp().doMain(args);
16
+    }
17
+
18
+    private void doMain(String[] args) throws Exception {
19
+        ConnectionSource connectionSource = null;
20
+        try {
21
+            // create our data-source for the database
22
+            connectionSource = new JdbcConnectionSource(DATABASE_URL, "root","");
23
+            // setup our  DAOs
24
+            setupDao(connectionSource);
25
+            // read, write and delete some data
26
+            processData();
27
+
28
+            System.out.println("\n\nIt seems to have worked\n\n");
29
+        } finally {
30
+            // destroy the data source which should close underlying connections
31
+            if (connectionSource != null) {
32
+                connectionSource.close();
33
+            }
34
+        }
35
+    }
36
+
37
+    /**
38
+     * Read and write some example data.
39
+     */
40
+    private void processData() throws Exception {
41
+        // create an instance of Account
42
+
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
+}

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


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