de.nim.wscr.dao.MemberDAO.java Source code

Java tutorial

Introduction

Here is the source code for de.nim.wscr.dao.MemberDAO.java

Source

package de.nim.wscr.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import de.nim.wscr.model.Member;
import de.nim.wscr.service.MemberService;

public class MemberDAO implements MemberService {

    private Connection connection;

    public MemberDAO(Connection connection) {
        this.connection = connection;
    }

    @Override
    public List<Member> getAllMembers() {
        // TODO Auto-generated method stub
        throw new UnsupportedOperationException("Not implemented yet.");
    }

    @Override
    public void addMember(Member member) {
        try {
            PreparedStatement statement = connection
                    .prepareStatement("INSERT INTO db1.member (FIRST_NAME, LAST_NAME, LICENSE) VALUES(?, ?, ?)");
            statement.setString(1, member.getFirstName());
            statement.setString(2, member.getLastName());
            statement.setBoolean(3, member.getDriverLicense());
            statement.execute();

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void updateMember(Member member) {
        try {
            PreparedStatement statement = connection
                    .prepareStatement("UPDATE db1.member SET FIRST_NAME = ?, LAST_NAME = ?, LICENSE = ?");
            statement.setString(1, member.getFirstName());
            statement.setString(2, member.getLastName());
            statement.setBoolean(3, member.getDriverLicense());
            statement.execute();

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void deleteMember(Member member) {
        try {
            PreparedStatement statement = connection.prepareStatement("DELETE FROM db1.member WHERE ID = ?");
            statement.setLong(1, member.getId());
            statement.execute();

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Member getMember(Member member) {

        Member toSearchForMember = new Member();

        if (null != member) {
            try {

                String sql = "SELECT * FROM db1. member WHERE ";
                int paramCount = 0;

                if (member.getId() != null) {
                    sql = sql + " ID = " + member.getId();
                    paramCount++;
                }

                if (StringUtils.isNotEmpty(member.getFirstName())) {
                    if (paramCount > 0) {
                        sql = sql + " AND";
                    }
                    sql = sql + " FIRST_NAME = " + member.getFirstName();
                    paramCount++;
                }

                if (StringUtils.isNotEmpty(member.getLastName())) {
                    if (paramCount > 0) {
                        sql = sql + " AND";
                    }
                    sql = sql + " LAST_NAME = " + member.getLastName();
                    paramCount++;
                }

                ResultSet rs = connection.createStatement().executeQuery(sql);

                if (rs.next()) {
                    toSearchForMember.setFirstName(rs.getString("FIRST_NAME"));
                    toSearchForMember.setLastName(rs.getString("LAST_NAME"));
                    toSearchForMember.setDriverLicense(rs.getBoolean("LICENSE"));
                    toSearchForMember.setId(rs.getLong("ID"));
                }

            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }
        return toSearchForMember;

    }

}