From 28a5a43a4715179e26fe26d861d1bd6564916467 Mon Sep 17 00:00:00 2001 From: Amit Dixit Date: Tue, 29 Mar 2016 16:36:00 +0530 Subject: [PATCH] basic implementation ++ basic implementation ++ --- data-mapper/index.md | 26 +++ data-mapper/pom.xml | 93 ++++++++++ .../java/com/iluwatar/datamapper/App.java | 105 ++++++++++++ .../datamapper/DataMapperException.java | 79 +++++++++ .../java/com/iluwatar/datamapper/Student.java | 83 +++++++++ .../datamapper/StudentDataMapper.java | 33 ++++ .../datamapper/StudentMySQLDataMapper.java | 160 ++++++++++++++++++ .../datamapper/StudentOracleDataMapper.java | 160 ++++++++++++++++++ data-mapper/src/main/resources/log4j.xml | 41 +++++ 9 files changed, 780 insertions(+) create mode 100644 data-mapper/index.md create mode 100644 data-mapper/pom.xml create mode 100644 data-mapper/src/main/java/com/iluwatar/datamapper/App.java create mode 100644 data-mapper/src/main/java/com/iluwatar/datamapper/DataMapperException.java create mode 100644 data-mapper/src/main/java/com/iluwatar/datamapper/Student.java create mode 100644 data-mapper/src/main/java/com/iluwatar/datamapper/StudentDataMapper.java create mode 100644 data-mapper/src/main/java/com/iluwatar/datamapper/StudentMySQLDataMapper.java create mode 100644 data-mapper/src/main/java/com/iluwatar/datamapper/StudentOracleDataMapper.java create mode 100644 data-mapper/src/main/resources/log4j.xml diff --git a/data-mapper/index.md b/data-mapper/index.md new file mode 100644 index 000000000..4e640d5d6 --- /dev/null +++ b/data-mapper/index.md @@ -0,0 +1,26 @@ +--- +layout: pattern +title: Data Mapper +folder: data-mapper +permalink: /patterns/dm/ +categories: Persistence Tier +tags: + - Java + - Difficulty-Beginner +--- + +## Intent +Object provides an abstract interface to some type of database or +other persistence mechanism. + +![alt text](./etc/dm.png "Data Mapper") + +## Applicability +Use the Data Mapper in any of the following situations + +* when you want to consolidate how the data layer is accessed +* when you want to avoid writing multiple data retrieval/persistence layers + +## Credits + +* [Data Mapper](http://richard.jp.leguen.ca/tutoring/soen343-f2010/tutorials/implementing-data-mapper/) diff --git a/data-mapper/pom.xml b/data-mapper/pom.xml new file mode 100644 index 000000000..866f5b76e --- /dev/null +++ b/data-mapper/pom.xml @@ -0,0 +1,93 @@ + + + + 4.0.0 + + com.iluwatar + java-design-patterns + 1.11.0-SNAPSHOT + + data-mapper + + + + junit + junit + test + + + log4j + log4j + + + + + + + + + + src/main/resources + + + src/main/resources + + log4j.xml + + .. + + + + + + + + org.apache.maven.plugins + maven-jar-plugin + 2.6 + + + log4j.xml + + + + true + + + + + + + + diff --git a/data-mapper/src/main/java/com/iluwatar/datamapper/App.java b/data-mapper/src/main/java/com/iluwatar/datamapper/App.java new file mode 100644 index 000000000..6345b5a4c --- /dev/null +++ b/data-mapper/src/main/java/com/iluwatar/datamapper/App.java @@ -0,0 +1,105 @@ +/** + * The MIT License Copyright (c) 2014 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and + * associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT + * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.iluwatar.datamapper; + +import java.util.Optional; +import java.util.UUID; + +import org.apache.log4j.Logger; + +/** + * + * The Data Mapper (DM) is a layer of software that separates the in-memory objects from the + * database. Its responsibility is to transfer data between the two and also to isolate them from + * each other. With Data Mapper the in-memory objects needn't know even that there's a database + * present; they need no SQL interface code, and certainly no knowledge of the database schema. (The + * database schema is always ignorant of the objects that use it.) Since it's a form of Mapper , + * Data Mapper itself is even unknown to the domain layer. + *

