COMP 321 Week 4. Overview Normalization Entity-Relationship Diagrams SQL JDBC/JDBC Drivers hsqldb Lab 4-1 Introduction.

Slides:



Advertisements
Similar presentations
Introduction to JDBC Standard framework for dealing with tabular and generally, relational data SQL (Structured Query Language) is standardized language.
Advertisements

Database programming in Java An introduction to Java Database Connectivity (JDBC)
Distributed Application Development B. Ramamurthy.
15-Jun-15 JDBC. JDBC is a Sun trademark It is often taken to stand for Java Database Connectivity Java is very standardized, but there are many versions.
1 C. Shahabi Application Programming for Relational Databases Cyrus Shahabi Computer Science Department University of Southern California
UFCE4Y UFCE4Y-20-3 Components and Services Julia Dawson.
JDBC CS 122. JDBC zJava Database Connectivity zDatabase Access Interface Õprovides access to a relational database (by allowing SQL statements to be sent.
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. –
Getting connected.  Java application calls the JDBC library.  JDBC loads a driver which talks to the database.  We can change database engines without.
CSCI 6962: Server-side Design and Programming
Java Database Connectivity (JDBC) Francisco Pajaro Saul Acosta Nahum Quezada Manuel Rubio.
Java Database Connectivity (JDBC). Introduction Database –Collection of data DBMS –Database management system –Storing and organizing data SQL –Relational.
JDBC. What is JDBC JDBC is an acronym for –Java Data Base Connectivity. It allows java/jsp program to connect to any database.
© Wang Bin 2004 JDBC ----Java Database Connectivity.
CSE470 Software Engineering Fall Database Access through Java.
Beginning Databases with JDBC Mike Bradley Adapted from and notes by Kevin Parker, Ph.D.
Java Utility Classes CS 21b. Some Java Utility Classes Vector Hashtable StringTokenizer * import java.util.*;
MySQL, Java, and JDBC CSE 3330 Southern Methodist University.
Dr R R DOCSIT, Dr BAMU. Basic Java : Introduction to JDBC 2 Objectives of This Session State what is Java Database Connectivity State different.
JDBC Tutorial MIE456 - Information Systems Infrastructure II Vinod Muthusamy November 4, 2004.
JDBC (Java Database Connectivity) SNU OOPSLA Lab. October 2005.
Introduction to JDBC Michelle Lee, Ye Wu & Jeff Offutt SWE 432 Design and Implementation of Software for the Web.
JDBC. JDBC stands for Java Data Base Connectivity. JDBC is different from ODBC in that – JDBC is written in Java (hence is platform independent, object.
JDBC – Java DataBase Connectivity. JDBC API Overview JDBC is Java API that allows the Java programmers to access database management system from Java.
JDBC. Preliminaries Database Database Collection of data Collection of data DBMS DBMS Database management system Database management system Stores and.
JDBC Enterprise Systems Programming. JDBC  Java Database Connectivity  Database Access Interface provides access to a relational database (by allowing.
Chapter 8 Databases.
Accessing Database using JDBC. JDBC Objectives Gain basic knowledge of Java JDBC Become familiar with the basics of interacting with a database using.
COMP201 Java Programming Topic 15: Database Connectivity JDBC Reading: Chapter 4, Volume 2.
WEB/DB1 DATABASE PROGRAMMING 3JDBC by the ASU Scholars.
Java Database Connectivity. Java and the database Database is used to store data. It is also known as persistent storage as the data is stored and can.
JDBC Database Programming in Java Prepared by., Mrs.S.Amudha AP/SWE.
JDBC. Java.sql.package The java.sql package contains various interfaces and classes used by the JDBC API. This collection of interfaces and classes enable.
JDBC CS 124. JDBC Java Database Connectivity Database Access Interface provides access to a relational database (by allowing SQL statements to be sent.
Java and Databases. JDBC Architecture Java Application JDBC API Data Base Drivers AccessSQL Server DB2InformixMySQLSybase.
UNIT III - JDBC JDBC Overview – JDBC implementation – Connection class – Statements - Catching Database Results, handling database Queries. Networking–
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.
CSI 3125, Preliminaries, page 1 JDBC. CSI 3125, Preliminaries, page 2 JDBC JDBC stands for Java Database Connectivity, which is a standard Java API (application.
Access Databases from Java Programs via JDBC Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale
Basics of JDBC Session 14.
JDBC (Java Database Connectivity)
JDBC - Java Database Connectivity. JDBC provides Java applications with access to most database systems via SQL The architecture and API closely resemble.
Advanced Java Session 5 New York University School of Continuing and Professional Studies.
Ch. NoNameMarks 01AWT24 02Networking18 03JDBC20 04Swing18 05Servlet20 Advance Java Programming.
Database Programming With Java & JDBC Reading: DD Ch. 18, pp al/jdbc/index.html, or anything covering JDBC.
Umair Javed©2005 Enterprise Application Development Java Database Connectivity (JDBC) JDBC1.
Introduction to JDBC Instructor: Mohamed Eltabakh 1.
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.
JDBC. SQL(Structured Query Language) is divided into 1.DDL(Data Definition Language) create db, create table, alter table, drop db or table 2.DML(Data.
Intro to JDBC Joseph Sant Applied Computing and Engineering Sciences Sheridan ITAL.
6-1 JAVA DATABASE CONNECTOR Colorado Technical University IT420 Tim Peterson.
JSP/Database Connectivity Instructor: Dr. M. Anwar Hossain.
Java and database. 3 Relational Databases A relational Database consists of a set of simple rectangular tables or relations The column headings are.
JDBC. Database is used to store data permanently. These days almost all Applications needs database to store its data persistently. Below are the most.
DEPTT. OF COMP. SC & APPLICATIONS
Lec - 14.
JDBC 15-Apr-18.
JDBC Database Management Database connectivity
Advanced Web Automation Using Selenium
JDBC 21-Aug-18.
HW#4 Making Simple BBS Using JDBC
Client Access, Queries, Stored Procedures, JDBC
Introduction to Server-Side Web Development using JSP and Databases
JDBC 15-Nov-18.
Interacting with Database
Using a Database with JDBC
Java API for Database Connectivity
Presentation transcript:

COMP 321 Week 4

Overview Normalization Entity-Relationship Diagrams SQL JDBC/JDBC Drivers hsqldb Lab 4-1 Introduction

Relational Databases Store data in tables made up of rows and columns Columns have data types Rows represent entries

Relational Databases Product_CodeDescriptionPrice Toaster Hair Dryer Car Vacuum CREATE TABLE Product ( Product_Code CHAR(11), Description CHAR(40), Price DECIMAL(10, 2) )

Normalization What’s wrong with this table definition? CREATE TABLE Order ( Product_Code CHAR(11), Quantity INTEGER, Description CHAR(40), Price DECIMAL(10, 2) )

Normalization What’s wrong with this table definition? CREATE TABLE Order ( Order_Id INTEGER, Product_Code CHAR(11), Quantity INTEGER, Description CHAR(40), Price DECIMAL(10, 2) )

Avoid Duplication – Create Two Tables CREATE TABLE Order ( Order_Id INTEGER PRIMARY KEY, Product_Code CHAR(11), Quantity INTEGER ) CREATE TABLE Product ( Product_Code CHAR(11) PRIMARY KEY, Description CHAR(40), Price DECIMAL(10, 2) )

Learning Activity 1 Problem description: –Normalize the following database definition. The intention is to represent an order with information about the customer, the order, and multiple line items.

Learning Activity 2 Problem description: –Draw an entity-relationship diagram for the tables you designed for storing orders.

SQL Four basic statements: oSELECT - selects data from tables oINSERT - inserts new data into a table oUPDATE - modifies existing rows in a table oDELETE - removes rows from a table

SQL (cont’d) SELECT * FROM Customer –Selects all columns from Customer table SELECT City, State FROM Customer –Selects only the City and State columns SELECT * FROM Customer WHERE State = ‘CA’ –Selects all customers who live in CA SELECT COUNT(*) FROM Customer WHERE State = ‘CA’ –Counts number of rows where State is CA

SQL (cont’d) INSERT INTO Customer VALUES (‘John Doe’, ‘Columbus’, ‘OH’) –Inserts a new customer record UPDATE Customer SET State = ‘OH’ WHERE State = ‘CA’ –Moves all customers who live in CA to OH DELETE FROM Customer –Deletes all rows from the Customer table DELETE FROM Customer WHERE State = ‘CA’ –Deletes all customers who live in CA

JDBC Java DataBase Connectivity - a set of classes and interfaces defined in the java.sql package Allows Java applications to connect to databases in a (mostly) database- independent way

JDBC (cont’d) The classes in java.sql are defined in a generic way, so they can be used with many databases The database-specific code is contained in a driver, which is usually provided by the database vendor Drivers are manipulated using the DriverManager class from java.sql

JDBC

JDBC Driver Types Type 1: JDBC-ODBC Bridge Type 2: Native API Driver Type 3: Network Protocol Driver Type 4: Native Protocol Driver

Type 1 - JDBC - ODBC Bridge Pros: Database independent Cons: –Windows only –Performance –ODBC driver must be present

Type 2 – Native API Driver Pros: better performance vs. type 1 Cons: –Client library must be present –Platform-dependant

Type 3 - Network Protocol Driver Pros: –No database library on client –Client is DB-independent Cons: –Extra layer –DB-specific coding required in middleware

Type 4 – Native Protocol Driver Pros: –Best performance –Pure Java Cons: –Driver required for each database

Using JDBC Identify type of driver needed Obtain/Install driver Add driver to classpath (In Eclipse, configure build path)

Making a Connection to the DB Manually load the driver class*: Class.forName("org.hsqldb.jdbcDriver"); Establish connection conn = DriverManager.getConnection( "jdbc:hsqldb:hsql://localhost:9001", "jdbc:hsqldb:hsql://localhost:9001", "sa", // username "sa", // username ""); // password ""); // password DriverManager takes care of details * Starting with JDBC4 (part of Java 6), the driver is loaded automatically

Statement Types Statement : SQL is sent to database each time PreparedStatement : compiled version of statement is cached and executed more than once CallableStatement : used to call stored procedures

JDBC Statements Statement stmt = conn.createStatement(); String cmd = "INSERT INTO Users ('User1', 'Password')"; try { stmt.executeUpdate(cmd); } finally { stmt.close(); }

ResultSet Connection conn = null; Statement stmt = conn.createStatement(); try { ResultSet rs = stmt.executeQuery("SELECT * FROM PRODUCT"); try { while (rs.next()) { int id = rs.getInt("ITEMID"); double price = rs.getDouble("PRICE"); String desc = rs.getString("DESCRIPTION"); // Do something with data } finally { rs.close(); } finally { stmt.close(); }

ResultSet Statement stmt = conn.createStatement(); try { ResultSet rs = stmt.executeQuery("SELECT ID,PRICE,DESC FROM PRODUCT"); try { while (rs.next()) { int id = rs.getInt(1); double price = rs.getDouble(2); String desc = rs.getString(3); // Do something with data } finally { rs.close(); } finally { stmt.close(); }

hsqldb % Java Database Open-source database we will be using for labs We will be using the latest version: RC 9

hsqldb - Installation Instructions Download and unzip into a local directory (for example C:\java\db\hsqldb) Create.cmd file to start hsqldb server C:\java\db\hsqldb\data\StartHSQLDB.cmd) cd C:\java\db\hsqldb\data java -cp../lib/hsqldb.jar org.hsqldb.server.Server --database.0 file:mydb --dbname.0 xdb

Validate Installation Start server, and run Testdb class from documentation Start with Testdb.java (in Week 4 folder on Website) Add hsqldb.jar to build path

hsqldb CREATE SCHEMA PUBLIC AUTHORIZATION DBA CREATE MEMORY TABLE SAMPLE_TABLE(ID INTEGER GENERATED BY DEFAULT AS IDENTITY(START WITH 0) NOT NULL PRIMARY KEY,STR_COL VARCHAR(256),NUM_COL INTEGER) ALTER TABLE SAMPLE_TABLE ALTER COLUMN ID RESTART WITH 8 CREATE USER SA PASSWORD "" GRANT DBA TO SA SET WRITE_DELAY 10 SET SCHEMA PUBLIC INSERT INTO SAMPLE_TABLE VALUES(0,'Ford',100) INSERT INTO SAMPLE_TABLE VALUES(1,'Toyota',200) INSERT INTO SAMPLE_TABLE VALUES(2,'Honda',300) INSERT INTO SAMPLE_TABLE VALUES(3,'GM',400) INSERT INTO SAMPLE_TABLE VALUES(4,'Ford',100) INSERT INTO SAMPLE_TABLE VALUES(5,'Toyota',200) INSERT INTO SAMPLE_TABLE VALUES(6,'Honda',300) INSERT INTO SAMPLE_TABLE VALUES(7,'GM',400)

Lab 4-1 Database (Hypersonic) Set Up Due May 30 th !