Skip to content

Commit deb15e2

Browse files
committed
JDBC removed...
1 parent 28a5a43 commit deb15e2

File tree

5 files changed

+433
-541
lines changed

5 files changed

+433
-541
lines changed
Lines changed: 104 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,104 @@
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-
*
28-
* The Data Mapper (DM) is a layer of software that separates the in-memory objects from the
29-
* database. Its responsibility is to transfer data between the two and also to isolate them from
30-
* each other. With Data Mapper the in-memory objects needn't know even that there's a database
31-
* present; they need no SQL interface code, and certainly no knowledge of the database schema. (The
32-
* database schema is always ignorant of the objects that use it.) Since it's a form of Mapper ,
33-
* Data Mapper itself is even unknown to the domain layer.
34-
* <p>
35-
* The below example demonstrates basic CRUD operations: select, add, update, and delete.
36-
*
37-
*/
38-
public final class App {
39-
40-
private static Logger log = Logger.getLogger(App.class);
41-
42-
43-
private static final String DB_TYPE_ORACLE = "Oracle";
44-
private static final String DB_TYPE_MYSQL = "MySQL";
45-
46-
47-
/**
48-
* Program entry point.
49-
*
50-
* @param args command line args.
51-
*/
52-
public static final void main(final String... args) {
53-
54-
if (log.isInfoEnabled() & args.length > 0) {
55-
log.debug("App.main(), db type: " + args[0]);
56-
}
57-
58-
StudentDataMapper mapper = null;
59-
60-
/* Check the desired db type from runtime arguments */
61-
if (args.length == 0) {
62-
63-
/* Create default data mapper for mysql */
64-
mapper = new StudentMySQLDataMapper();
65-
66-
} else if (args.length > 0 && DB_TYPE_ORACLE.equalsIgnoreCase(args[0])) {
67-
68-
/* Create new data mapper for mysql */
69-
mapper = new StudentMySQLDataMapper();
70-
71-
} else if (args.length > 0 && DB_TYPE_MYSQL.equalsIgnoreCase(args[0])) {
72-
73-
/* Create new data mapper for oracle */
74-
mapper = new StudentMySQLDataMapper();
75-
} else {
76-
77-
/* Don't couple any Data Mapper to java.sql.SQLException */
78-
throw new DataMapperException("Following data source(" + args[0] + ") is not supported");
79-
}
80-
81-
/* Create new student */
82-
Student student = new Student(UUID.randomUUID(), 1, "Adam", 'A');
83-
84-
/* Add student in respectibe db */
85-
mapper.insert(student);
86-
87-
/* Find this student */
88-
final Optional<Student> studentToBeFound = mapper.find(student.getGuId());
89-
90-
if (log.isDebugEnabled()) {
91-
log.debug("App.main(), db find returned : " + studentToBeFound);
92-
}
93-
94-
/* Update existing student object */
95-
student = new Student(student.getGuId(), 1, "AdamUpdated", 'A');
96-
97-
/* Update student in respectibe db */
98-
mapper.update(student);
99-
100-
/* Delete student in db */
101-
mapper.delete(student);
102-
}
103-
104-
private App() {}
105-
}
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+
23+
import org.apache.log4j.Logger;
24+
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: Create, Read, 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 (args.length == 0) {
61+
62+
/* Create default data mapper for mysql */
63+
mapper = new StudentMySQLDataMapper();
64+
65+
} else if (args.length > 0 && DB_TYPE_ORACLE.equalsIgnoreCase(args[0])) {
66+
67+
/* Create new data mapper for mysql */
68+
mapper = new StudentMySQLDataMapper();
69+
70+
} else if (args.length > 0 && DB_TYPE_MYSQL.equalsIgnoreCase(args[0])) {
71+
72+
/* Create new data mapper for oracle */
73+
mapper = new StudentMySQLDataMapper();
74+
} else {
75+
76+
/* Don't couple any Data Mapper to java.sql.SQLException */
77+
throw new DataMapperException("Following data mapping type(" + args[0] + ") is not supported");
78+
}
79+
80+
/* Create new student */
81+
Student student = new Student(1, "Adam", 'A');
82+
83+
/* Add student in respectibe db */
84+
mapper.insert(student);
85+
86+
/* Find this student */
87+
final Optional<Student> studentToBeFound = mapper.find(student.getStudentId());
88+
89+
if (log.isDebugEnabled()) {
90+
log.debug("App.main(), db find returned : " + studentToBeFound);
91+
}
92+
93+
/* Update existing student object */
94+
student = new Student(student.getStudentId(), "AdamUpdated", 'A');
95+
96+
/* Update student in respectibe db */
97+
mapper.update(student);
98+
99+
/* Delete student in db */
100+
mapper.delete(student);
101+
}
102+
103+
private App() {}
104+
}
Lines changed: 103 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,103 @@
1-
/**
2-
* The MIT License Copyright (c) 2016 Amit Dixit
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.io.Serializable;
22-
import java.util.UUID;
23-
24-
public final class Student implements Serializable {
25-
26-
private static final long serialVersionUID = 1L;
27-
28-
private UUID guid;
29-
private int studentID;
30-
private String name;
31-
private char grade;
32-
33-
public Student() {
34-
this.guid = UUID.randomUUID();
35-
}
36-
37-
public Student(final UUID guid, final int studentID, final String name, final char grade) {
38-
super();
39-
40-
this.guid = guid;
41-
this.studentID = studentID;
42-
this.name = name;
43-
this.grade = grade;
44-
}
45-
46-
47-
public Student(final UUID guid) {
48-
this.guid = guid;
49-
}
50-
51-
public final int getStudentId() {
52-
return studentID;
53-
}
54-
55-
public final void setStudentId(final int studentID) {
56-
this.studentID = studentID;
57-
}
58-
59-
public final String getName() {
60-
return name;
61-
}
62-
63-
public final void setName(final String name) {
64-
this.name = name;
65-
}
66-
67-
public final char getGrade() {
68-
return grade;
69-
}
70-
71-
public final void setGrade(final char grade) {
72-
this.grade = grade;
73-
}
74-
75-
public final UUID getGuId() {
76-
return guid;
77-
}
78-
79-
@Override
80-
public final String toString() {
81-
return "Student [guid=" + guid + ", studentID=" + studentID + ", name=" + name + ", grade=" + grade + "]";
82-
}
83-
}
1+
/**
2+
* The MIT License Copyright (c) 2016 Amit Dixit
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+
22+
import java.io.Serializable;
23+
24+
public final class Student implements Serializable {
25+
26+
private static final long serialVersionUID = 1L;
27+
28+
private int studentId;
29+
private String name;
30+
private char grade;
31+
32+
public Student() {
33+
34+
}
35+
36+
public Student(final int studentId, final String name, final char grade) {
37+
super();
38+
39+
this.studentId = studentId;
40+
this.name = name;
41+
this.grade = grade;
42+
}
43+
44+
public final int getStudentId() {
45+
return studentId;
46+
}
47+
48+
public final void setStudentId(final int studentId) {
49+
this.studentId = studentId;
50+
}
51+
52+
public final String getName() {
53+
return name;
54+
}
55+
56+
public final void setName(final String name) {
57+
this.name = name;
58+
}
59+
60+
public final char getGrade() {
61+
return grade;
62+
}
63+
64+
public final void setGrade(final char grade) {
65+
this.grade = grade;
66+
}
67+
68+
@Override
69+
public boolean equals(final Object inputObject) {
70+
71+
boolean isEqual = false;
72+
73+
/* Check if both objects are same */
74+
if (this == inputObject) {
75+
76+
isEqual = true;
77+
}
78+
/* Check if objects belong to same class */
79+
else if (inputObject != null && getClass() == inputObject.getClass()) {
80+
81+
final Student student = (Student) inputObject;
82+
83+
/* If student id matched */
84+
if (this.getStudentId() == student.getStudentId()) {
85+
86+
isEqual = true;
87+
}
88+
}
89+
return isEqual;
90+
}
91+
92+
@Override
93+
public int hashCode() {
94+
95+
/* Student id is assumed to be unique */
96+
return this.getStudentId();
97+
}
98+
99+
@Override
100+
public final String toString() {
101+
return "Student [studentId=" + studentId + ", name=" + name + ", grade=" + grade + "]";
102+
}
103+
}

0 commit comments

Comments
 (0)