Presentation is loading. Please wait.

Presentation is loading. Please wait.

Database Activity CEMC 2015. Steps 1. Verify Data Tools Platform is installed in Eclipse 2. Configure a Connection to Derby Create necessary table for.

Similar presentations


Presentation on theme: "Database Activity CEMC 2015. Steps 1. Verify Data Tools Platform is installed in Eclipse 2. Configure a Connection to Derby Create necessary table for."— Presentation transcript:

1 Database Activity CEMC 2015

2 Steps 1. Verify Data Tools Platform is installed in Eclipse 2. Configure a Connection to Derby Create necessary table for STUDENTS in the database 3. Import student DB code from Github into Eclipse 4. Run the code and examine its structure and how it uses Java interfaces and multiple classes to insert students and search for students

3 Data Tools Project Use LUNA or MARS release of Eclipse Help-> Eclipse Marketplace Selection Install

4 Data Tools Project - Validation Help->Installation Details

5 Database Development Perspective New perspective is added

6 DB Connection Profile

7 Supplying Name of Database

8 Finalize Connection

9 Open Scrapbook - Ad-hoc queries

10 Creating a table for Students - DDL

11 INSERTing data

12 Data Access from Java Options include: Frameworks Java Persistence API (JPA) APIs JDBC

13 DAO - Data Access Object Design Pattern Design Pattern commonly used to persist and query data Purpose is to isolate the application programmer from the details of the query and persistence infrastructure. Type of Database and Query mechanism should not be exposed to developer

14 Example DAO Java interface public interface StudentDAO { boolean insertStudent(Student student); ArrayList findStudentLastName(String lastName); }

15 JDBC basics Types of objections Connection Statement ResultSet

16 Getting Connected from Java using JDBC public class StudentDB implements StudentDAO { final static String tableName="APP.STUDENTS"; final static String url="jdbc:derby:C:\\db\\names;create=true"; static Connection con = null; static Statement st = null; static ResultSet rs = null;

17 public boolean insertStudent(Student student) { boolean wasRowInserted = false; int num=0; // track if row was inserted or not try { con = DriverManager.getConnection(url); st = con.createStatement(); PreparedStatement pstmt = con.prepareStatement ("INSERT INTO " + tableName + " VALUES (?, ?, ?,?)"); pstmt.setInt(1, student.getID()); pstmt.setString(2, student.getFirstName()); pstmt.setString(3, student.getLastName()); pstmt.setDate(4, convertJavaDateToSqlDate(student.getDate().getTime())); num = pstmt.executeUpdate(); if (num == 1) { wasRowInserted = true; } // rows inserted Inserting Data

18 con.close(); // close connection and resources for database } catch (SQLException se){ System.out.println("Database Error has occurred."); System.out.println(se.getMessage()); } return wasRowInserted; Clean Up Resources

19 PreparedStatement pstmt = con.prepareStatement ("SELECT id, fname, lname,bdate from " + tableName + " where lname = ?"); pstmt.setString(1, lastName); rs = pstmt.executeQuery(); while (rs.next()) { id = rs.getInt("ID"); fname = rs.getString("FNAME"); lname = rs.getString("LNAME"); bdate = convertSQLDateToJavaDate(rs.getDate("BDATE")); Student s = new Student(id, fname, lname, bdate); students.add(s); // add students to ArrayList } Querying Data

20 Student Learning Objectives Design Patterns Introduction to Design Patterns Common techniques used to solve problems. Many use Object Oriented principles. DAO Low degree of COUPLING High degree of COHESION Many other design patterns are encountered in Software Engineering: GUI frameworks Web applications Mobile app development

21 Design Pattern - References

22 Student Learning Objectives Excellent example of Industry API (Application Programming Interface) Data Tools Database servers Data access languages - SQL (declarative) Big Data scenarios often use the R language or Python with library extensions


Download ppt "Database Activity CEMC 2015. Steps 1. Verify Data Tools Platform is installed in Eclipse 2. Configure a Connection to Derby Create necessary table for."

Similar presentations


Ads by Google