IST 311 Object-Oriented Design and Software Applications

Slides:



Advertisements
Similar presentations
Connecting to Databases. relational databases tables and relations accessed using SQL database -specific functionality –transaction processing commit.
Advertisements

Distributed Application Development B. Ramamurthy.
The Use of Microsoft based Technologies for the benefit of the Community Prof. Avi Mendelson – Microsoft & Technion - Taub 643.
DT228/3 Web Development Databases. Database Almost all web application on the net access a database e.g. shopping sites, message boards, search engines.
1 Foundations of Software Design Lecture 27: Java Database Programming Marti Hearst Fall 2002.
Chapter 7 Managing Data Sources. ASP.NET 2.0, Third Edition2.
Databases Dan Otero Alex Loddengaard
CIS 270—App Dev II Big Java Chapter 22 Relational Databases.
Advance Computer Programming Java Database Connectivity (JDBC) – In order to connect a Java application to a database, you need to use a JDBC driver. –
1 CSC 440 Database Management Systems JDBC This presentation uses slides and lecture notes available from
CSCI 6962: Server-side Design and Programming
Session 5: Working with MySQL iNET Academy Open Source Web Development.
Database Lecture # 1 By Ubaid Ullah.
Helena Pomezná, ciz034 St. skupina: L392 FEI, VŠB-TUO Ak. rok. 2002/2003 Download:
Denise Luther Senior IT Consultant Practical Technology Enablement with Enterprise Integrator.
CSCI 6962: Server-side Design and Programming JDBC Database Programming.
ASP.NET Programming with C# and SQL Server First Edition
Beginning Databases with JDBC Mike Bradley Adapted from and notes by Kevin Parker, Ph.D.
Java Database Connectivity (JDBC) Introduction to JDBC JDBC is a simple API for connecting from Java applications to multiple databases. Lets you smoothly.
PHP Programming with MySQL Slide 8-1 CHAPTER 8 Working with Databases and MySQL.
Web Server Administration Chapter 7 Installing and Testing a Programming Environment.
MySQL, Java, and JDBC CSE 3330 Southern Methodist University.
Designing and Developing WS B. Ramamurthy. Plans We will examine the resources available for development of JAX-WS based web services. We need an IDE,
1 Working with MS SQL Server Textbook Chapter 14.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Working with MSSQL Server Code:G0-C# Version: 1.0 Author: Pham Trung Hai CTD.
 What software components are required?  How do I install the Oracle JDBC driver?  How do I connect to the database?  What form is the data in and.
JDBC Java and Databases. RHS – SOC 2 JDBC JDBC – Java DataBase Connectivity An API (i.e. a set of classes and methods), for working with databases in.
Accessing Database using JDBC. JDBC Objectives Gain basic knowledge of Java JDBC Become familiar with the basics of interacting with a database using.
Object Oriented Software Development 10. Persistent Storage.
Java Database Connectivity (JDBC). Topics 1. The Vendor Variation Problem 2. SQL and Versions of JDBC 3. Creating an ODBC Data Source 4. Simple Database.
COMP 321 Week 4. Overview Normalization Entity-Relationship Diagrams SQL JDBC/JDBC Drivers hsqldb Lab 4-1 Introduction.
DataBases and SQL INFSY 547 Spring Course Wrap Up April 12: Complete Work on Servlets Review of Team Projects Close of Portfolio Work April 19:
Database Access Using JDBC BCIS 3680 Enterprise Programming.
JDBC CS 260 Database Systems. Overview  Introduction  JDBC driver types  Eclipse project setup  Programming with JDBC  Prepared statements  SQL.
Starting with Oracle SQL Plus. Today in the lab… Connect to SQL Plus – your schema. Set up two tables. Find the tables in the catalog. Insert four rows.
JDBC Java and Databases. SWC – JDBC JDBC – Java DataBase Connectivity An API (i.e. a set of classes and methods), for working with databases in.
1 Download current version of Tomcat from: g/tomcat/ g/tomcat/ Install it in: C:\Program Files\Apache.
SQL pepper. Why SQL File I/O is a great deal of code Optimal file organization and indexing is critical and a great deal of code and theory implementation.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe.
Introduction to Database Programming with Python Gary Stewart
Chapter 12 Introducing Databases. Objectives What a database is and which databases are typically used with ASP.NET pages What SQL is, how it looks, and.
Database: JDBC Overview
Fundamental of Databases
ASP.NET Programming with C# and SQL Server First Edition
INTRODUCTION TO DATABASES (MICROSOFT ACCESS)
Running a Forms Developer Application
JDBC Database Management Database connectivity
GO! with Microsoft Office 2016
Web Technologies IT230 Dr Mohamed Habib.
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
Database JDBC Overview CS Programming Languages for Web Applications
Applied CyberInfrastructure Concepts Fall 2017
GO! with Microsoft Access 2016
HW#4 Making Simple BBS Using JDBC
JDBC.
ISC440: Web Programming 2 Server-side Scripting PHP 3
Introduction to Server-Side Web Development using JSP and Databases
Chapter 8 Working with Databases and MySQL
Database Fundamentals
Teaching slides Chapter 8.
MSIS 655 Advanced Business Applications Programming
Bolat Azamat, Kim Dongmin
Computer Science Projects Database Theory / Prototypes
Contents Preface I Introduction Lesson Objectives I-2
JAVA DATABaSE CONNECTIVITY
Database Management Systems
CS3220 Web and Internet Programming SQL and MySQL
Understanding Core Database Concepts
Unit J: Creating a Database
Presentation transcript:

