Tiers (or layers) Separation of concerns

Similar documents
Accessing a database from Java. Using JDBC

Getting started with Winstone. Minimal servlet container

Choosing output format. Dynamically choosing format

CSC System Development with Java. Database Connection. Department of Statistics and Computer Science. Budditha Hettige

SQream Connector JDBC SQream Technologies Version 2.9.3

Accessing databases in Java using JDBC

Chair of Software Engineering. Java and C# in Depth. Prof. Dr. Bertrand Meyer. Exercise Session 9. Nadia Polikarpova

Assignment -3 Source Code. Student.java

DataBase Lab JAVA-DATABASE CONNECTION. Eng. Haneen El-masry

WEB SERVICES EXAMPLE 2

Oracle Exam 1z0-809 Java SE 8 Programmer II Version: 6.0 [ Total Questions: 128 ]

Pieter van den Hombergh. March 25, 2018

You write standard JDBC API application and plug in the appropriate JDBC driver for the database the you want to use. Java applet, app or servlets

ERwin and JDBC. Mar. 6, 2007 Myoung Ho Kim

Java Database Connectivity (JDBC) 25.1 What is JDBC?

JDBC Architecture. JDBC API: This provides the application-to- JDBC Manager connection.

Lab 6-1: MySQL Server

Assignment-1 Final Code. Student.java

INTRODUCTION TO JDBC - Revised spring

This lecture. Databases - JDBC I. Application Programs. Database Access End Users

SQL in a Server Environment

Writing your own Exceptions. How to extend Exception

DB I. 1 Dr. Ahmed ElShafee, Java course

COP4540 TUTORIAL PROFESSOR: DR SHU-CHING CHEN TA: H S IN-YU HA

Databases and SQL Lab EECS 448

INTRODUCTION TO JDBC - Revised Spring

Database Application Development

import org.simpleframework.xml.default; import org.simpleframework.xml.element; import org.simpleframework.xml.root;

PASS4TEST IT 인증시험덤프전문사이트

Workbook 5. Introduction. Using a database in Java. Important

JDBC, Transactions. Niklas Fors JDBC 1 / 38

Introduction to Databases

DATABASE DESIGN I - 1DL300

Working with Databases and Java

JAVA AND DATABASES. Summer 2018

Lecture 02, Fall 2018 Friday September 7

Database Programming Overview. COSC 304 Introduction to Database Systems. Database Programming. JDBC Interfaces. JDBC Overview

SQL: Programming Midterm in class next Thursday (October 5)

SQL: Programming. Announcements (September 25) Motivation. CPS 116 Introduction to Database Systems. Pros and cons of SQL.

InfiniteGraph Manual 1

Backend. (Very) Simple server examples

Instructor: Jinze Liu. Fall 2008

JDBC Java Database Connectivity is a Java feature that lets you connect

SQL DML and DB Applications, JDBC

CS506 Web Design & Development Final Term Solved MCQs with Reference

Java Classes - Using your classes. How the classes you write are being used

IT 313 Advanced Application Development

VanillaCore Walkthrough Part 1. Introduction to Database Systems DataLab CS, NTHU

Database Application Programs PL/SQL, Java and the Web

Database Application Development

Introduction to Java Development with IDS

Server-side Web Programming

Types of Databases. Types of Databases. Types of Databases. Databases and Web. Databases and Web. Relational databases may also have indexes

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

e-pg Pathshala Subject: Computer Science Paper: Web Technology Module: JDBC INTRODUCTION Module No: CS/WT/26 Quadrant 2 e-text

Database Access with JDBC. Dr. Jens Bennedsen, Aarhus University, School of Engineering Aarhus, Denmark

13 Creation and Manipulation of Tables and Databases

More on Objects in JAVA TM

What is it? CMSC 433 Programming Language Technologies and Paradigms Spring Approach 1. Disadvantage of Approach 1

Object-Oriented Design Lecture 3 CSU 370 Fall 2007 (Pucella) Friday, Sep 14, 2007

Object Oriented Programming

Unit 3 - Java Data Base Connectivity

SQL and Java. Database Systems Lecture 20 Natasha Alechina

MYBATIS - ANNOTATIONS

COE318 Lecture Notes Week 10 (Nov 7, 2011)

Java Database Connectivity


An IBM Rational Software TechNote

Highlights of Previous Lecture

Enterprise JavaBeans. Layer:08. Persistence

Exceptions. What exceptional things might our programs run in to?

Exceptions and Libraries

Web Applications and Database Connectivity using JDBC (Part II)

Lecture 9&10 JDBC. Mechanism. Some Warnings. Notes. Style. Introductory Databases SSC Introduction to DataBases 1.

Chapter 4 Defining Classes I

Distributed Systems Project 5 Assigned: Friday, April 6, 2012 Due: Friday, April 20, 11:59:59 PM

Ryerson University Department of Electrical & Computer Engineering COE618 Midterm Examination February 26, 2013

