Skip to content

Commit eb72493

Browse files
committed
JDBC removed...
1 parent deb15e2 commit eb72493

4 files changed

Lines changed: 140 additions & 19 deletions

File tree

data-mapper/src/main/java/com/iluwatar/datamapper/App.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,15 @@ public static final void main(final String... args) {
8383
/* Add student in respectibe db */
8484
mapper.insert(student);
8585

86+
if (log.isDebugEnabled()) {
87+
log.debug("App.main(), student : " + student + ", is inserted");
88+
}
89+
8690
/* Find this student */
8791
final Optional<Student> studentToBeFound = mapper.find(student.getStudentId());
8892

8993
if (log.isDebugEnabled()) {
90-
log.debug("App.main(), db find returned : " + studentToBeFound);
94+
log.debug("App.main(), student : " + studentToBeFound + ", is searched");
9195
}
9296

9397
/* Update existing student object */
@@ -96,7 +100,15 @@ public static final void main(final String... args) {
96100
/* Update student in respectibe db */
97101
mapper.update(student);
98102

103+
if (log.isDebugEnabled()) {
104+
log.debug("App.main(), student : " + student + ", is updated");
105+
}
106+
99107
/* Delete student in db */
108+
109+
if (log.isDebugEnabled()) {
110+
log.debug("App.main(), student : " + student + ", is deleted");
111+
}
100112
mapper.delete(student);
101113
}
102114

data-mapper/src/main/java/com/iluwatar/datamapper/StudentMySQLDataMapper.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,20 @@
1818
*/
1919
package com.iluwatar.datamapper;
2020

21+
import java.util.ArrayList;
2122
import java.util.List;
2223
import java.util.Optional;
2324

2425
public final class StudentMySQLDataMapper implements StudentDataMapper {
2526

2627
/* Note: Normally this would be in the form of an actual database */
27-
private List<Student> students;
28+
private List<Student> students = new ArrayList<>();
2829

2930
@Override
3031
public final Optional<Student> find(final int studentId) {
3132

3233
/* Compare with existing students */
33-
for (final Student student : this.students) {
34+
for (final Student student : this.getStudents()) {
3435

3536
/* Check if student is found */
3637
if (student.getStudentId() == studentId) {
@@ -48,13 +49,13 @@ public final void update(final Student studentToBeUpdated) throws DataMapperExce
4849

4950

5051
/* Check with existing students */
51-
if (this.students.contains(studentToBeUpdated)) {
52+
if (this.getStudents().contains(studentToBeUpdated)) {
5253

5354
/* Get the index of student in list */
54-
final int index = this.students.indexOf(studentToBeUpdated);
55+
final int index = this.getStudents().indexOf(studentToBeUpdated);
5556

5657
/* Update the student in list */
57-
this.students.set(index, studentToBeUpdated);
58+
this.getStudents().set(index, studentToBeUpdated);
5859

5960
} else {
6061

@@ -67,10 +68,10 @@ public final void update(final Student studentToBeUpdated) throws DataMapperExce
6768
public final void insert(final Student studentToBeInserted) throws DataMapperException {
6869

6970
/* Check with existing students */
70-
if (!this.students.contains(studentToBeInserted)) {
71+
if (!this.getStudents().contains(studentToBeInserted)) {
7172

7273
/* Add student in list */
73-
this.students.add(studentToBeInserted);
74+
this.getStudents().add(studentToBeInserted);
7475

7576
} else {
7677

@@ -83,15 +84,19 @@ public final void insert(final Student studentToBeInserted) throws DataMapperExc
8384
public final void delete(final Student studentToBeDeleted) throws DataMapperException {
8485

8586
/* Check with existing students */
86-
if (this.students.contains(studentToBeDeleted)) {
87+
if (this.getStudents().contains(studentToBeDeleted)) {
8788

8889
/* Delete the student from list */
89-
this.students.remove(studentToBeDeleted);
90+
this.getStudents().remove(studentToBeDeleted);
9091

9192
} else {
9293

9394
/* Throw user error */
9495
throw new DataMapperException("Student [" + studentToBeDeleted.getName() + "] is not found");
9596
}
9697
}
98+
99+
public List<Student> getStudents() {
100+
return this.students;
101+
}
97102
}

data-mapper/src/main/java/com/iluwatar/datamapper/StudentOracleDataMapper.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,20 @@
1818
*/
1919
package com.iluwatar.datamapper;
2020

21+
import java.util.ArrayList;
2122
import java.util.List;
2223
import java.util.Optional;
2324

2425
public final class StudentOracleDataMapper implements StudentDataMapper {
2526

2627
/* Note: Normally this would be in the form of an actual database */
27-
private List<Student> students;
28+
private List<Student> students = new ArrayList<>();
2829

2930
@Override
3031
public final Optional<Student> find(final int studentId) {
3132

3233
/* Compare with existing students */
33-
for (final Student student : this.students) {
34+
for (final Student student : this.getStudents()) {
3435

3536
/* Check if student is found */
3637
if (student.getStudentId() == studentId) {
@@ -48,13 +49,13 @@ public final void update(final Student studentToBeUpdated) throws DataMapperExce
4849

4950

5051
/* Check with existing students */
51-
if (this.students.contains(studentToBeUpdated)) {
52+
if (this.getStudents().contains(studentToBeUpdated)) {
5253

5354
/* Get the index of student in list */
54-
final int index = this.students.indexOf(studentToBeUpdated);
55+
final int index = this.getStudents().indexOf(studentToBeUpdated);
5556

5657
/* Update the student in list */
57-
this.students.set(index, studentToBeUpdated);
58+
this.getStudents().set(index, studentToBeUpdated);
5859

5960
} else {
6061

@@ -67,10 +68,10 @@ public final void update(final Student studentToBeUpdated) throws DataMapperExce
6768
public final void insert(final Student studentToBeInserted) throws DataMapperException {
6869

6970
/* Check with existing students */
70-
if (!this.students.contains(studentToBeInserted)) {
71+
if (!this.getStudents().contains(studentToBeInserted)) {
7172

7273
/* Add student in list */
73-
this.students.add(studentToBeInserted);
74+
this.getStudents().add(studentToBeInserted);
7475

7576
} else {
7677

@@ -83,15 +84,19 @@ public final void insert(final Student studentToBeInserted) throws DataMapperExc
8384
public final void delete(final Student studentToBeDeleted) throws DataMapperException {
8485

8586
/* Check with existing students */
86-
if (this.students.contains(studentToBeDeleted)) {
87+
if (this.getStudents().contains(studentToBeDeleted)) {
8788

8889
/* Delete the student from list */
89-
this.students.remove(studentToBeDeleted);
90+
this.getStudents().remove(studentToBeDeleted);
9091

9192
} else {
9293

9394
/* Throw user error */
9495
throw new DataMapperException("Student [" + studentToBeDeleted.getName() + "] is not found");
9596
}
9697
}
98+
99+
public List<Student> getStudents() {
100+
return this.students;
101+
}
97102
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/**
2+
* The MIT License Copyright (c) 2014 Ilkka Seppälä
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
5+
* associated documentation files (the "Software"), to deal in the Software without restriction,
6+
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
7+
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
8+
* furnished to do so, subject to the following conditions:
9+
*
10+
* The above copyright notice and this permission notice shall be included in all copies or
11+
* substantial portions of the Software.
12+
*
13+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
14+
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
16+
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18+
*/
19+
package com.iluwatar.datamapper;
20+
21+
import java.util.Optional;
22+
import java.util.UUID;
23+
24+
import org.apache.log4j.Logger;
25+
26+
/**
27+
* The Data Mapper (DM) is a layer of software that separates the in-memory objects from the
28+
* database. Its responsibility is to transfer data between the two and also to isolate them from
29+
* each other. With Data Mapper the in-memory objects needn't know even that there's a database
30+
* present; they need no SQL interface code, and certainly no knowledge of the database schema. (The
31+
* database schema is always ignorant of the objects that use it.) Since it's a form of Mapper ,
32+
* Data Mapper itself is even unknown to the domain layer.
33+
* <p>
34+
* The below example demonstrates basic CRUD operations: select, add, update, and delete.
35+
*
36+
*/
37+
public final class App {
38+
39+
private static Logger log = Logger.getLogger(App.class);
40+
41+
42+
private static final String DB_TYPE_ORACLE = "Oracle";
43+
private static final String DB_TYPE_MYSQL = "MySQL";
44+
45+
46+
/**
47+
* Program entry point.
48+
*
49+
* @param args command line args.
50+
*/
51+
public static final void main(final String... args) {
52+
53+
if (log.isInfoEnabled() & args.length > 0) {
54+
log.debug("App.main(), db type: " + args[0]);
55+
}
56+
57+
StudentDataMapper mapper = null;
58+
59+
/* Check the desired db type from runtime arguments */
60+
if (DB_TYPE_ORACLE.equalsIgnoreCase(args[0])) {
61+
62+
/* Create new data mapper for mysql */
63+
mapper = new StudentMySQLDataMapper();
64+
65+
} else if (DB_TYPE_MYSQL.equalsIgnoreCase(args[0])) {
66+
67+
/* Create new data mapper for oracle */
68+
mapper = new StudentMySQLDataMapper();
69+
} else {
70+
71+
/* Don't couple any Data Mapper to java.sql.SQLException */
72+
throw new DataMapperException("Following data source(" + args[0] + ") is not supported");
73+
}
74+
75+
/* Create new student */
76+
Student student = new Student(UUID.randomUUID(), 1, "Adam", 'A');
77+
78+
/* Add student in respectibe db */
79+
mapper.insert(student);
80+
81+
/* Find this student */
82+
final Optional<Student> studentToBeFound = mapper.find(student.getGuId());
83+
84+
if (log.isDebugEnabled()) {
85+
log.debug("App.main(), db find returned : " + studentToBeFound);
86+
}
87+
88+
/* Update existing student object */
89+
student = new Student(student.getGuId(), 1, "AdamUpdated", 'A');
90+
91+
/* Update student in respectibe db */
92+
mapper.update(student);
93+
94+
/* Delete student in db */
95+
mapper.delete(student);
96+
}
97+
98+
private App() {}
99+
}

0 commit comments

Comments
 (0)