IST 311 Object-Oriented Design and Software Applications Persistent Data Persistent Data

Objectives Understand Relational Database (review) Understand SQL (review) Understand steps to add required JAR files to a Java database project Be able to connect to a database and fetch data from the tables Persistent Data

Motivation Software applications often need to store data permanently, even after the applications are terminated We can use a flat file to store data Everything is stored in a plain text file (easy to read and understand) Need programmers to specifically deal with data manipulation (a lot of programming work) The efficiency normally suffers Difficult to exchange data across different system Database Management Systems are created to address those issues Persistent Data

Relational Database Overview RDBMS (relational database management system) is currently the most dominant database management system NoSQL (not-only-sql) has become popular after the web 2.0 era There are standard languages for: Data Definition Languages (DDL) Data Manipulation Languages (DML) Data Control Languages (DCL) Transaction Control Languages (TCL) SQL (structural query language) is the most common database language in this context Examples of RDBMS are: MySQL, MSSQL, Oracle, PostgreSQL, SQLite, Apache Derby Persistent Data

Structure of RDBMS Database Tables Records Fields main framework a set of related data one instance of related data one piece of information of a record Excel workbook Excel spreadsheet One row in a spreadsheet One cell in a spreadsheet Primary key: is a value (one field or multiple fields) that uniquely identify a record A well designed table ought to have a primary key Foreign key: a field in a table that is being references by another compatible field in another table and the referencing field is a primary key. For example, in a course database, each student has one record with the student ID being the primary key. In course table, because students are taking multiple courses, there should be multiple courses for the same student. To find the student information from the course table, the student ID should be part of the course table, which makes the student ID in the course table a Foreign Key. Persistent Data

Entity-Relation Diagram (E-R Diagram) Persistent Data

SQL Overview SQL is a language that helps us manipulate databases For the purpose of IST 311, we will skip database design and creation part. Instead we concentrate on a subset of data manipulations (Read, Write, Update, and Delete or CRUD) SELECT: retrieve data from tables INSERT: add new records to tables UPDATE: change data in existing records DELETE: remove entire records Persistent Data

The SELECT Statement Let’s try some simple SELECT statements Open NetBeans and press Ctrl+5 to open the Service tab Expand Databases, then Java DB. Right-click sample and select “Connect…” Expand jdbc:derby://localhost:1527/sample [app on APP] to make sure a connection is established successfully Right-click jdbc… and select “Execute Command…” [We can now type SQL command in the new window] In the new blank window, type SELECT * FROM customer Press Ctrl+Shift+E [A list of customer should appear in the windows below the SQL command window] Persistent Data

The SELECT Statement (cont.) List all customer details for those who live in California SELECT * FROM customer WHERE “STATE” = ‘CA’ STATE is a reserve word (hence “STATE”) Although STATE works, but notice the highlight color is blue, which indicates it’s a SQL reserve word Use single quotation marks for strings in SQL List product IDs for products that were purchased by customers whose ID is 1 SELECT product_id FROM purchase_order WHERE customer_id = 1 Persistent Data

Joining Tables List product IDs for all products that were purchased by customers who live in California (5 rows) SELECT product_id FROM purchase_order JOIN customer ON (purchase_order.customer_id = customer.customer_id) WHERE customer."STATE" = 'CA' List product description for all products that were purchased by customers who live in California (5 rows) SELECT description FROM product JOIN purchase_order ON (product.product_id = purchase_order.product_id) JOIN customer ON (purchase_order.customer_id = customer.customer_id) WHERE customer."STATE" = 'CA' Persistent Data