Database Application Development

Database Applications. SQL/PSM Embedded SQL JDBC

public Candy() { ingredients = new ArrayList<String>(); ingredients.add("sugar");

this keyword in java javatpoint

Abstract. 1. What is an ABSTRACT METHOD? 2. Why you would want to declare a method as abstract? 3. A non-abstract CLASS is called a concrete class

Outline. Lecture 10: Database Connectivity -JDBC. Java Persistence. Persistence via Database

Three-Tier Architecture

Tutorial: Using Java/JSP to Write a Web API

Static, Final & Memory Management

EJB - ACCESS DATABASE

Lab # 9. Java to Database Connection

Kyle Brown Knowledge Systems Corporation by Kyle Brown and Knowledge Systems Corporation

CSE 510 Web Data Engineering

BUSINESS INTELLIGENCE LABORATORY. Data Access: Relational Data Bases. Business Informatics Degree

Copyright Descriptor Systems, Course materials may not be reproduced in whole or in part without prior written consent of Joel Barnum

Java Database Connectivity

Highlights of Previous Lecture

Ū P O K O O T E I K A A M Ā U I U N I V E R S I T Y O F W E L L I N G T O N EXAMINATIONS 2015 TRIMESTER 1 SWEN221. Software Development

Java E-Commerce Martin Cooke,

Calling SQL from a host language (Java and Python) Kathleen Durant CS 3200

CSE 530A. DAOs and MVC. Washington University Fall 2012

Introduction to SQL & Database Application Development Using Java

Transcription:

Tiers (or layers) Separation of concerns

Hiding the type of storage from the client class Let s say we have a program that needs to fetch objects from a storage. Should the program have to be concerned with the specifics of the storage?

Hiding the type of storage from the client class No!? What is a storage to the client? It s some means of storing something - and means of retrieving or otherwise interacting with the storage thingy.

Abstraction OK, so something is stored somehow and we need to access that something. Let s use abstraction - so that we can talk about the storage in general terms.

A storage abstraction Let s say we have stored data on students somewhere, and our program needs access to the stored students. We d like to be able to fetch all students, for instance. Therefore we d like to have a programming interface to some kind of storage which holds the data on our students. In Java, this sounds a lot like an interface!

The StudentStorage interface The program/client class needs to get a list of all students. Sometimes it needs to get one student, providing some kind of key. Suggestion: public List<Student> getallstudents(); public Student getstudentbyid(int id); Now, we (the client code) can get all students from storage, or get one student if we provide a student id...

Simple StudentStorage interface package so.and.so; import java.util.list; import so.and.so.domain.student; public interface StudentStorage { public List<Student> getallstudents(); public Student getstudentbyid(int id); All we needed was a way to get all students as a list, and a way to get one student by providing a student id. So that s pretty much it! (Not including Exceptions)

But, that s just an interface... Who s implementing? Someone can create a concrete class which implements the interface. Let s pretend someone s done that, using JDBC to get the actual data to create Student objects. They would use that to implement the only two methods specified by the interface; getallstudents() and getstudentbyid(int id). How do we know what concrete classes exists? We can have a StudentStorageFactory that will give us some concrete class implementing the interface StudentStorage. We still wouldn t have to care about what concrete class does that job for us, and how that job is done!

StudentStorageFactory Something like this: public class StudentStorageFactory { public static StudentStorage getstorage() { //find out what storage to use and return for instance: return new StudentStorageDB(); // StudentStorageDB is a class which implements // StudentStorage and uses a database to accomplish that

What s the point? Well, the point is that now we have a means to get a list of students without having to know or care about how those students are stored. We have a programming interface (using a Java interface) that we can use! In our client code (like our program s main method for instance) which can simply do this: StudentStorage storage = StudentStorageFactory.getStorage();

Classes/interfaces needed so far... Our Main class that runs the program A StudentStorage interface giving us the methods we need A StudentStorageFactory class giving us some implementation of the interface A StudentStorageDB class, implementing the StudentStorage interface using a database.

Example code in our client class Our main method could now look like this (not showing exceptions): public static void main(string[] args) { StudentStorage storage = StudentStorageFactory.getStorage(); List<Student> students = storage.getallstudents(); for(student student : students) { // Do something with each Student... The point being? That we can access all students from the storage, without having to care about how those students are stored. Nothing in our code reveals how students are stored in the storage. Our code can focus on getting students and using all the Student objects...

So how would we implement the interface? The implementation StudentStorageDB obviously fetches students from a database. So we d need to use JDBC. To keep things simple for the StudentStorageDB class, we could create a DBUtils helper class that handles the database setup. DBUtils can set up the connection and so forth and provide a method for performing a query based on a String with SQL and returning a ResultSet with the result.

The DBUtil class This class should load the JDBC driver and create a Connection to the database. It should expose/offer a method that given a String with SQL returns a ResultSet. Signature could be: public ResultSet query(string sql) { // code to create a statement and execute a query

What s the gain for the StudentStorageDB class? The StudentStorageDB class now only has to handle ResultSet objects (and doesn t have to care about setting up the connection, loading the driver etc etc). Code could be something like this (leaving out try-catch and other details ): public List<Student> getallstudents() { ResultSet rs = db.query(select_all_query); List<Student> students = new ArrayList<Student>(); while(rs.next()) { students.add( new Student(rs.getString("id"), rs.getstring("name")) ); return students;

What s the gain for the StudentStorageDB class? The code could for the getstudentbyid() could be something like this (leaving out try-catch and other details): public Student getstudentbyid(int id) { String query = SELECT_STUDENT_QUERY + id; ResultSet rs = db.query(query); rs.next(); Student student = new Student(rs.getString("id"), rs.getstring("name")) ); return student; *) what to do if rs.next() returns false?

What did StudentStorageDB do? StudentStorageDB offered a method to get a List<Student> without exposing where it got it from. It implements the StudentStorage interface so that clients can use it as any StudentStorage - they all have in common that they offer one method for fetching all students from some storage. It also offered a method to get one Student from a provided student id. The StudentStorage accomplished this using the DBUtil class which provided access to the database where all student data was stored. The DBUtil could be implemented as a Singleton.

Overview Main - create a StudentStorage from StudentStorageFactory StudentStorageFactory selects a suitable implementation and returns an instance. Main uses the storage to fetch all students and does something with them. The implementation StudentStorageDB uses DBUtils to get a ResultSet from a query string, and creates a list of all students and returns the list. DBUtils does the dirty work of setting up the DB-connection and statements needed to e.g. fetch students in a ResultSet which is returned.

Main package org.henrikard.student.main; import org.henrikard.student.storage.studentstoragefactory; import org.henrikard.student.storage.studentstorage; import org.henrikard.student.storage.storageexception; import org.henrikard.student.domain.student; import java.util.list; public class Main { public static void main(string[] args) { try{ StudentStorage storage = StudentStorageFactory.getStorage(); List<Student> students = storage.getallstudents(); for(student student : students) { System.out.println(student); catch (StorageException se) { System.err.println("Error accessing the storage: " + se.getmessage());

StudentStorage package org.henrikard.student.storage; import org.henrikard.student.domain.student; import java.util.list; public interface StudentStorage { public List<Student> getallstudents() throws StorageException; public Student getstudentbyid(int id) throws StorageException;

StudentStorageFactory package org.henrikard.student.storage; public class StudentStorageFactory { public static StudentStorage getstorage() { // We only have one type of storage just now... return new StudentStorageDB();

Student package org.henrikard.student.domain; public class Student { private String name; private int id; public Student(int id, String name) { this.name = name; this.id = id; public String name(){ return this.name; public int id(){ return id; @Override public String tostring() { return new StringBuilder().append(name).append(" (").append(id).append(")").tostring();

StorageException package org.henrikard.student.storage; public class StorageException extends Exception { public StorageException(String msg) { super(msg);

DBUtil pt1 package org.henrikard.student.db; import java.sql.*; public class DBUtil { private static Connection con; private static final String DB_CON_STR = "jdbc:sqlite:org/henrikard/student/resources/students.db"; static { try { Class.forName("org.sqlite.JDBC"); con = getconnection(); catch (ClassNotFoundException cnfe) { System.err.println("Could not load database driver: " + cnfe.getmessage());

DBUtil pt2 private static Connection getconnection() { if (con == null) { try { con = DriverManager.getConnection(DB_CON_STR); catch(sqlexception sqle) { System.err.println("Error creating a connection to the database: " + sqle.getmessage()); return con; private Statement statement() throws SQLException { return con.createstatement();

DBUtil pt3 private static DBUtil instance; private DBUtil() { public static DBUtil getinstance() { if (instance == null) { instance = new DBUtil(); return instance; public ResultSet query(string sql) throws SQLException { Statement stm = statement(); ResultSet rs = stm.executequery(sql); return rs;

What did we gain from all this? We separated concerns so that: Main only fetches students and operates on them StudentStorage only specifies what a storage is and can do StudentStorageDB implements StudentStorage using a ResultSet DBUtils handles all database stuff and only exposes ResultSet

Compile and run # change directory to the one above org javac */*/*/*/*.java && java -cp.:org/henrikard/student/resources/sqlite-jdbc-3.8.11.2.jar org.henrikard.student.main.main (on one single row)

Directory and file layout. `-- org `-- henrikard `-- student -- db `-- DBUtil.java -- domain `-- Student.java -- main `-- Main.java -- resources -- sqlite-jdbc-3.8.11.2.jar `-- students.db `-- storage -- StorageException.java -- StudentStorageDB.java -- StudentStorageFactory.java `-- StudentStorage.java 8 directories, 9 files