Download 4_Lab_Database

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Open Database Connectivity wikipedia , lookup

Microsoft Jet Database Engine wikipedia , lookup

Database wikipedia , lookup

Relational model wikipedia , lookup

Clusterpoint wikipedia , lookup

Database model wikipedia , lookup

Transcript
Database Activity
CEMC 2015
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
Data Tools Project
• Use LUNA or MARS release of Eclipse
• Help-> Eclipse Marketplace
• Selection Install
Data Tools Project - Validation
• Help->Installation Details
Database Development Perspective
• New perspective is added
DB Connection Profile
Supplying Name of Database
Finalize Connection
Open Scrapbook - Ad-hoc queries
Creating a table for Students - DDL
INSERTing data
Data Access from Java
• Options include:
• Frameworks
• Java Persistence API (JPA)
• APIs
• JDBC
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
Example DAO Java interface
public interface StudentDAO {
boolean insertStudent(Student student);
ArrayList<Student> findStudentLastName(String lastName);
}
JDBC basics
• Types of objections
• Connection
• Statement
• ResultSet
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;
Inserting Data
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
Clean Up Resources
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;
Querying Data
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
}
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
Design Pattern - References
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