Front-end and Back-end We have experience the power of SQL. To add the power of SQL to a Java program, we need to use JDBC (Java DataBase Connectivity) to communicate with a database. To use JDBC, we will: Create the database and enable it to accept connections. Set up the Java environment/project workspace to connect to a database. Create an instance of the database client driver in the Java code. Create a connection to the database in the Java code. (A Connection object.) Write SQL statement(s) to insert records into the database or retrieve records from the database via JBDC/Java. (A Statement object.) Execute the SQL statement and retrieve the results. (A ResultSet object.) Process the results, e.g. iterate through the ResultSet object. Close the ResultSet, Statement, and Connection objects when we're finished with them. Persistent Data

Derby Database We will first use a database called Derby Derby is an open-source relational database created by Apache Based on SQL standard Entirely implemented in Java and small foot-print Can be embedded in any Java program You can deploy Java solution without additional server installation Persistent Data

Getting Database Ready Open NetBeans and go to services tab (Ctrl+5) Look under the Databases group, expand Java DB. There should be a sample database in it. Expand jdbc:derby://localhost:… If it is not expandable, right-click the sample database and select Connect... Expand App in the jdbc group. There should be APP and tables inside APP. Right-click the jdbc string and select “Properties”, you should see Persistent Data

Creating a Project Create a Java Application project Create a folder named lib in the project directory Download derby.jar to the lib folder you just created Right-click the project name and select Properties Select Libraries from the Categories Select Run tab on the right and then select Add JAR/Folder… Find the derby.jar in the lib folder and select Relative Path (this should be the default value). Click Open Now, you’ve added derby.jar to your project Download derbyclient.jar and follow the same step to add it to the Compile tab Notice that if you are using other database management system such as SQLite or MySQL, the jar file(s) may be different. Persistent Data

Connecting to the Database Let try to “talk” to the database file In the java file, import java.sql.* In the main method, add the following lines try { } catch (SQLException e) { System.out.println("Cannot connect to database.\n" + e.getMessage()); } catch (ClassNotFoundException e) { System.out.println("Cannot create an instance of the database driver.\n" + e.getMessage()); } Persistent Data

Connecting to the Database Inside the try block, add the following // setting up database connection Class.forName("org.apache.derby.jdbc.ClientDriver); Connection dbConnection = DriverManager.getConnection("jdbc:derby://localhost: 1527//sample", "app", "app"); The first statement loads database driver. If you use multiple types of database, all drivers must be loaded. The second statement connects to the database server Our database runs locally right now. (hence localhost) Normally, you need to log in to access database. In our connection string, the 1st app is the user name and the 2nd app is the password Persistent Data

Testing a Simple Query Add the following code to after the connection to db // test database query Statement stmt = dbConnection.createStatement(); String testQuery = "SELECT * FROM CUSTOMER"; ResultSet results; results = stmt.executeQuery(testQuery); All selected records should now be in the results variable Next we will see if we did get the rows back Persistent Data

Processing Results Next enter the following lines to output the results // processing results while (results.next()) { System.out.println("Result: " + results.getString("NAME")); } Persistent Data

Closing Objects We should always close the objects when we finish // clean up results.close(); stmt.close(); dbConnection.close(); You can now run the application. If nothing is wrong, you should see a list names Persistent Data

SQLite Database Engine SQLite is a database engine that is: Serverless: it does not require a separate server to be installed no installation and configuration Self-contained: it has very few dependency and the entire database is in a single file Cross-platform: the database file can be copied to any system on any platform SQLite is very widely used and is part of many mobile devices, including Android and iOS. Current version of SQLite is version 3 Persistent Data

Connecting to a SQLite database Exercise Download a sqlite database from http://www.personal.psu.edu/kqy1/teaching/ist311/jdbc/sample.s qlite3 This SQLite database is a duplicate of the sample database in derby. Download the sqlite Jar file from http://www.personal.psu.edu/kqy1/teaching/ist311/jdbc/ sqlite- jdbc-3.14.2.jar Think about what we’ve done previously and now change the DriverManager and connection to this sqlite database. Persistent Data

Summary Briefly review RDBMS Briefly review the SELECT statement of SQL Learned how to connect to Derby database Practiced how to connect to SQLite database Persistent Data

References Derby SQLite JDBC Basics Persistent Data