+ * The below example demonstrates basic CRUD operations: select, add, update, and delete. + * + */ +public final class App { + + private static Logger log = Logger.getLogger(App.class); + + + private static final String DB_TYPE_ORACLE = "Oracle"; + private static final String DB_TYPE_MYSQL = "MySQL"; + + + /** + * Program entry point. + * + * @param args command line args. + */ + public static final void main(final String... args) { + + if (log.isInfoEnabled() & args.length > 0) { + log.debug("App.main(), db type: " + args[0]); + } + + StudentDataMapper mapper = null; + + /* Check the desired db type from runtime arguments */ + if (args.length == 0) { + + /* Create default data mapper for mysql */ + mapper = new StudentMySQLDataMapper(); + + } else if (args.length > 0 && DB_TYPE_ORACLE.equalsIgnoreCase(args[0])) { + + /* Create new data mapper for mysql */ + mapper = new StudentMySQLDataMapper(); + + } else if (args.length > 0 && DB_TYPE_MYSQL.equalsIgnoreCase(args[0])) { + + /* Create new data mapper for oracle */ + mapper = new StudentMySQLDataMapper(); + } else { + + /* Don't couple any Data Mapper to java.sql.SQLException */ + throw new DataMapperException("Following data source(" + args[0] + ") is not supported"); + } + + /* Create new student */ + Student student = new Student(UUID.randomUUID(), 1, "Adam", 'A'); + + /* Add student in respectibe db */ + mapper.insert(student); + + /* Find this student */ + final Optional studentToBeFound = mapper.find(student.getGuId()); + + if (log.isDebugEnabled()) { + log.debug("App.main(), db find returned : " + studentToBeFound); + } + + /* Update existing student object */ + student = new Student(student.getGuId(), 1, "AdamUpdated", 'A'); + + /* Update student in respectibe db */ + mapper.update(student); + + /* Delete student in db */ + mapper.delete(student); + } + + private App() {} +} diff --git a/data-mapper/src/main/java/com/iluwatar/datamapper/DataMapperException.java b/data-mapper/src/main/java/com/iluwatar/datamapper/DataMapperException.java new file mode 100644 index 000000000..7cc66d5af --- /dev/null +++ b/data-mapper/src/main/java/com/iluwatar/datamapper/DataMapperException.java @@ -0,0 +1,79 @@ +/** + * The MIT License Copyright (c) 2016 Amit Dixit + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and + * associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT + * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.iluwatar.datamapper; + +/** + * + * @author amit.dixit + * + */ +public final class DataMapperException extends RuntimeException { + + private static final long serialVersionUID = 1L; + + /** + * Constructs a new runtime exception with {@code null} as its detail message. The cause is not + * initialized, and may subsequently be initialized by a call to {@link #initCause}. + */ + public DataMapperException() { + super(); + } + + + /** + * Constructs a new runtime exception with the specified detail message. The cause is not + * initialized, and may subsequently be initialized by a call to {@link #initCause}. + * + * @param message the detail message. The detail message is saved for later retrieval by the + * {@link #getMessage()} method. + */ + public DataMapperException(final String message) { + super(message); + } + + /** + * Constructs a new runtime exception with the specified detail message and cause. + *

+ * Note that the detail message associated with {@code cause} is not automatically + * incorporated in this runtime exception's detail message. + * + * @param message the detail message (which is saved for later retrieval by the + * {@link #getMessage()} method). + * @param cause the cause (which is saved for later retrieval by the {@link #getCause()} method). + * (A null value is permitted, and indicates that the cause is nonexistent or + * unknown.) + */ + public DataMapperException(final String message, final Throwable cause) { + super(message, cause); + } + + /** + * Constructs a new runtime exception with the specified cause and a detail message of + * (cause==null ? null : cause.toString()) (which typically contains the class and detail + * message of cause). This constructor is useful for runtime exceptions that are little + * more than wrappers for other throwables. + * + * @param cause the cause (which is saved for later retrieval by the {@link #getCause()} method). + * (A null value is permitted, and indicates that the cause is nonexistent or + * unknown.) + */ + public DataMapperException(final Throwable cause) { + super(cause); + } +} diff --git a/data-mapper/src/main/java/com/iluwatar/datamapper/Student.java b/data-mapper/src/main/java/com/iluwatar/datamapper/Student.java new file mode 100644 index 000000000..920d4d9ee --- /dev/null +++ b/data-mapper/src/main/java/com/iluwatar/datamapper/Student.java @@ -0,0 +1,83 @@ +/** + * The MIT License Copyright (c) 2016 Amit Dixit + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and + * associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT + * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.iluwatar.datamapper; + +import java.io.Serializable; +import java.util.UUID; + +public final class Student implements Serializable { + + private static final long serialVersionUID = 1L; + + private UUID guid; + private int studentID; + private String name; + private char grade; + + public Student() { + this.guid = UUID.randomUUID(); + } + + public Student(final UUID guid, final int studentID, final String name, final char grade) { + super(); + + this.guid = guid; + this.studentID = studentID; + this.name = name; + this.grade = grade; + } + + + public Student(final UUID guid) { + this.guid = guid; + } + + public final int getStudentId() { + return studentID; + } + + public final void setStudentId(final int studentID) { + this.studentID = studentID; + } + + public final String getName() { + return name; + } + + public final void setName(final String name) { + this.name = name; + } + + public final char getGrade() { + return grade; + } + + public final void setGrade(final char grade) { + this.grade = grade; + } + + public final UUID getGuId() { + return guid; + } + + @Override + public final String toString() { + return "Student [guid=" + guid + ", studentID=" + studentID + ", name=" + name + ", grade=" + grade + "]"; + } +} diff --git a/data-mapper/src/main/java/com/iluwatar/datamapper/StudentDataMapper.java b/data-mapper/src/main/java/com/iluwatar/datamapper/StudentDataMapper.java new file mode 100644 index 000000000..2493a9478 --- /dev/null +++ b/data-mapper/src/main/java/com/iluwatar/datamapper/StudentDataMapper.java @@ -0,0 +1,33 @@ +/** + * The MIT License Copyright (c) 2016 Amit Dixit + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and + * associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT + * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.iluwatar.datamapper; + +import java.util.Optional; +import java.util.UUID; + +public interface StudentDataMapper { + + public Optional find(final UUID uniqueID) throws DataMapperException; + + public void insert(final Student student) throws DataMapperException; + + public void update(final Student student) throws DataMapperException; + + public void delete(final Student student) throws DataMapperException; +} diff --git a/data-mapper/src/main/java/com/iluwatar/datamapper/StudentMySQLDataMapper.java b/data-mapper/src/main/java/com/iluwatar/datamapper/StudentMySQLDataMapper.java new file mode 100644 index 000000000..7520bc923 --- /dev/null +++ b/data-mapper/src/main/java/com/iluwatar/datamapper/StudentMySQLDataMapper.java @@ -0,0 +1,160 @@ +/** + * The MIT License Copyright (c) 2016 Amit Dixit + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and + * associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT + * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.iluwatar.datamapper; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Optional; +import java.util.UUID; + +public final class StudentMySQLDataMapper implements StudentDataMapper { + + @Override + public final Optional find(final UUID uniqueID) throws DataMapperException { + + try { + /* OracleDriver class cant be initilized directly */ + Class.forName("oracle.jdbc.driver.OracleDriver"); + + /* Create new connection */ + final Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/MySQL", "username", "password"); + + /* Create new Oracle compliant sql statement */ + final String statement = "SELECT `guid`, `grade`, `studentID`, `name` FROM `students` where `guid`=?"; + final PreparedStatement dbStatement = connection.prepareStatement(statement); + + /* Set unique id in sql statement */ + dbStatement.setString(1, uniqueID.toString()); + + /* Execute the sql query */ + final ResultSet rs = dbStatement.executeQuery(); + + while (rs.next()) { + + /* Create new student */ + final Student student = new Student(UUID.fromString(rs.getString("guid"))); + + /* Set all values from database in java object */ + student.setName(rs.getString("name")); + student.setGrade(rs.getString("grade").charAt(0)); + student.setStudentId(rs.getInt("studentID")); + + return Optional.of(student); + } + } catch (final SQLException | ClassNotFoundException e) { + + /* Don't couple any Data Mapper to java.sql.SQLException */ + throw new DataMapperException("Error occured reading Students from MySQL data source.", e); + } + + /* Return empty value */ + return Optional.empty(); + } + + @Override + public final void update(final Student student) throws DataMapperException { + try { + + /* OracleDriver class cant be initilized directly */ + Class.forName("oracle.jdbc.driver.OracleDriver"); + + /* Create new connection */ + final Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/MySQL", "username", "password"); + + /* Create new Oracle compliant sql statement */ + final String statement = "UPDATE `students` SET `grade`=?, `studentID`=?, `name`=? where `guid`=?"; + + final PreparedStatement dbStatement = connection.prepareStatement(statement); + + /* Set java object values in sql statement */ + dbStatement.setString(1, Character.toString(student.getGrade())); + dbStatement.setInt(2, student.getStudentId()); + dbStatement.setString(3, student.getName()); + dbStatement.setString(4, student.getGuId().toString()); + + /* Execute the sql query */ + dbStatement.executeUpdate(); + + } catch (final SQLException | ClassNotFoundException e) { + + /* Don't couple any Data Mapper to java.sql.SQLException */ + throw new DataMapperException("Error occured reading Students from MySQL data source.", e); + } + } + + @Override + public final void insert(final Student student) throws DataMapperException { + try { + + /* OracleDriver class cant be initilized directly */ + Class.forName("oracle.jdbc.driver.OracleDriver"); + + /* Create new connection */ + final Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/MySQL", "username", "password"); + + /* Create new Oracle compliant sql statement */ + final String statement = "INSERT INTO `students` (`grade`, `studentID`, `name`, `guid`) VALUES (?, ?, ?, ?)"; + final PreparedStatement dbStatement = connection.prepareStatement(statement); + + /* Set java object values in sql statement */ + dbStatement.setString(1, Character.toString(student.getGrade())); + dbStatement.setInt(2, student.getStudentId()); + dbStatement.setString(3, student.getName()); + dbStatement.setString(4, student.getGuId().toString()); + + /* Execute the sql query */ + dbStatement.executeUpdate(); + + } catch (final SQLException | ClassNotFoundException e) { + + /* Don't couple any Data Mapper to java.sql.SQLException */ + throw new DataMapperException("Error occured reading Students from MySQL data source.", e); + } + } + + @Override + public final void delete(final Student student) throws DataMapperException { + try { + + /* OracleDriver class cant be initilized directly */ + Class.forName("oracle.jdbc.driver.OracleDriver"); + + /* Create new connection */ + final Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/MySQL", "username", "password"); + + /* Create new Oracle compliant sql statement */ + final String statement = "DELETE FROM `students` where `guid`=?"; + final PreparedStatement dbStatement = connection.prepareStatement(statement); + + /* Set java object values in sql statement */ + dbStatement.setString(1, student.getGuId().toString()); + + /* Execute the sql query */ + dbStatement.executeUpdate(); + + } catch (final SQLException | ClassNotFoundException e) { + + /* Don't couple any Data Mapper to java.sql.SQLException */ + throw new DataMapperException("Error occured reading Students from MySQL data source.", e); + } + } +} diff --git a/data-mapper/src/main/java/com/iluwatar/datamapper/StudentOracleDataMapper.java b/data-mapper/src/main/java/com/iluwatar/datamapper/StudentOracleDataMapper.java new file mode 100644 index 000000000..0b6b7ebbf --- /dev/null +++ b/data-mapper/src/main/java/com/iluwatar/datamapper/StudentOracleDataMapper.java @@ -0,0 +1,160 @@ +/** + * The MIT License Copyright (c) 2016 Amit Dixit + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and + * associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT + * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.iluwatar.datamapper; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Optional; +import java.util.UUID; + +public final class StudentOracleDataMapper implements StudentDataMapper { + + @Override + public final Optional find(final UUID uniqueID) throws DataMapperException { + + try { + /* OracleDriver class cant be initilized directly */ + Class.forName("oracle.jdbc.driver.OracleDriver"); + + /* Create new connection */ + final Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:Oracle", "username", "password"); + + /* Create new Oracle compliant sql statement */ + final String statement = "SELECT `guid`, `grade`, `studentID`, `name` FROM `students` where `guid`=?"; + final PreparedStatement dbStatement = connection.prepareStatement(statement); + + /* Set unique id in sql statement */ + dbStatement.setString(1, uniqueID.toString()); + + /* Execute the sql query */ + final ResultSet rs = dbStatement.executeQuery(); + + while (rs.next()) { + + /* Create new student */ + final Student student = new Student(UUID.fromString(rs.getString("guid"))); + + /* Set all values from database in java object */ + student.setName(rs.getString("name")); + student.setGrade(rs.getString("grade").charAt(0)); + student.setStudentId(rs.getInt("studentID")); + + return Optional.of(student); + } + } catch (final SQLException | ClassNotFoundException e) { + + /* Don't couple any Data Mapper to java.sql.SQLException */ + throw new DataMapperException("Error occured reading Students from Oracle data source.", e); + } + + /* Return empty value */ + return Optional.empty(); + } + + @Override + public final void update(final Student student) throws DataMapperException { + try { + + /* OracleDriver class cant be initilized directly */ + Class.forName("oracle.jdbc.driver.OracleDriver"); + + /* Create new connection */ + final Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:Oracle", "username", "password"); + + /* Create new Oracle compliant sql statement */ + final String statement = "UPDATE `students` SET `grade`=?, `studentID`=?, `name`=? where `guid`=?"; + + final PreparedStatement dbStatement = connection.prepareStatement(statement); + + /* Set java object values in sql statement */ + dbStatement.setString(1, Character.toString(student.getGrade())); + dbStatement.setInt(2, student.getStudentId()); + dbStatement.setString(3, student.getName()); + dbStatement.setString(4, student.getGuId().toString()); + + /* Execute the sql query */ + dbStatement.executeUpdate(); + + } catch (final SQLException | ClassNotFoundException e) { + + /* Don't couple any Data Mapper to java.sql.SQLException */ + throw new DataMapperException("Error occured reading Students from Oracle data source.", e); + } + } + + @Override + public final void insert(final Student student) throws DataMapperException { + try { + + /* OracleDriver class cant be initilized directly */ + Class.forName("oracle.jdbc.driver.OracleDriver"); + + /* Create new connection */ + final Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:Oracle", "username", "password"); + + /* Create new Oracle compliant sql statement */ + final String statement = "INSERT INTO `students` (`grade`, `studentID`, `name`, `guid`) VALUES (?, ?, ?, ?)"; + final PreparedStatement dbStatement = connection.prepareStatement(statement); + + /* Set java object values in sql statement */ + dbStatement.setString(1, Character.toString(student.getGrade())); + dbStatement.setInt(2, student.getStudentId()); + dbStatement.setString(3, student.getName()); + dbStatement.setString(4, student.getGuId().toString()); + + /* Execute the sql query */ + dbStatement.executeUpdate(); + + } catch (final SQLException | ClassNotFoundException e) { + + /* Don't couple any Data Mapper to java.sql.SQLException */ + throw new DataMapperException("Error occured reading Students from Oracle data source.", e); + } + } + + @Override + public final void delete(final Student student) throws DataMapperException { + try { + + /* OracleDriver class cant be initilized directly */ + Class.forName("oracle.jdbc.driver.OracleDriver"); + + /* Create new connection */ + final Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:Oracle", "username", "password"); + + /* Create new Oracle compliant sql statement */ + final String statement = "DELETE FROM `students` where `guid`=?"; + final PreparedStatement dbStatement = connection.prepareStatement(statement); + + /* Set java object values in sql statement */ + dbStatement.setString(1, student.getGuId().toString()); + + /* Execute the sql query */ + dbStatement.executeUpdate(); + + } catch (final SQLException | ClassNotFoundException e) { + + /* Don't couple any Data Mapper to java.sql.SQLException */ + throw new DataMapperException("Error occured reading Students from Oracle data source.", e); + } + } +} diff --git a/data-mapper/src/main/resources/log4j.xml b/data-mapper/src/main/resources/log4j.xml new file mode 100644 index 000000000..b591c17e1 --- /dev/null +++ b/data-mapper/src/main/resources/log4j.xml @@ -0,0 +1,41 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file