130 likes | 279 Vues
Example – a list of contacts. See Contact.java at http://www.aber.ac.uk/~dcswww/Dept/Teaching/CourseNotes/current/CS12230/codeExamples/0-simple-examples/Contact.java What if we want the contacts data to persist?. Contact – OO model. UML class diagram Attributes name and phone
E N D
Example – a list of contacts • See Contact.java at • http://www.aber.ac.uk/~dcswww/Dept/Teaching/CourseNotes/current/CS12230/codeExamples/0-simple-examples/Contact.java • What if we want the contacts data to persist?
Contact – OO model • UML class diagram • Attributes name and phone • Operation toString()
Contact – ER model • Just the attributes • No operations • Still a UML class diagram
Key-value pairs • Name is the key • Phone is the value • Data model is a kind of persistent hash table
Storage choices • Text file • Spreadsheet • Browser • Database • Relational • Object oriented • NoSQL • Native XML • …
Browser • HTML5 localStorage • localStorage.setItem(key, value) • localStorage.getItem(key) • For offline use, use appcache • Benefits • Limitations • Example
SQLite • Create the SQLite database • http://www.sqlite.org/ • Use a shell tool – a convenient way to create the database in the first place • Or write a program
Applications Programming with SQL/CLI • Obtain a handle on the database • Send SQL queries to the database management system using query functions • Process the results of those queries • Results are tables and must be transformed into types that the application program can use
Java JDBCimage from http://www.cse.unsw.edu.au/~jas/talks/jdbc/notes.htmlaccessed 15/Oct/2012
Java JDBC • Driver manager provides implementations of Connection, Statement and ResultSet • Connection acts as database handle • Statement enables creation and execution of SQL queries • ResultSet maintains a cursor that enables access to current row of data returned by query
SQLite with Java JDBC • Suppose you have a database and a jdbc driver • In your Java program • Initialize the jdbcdriver Class.forName("org.sqlite.JDBC");causes org.sqlite.JDBC to be loaded at runtime • Get a connection this.connection= DriverManager.getConnection("jdbc:sqlite:"+dbname); • Use the connection to send SQL queries to the database • Contacts example
Java Persistence API • Alternative to call level interface • Work directly with the object oriented data model • We’ll revisit this later in the course
Summary • First think about • Where the data comes from • Why the data is stored • How it is to be used • Then think about • Data models • Data storage options • Applications programming