Database Management Systems

Slides:



Advertisements
Similar presentations
Copyright © by Royal Institute of Information Technology Introduction To Structured Query Language (SQL) 1.
Advertisements

JDBC CS-328. JDBC Java API for accessing RDBMS Allows use of SQL for RDBMS programming Can be used for: –embedded SQL –execution of stored queries.
Introduction to Structured Query Language (SQL)
1 JDBC Java Database Connectivity. 2 c.pdf
1 JDBC: Java Database Connectivity. 2 Introduction to JDBC JDBC is used for accessing databases from Java applications Information is transferred from.
Object-Oriented Programming MISM/MSIT Carnegie Mellon University Lecture 12: JDBC.
Structured Query Language (SQL) A2 Teacher Up skilling LECTURE 2.
Java Database Connectivity Vijayan Sugumaran Department of DIS Oakland University.
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.
Java Database Connectivity ASE. Java Database Connectivity (JDBC) l JDBC – provides an interface to Relational Data Sources l JDBC library provides the.
Javax.sql and java.sql. java.sql Interface Connection public interface Connection extends WrapperWrapper A connection (session) with a specific database.
Active Server Pages ASP is Microsoft’s server-side script engine for dynamically-generated web pages. Most common language used is VBScript. If you use.
Databases: Queries with Java Dr Andy Evans. JDBC SQL Three methods: Statements: Standard, simple, SQL. PreparedStatements: Compiled SQL statements that.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
JDBC (Java Database Connectivity) SNU OOPSLA Lab. October 2005.
JDBC Java and Databases, including Postgress. JDBC l Developed by Industry leaders l Three main goals: –JDBC should be an SQL-level API –JDBC should capitalize.
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.
1 Structured Query Language (SQL). 2 Contents SQL – I SQL – II SQL – III SQL – IV.
1 JDBC – Java Database Connectivity. 2 Introduction to JDBC JDBC is used for accessing databases from Java applications Information is transferred from.
1 JDBC Aum Amriteshwaryai Namah. 2 2 JDBC – Java DataBase Connectivity.
JDBC. Preliminaries Database Database Collection of data Collection of data DBMS DBMS Database management system Database management system Stores and.
Chapter 8 Databases.
JDBC Session 2 Tonight’s topics: 1.Prepared Statements 2.Transaction Processing 3.Callable Statements & Stored Procedures 4.Scrollable & Updatable Result.
JDBC – Java Database Concentricity
Copyright  Oracle Corporation, All rights reserved. 6 Accessing a Database Using the JDBC API.
CS146 References: ORACLE 9i PROGRAMMING A Primer Rajshekhar Sunderraman
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.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
Copyright © 2002 ProsoftTraining. All rights reserved. Building Database Client Applications Using JDBC 2.0.
Web Design & Development 1 Lec Web Design & Development 2 More on JDBC.
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.
Li Tak Sing COMPS311F. Database programming JDBC (Java Database Connectivity) Java version of ODBC (Open Database Connectivity) ODBC provides a standard.
CMPT 258 Database Systems The Relationship Model (Chapter 3)
JDBC and SQLJ CIS 612 Spring JDBC JDBC is an API that enables database access from Java programs JDBC for DB access provides ◦ Portability across.
Web Programming MySql JDBC Web Programming.
Access Databases from Java Programs via JDBC Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale
JDBC Part II CS 124. More about JDBC Types Statement versus PreparedStatement Timeout NULL values Meta-data close() methods Exceptions Transactions JDBC.
JDBC (Java Database Connectivity)
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.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Java Database Connectivity.
1 JDBC – Java Database Connectivity CS , Spring 2010.
Java Database Connectivity JDBC. Open Database Connectivity developed by Microsoft to provide interaction with databases using SQL. Use the JDBC-ODBC.
CS422 Principles of Database Systems JDBC and Embedded SQL Chengyu Sun California State University, Los Angeles.
JDBC Statements The JDBC Statement, CallableStatement, and PreparedStatement interfaces define the methods and properties that enables to send SQL or PL/SQL.
JDBC III IS Outline  Scrollable ResultSets  Updatable ResultSets  Prepared statements  Stored procedures.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
CS3220 Web and Internet Programming Database Access with JDBC
JDBC – Java Database Connectivity
JDBC III IS
Database Systems: Design, Implementation, and Management Tenth Edition
JDBC 21-Aug-18.
UNIT-2 Java Database Programming
JDBC.
SQL: Advanced Options, Updates and Views Lecturer: Dr Pavle Mogin
Design and Implementation of Software for the Web
JDBC – Java DataBase Connectivity
JDBC – Java DataBase Connectivity
Objectives In this lesson, you will learn about:
Interacting with Database
JDBC – Java Database Connectivity
JDBC Example.
Database Systems: Design, Implementation, and Management Tenth Edition
Lecture 11 Database Connection
CS3220 Web and Internet Programming Database Access with JDBC
CS3220 Web and Internet Programming Database Access with JDBC
JDBC – Java DataBase Connectivity
Java Chapter 6 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Database Management Systems DBMS Database Management Systems

Outline Introduction to relational databases Structured Query Language (SQL) Introduction to JDBC Connecting to a database Querying a database

Relational Databases Based on Mathematical Relations Table = Relation Row = Tuple Operators take relations as operands and produce relations as results

Basic SQL Data Types char[(n)] character[(n)] String of length n (right padded if necessary). Long strings are truncated. Default length is 1. varchar(n) character varying(n) String with max length n. Long strings truncated. smallint 16 bit signed integer int integer 32 bit signed integer numeric(p [,s]) Fixed point number with decimal precision p (number of digits) and scale s (digits to right of decimal point). decimal(p [,s]) Same as numeric, but may be represented internally with larger scale. float[(p)] Floating point number with maximum binary precision (bits) p. real double Floating point numbers with implementation dependent precision. Double more precise than real. date Month, day, and year. time Hour, minute, second. timestamp Combination of date and time.

Sample Database Design People ssn fname lname city state 1 * Autos vin make year Accidents accident_date damages 0..1 * * * drives owns * handles Agents salary photo 0..1

Sample Database Tables People (ssn char(11), fname varchar(12), lname varchar(15), city varchar(12), state char(2)) Agents (ssn char(11), salary float, photo blob) Autos (vin varchar(12), make varchar(10), year int, agent_ssn char(11)) Accidents (accident_date date, damages float, vin char(12), driver_ssn char(11)) Owns (ssn char(11), vin char(12))

Basic SQL Statements CREATE TABLE INSERT SELECT UPDATE DELETE SQL is not sensitive to spacing and line breaks. SQL is not case sensitive in regards to keywords and identifiers (e.g. table names, columns names, etc.). SQL is case sensitive in regards to character data (e.g. inside quotation marks). SQL statements are not terminated with semicolons!

CREATE TABLE Statement CREATE TABLE table_name (attribute type, … ) CREATE TABLE autos( vin varchar(12) primary key, make varchar(10), year integer, agent_ssn char(11))

INSERT Statement INSERT INTO table_name (attribute, …) VALUES (value, …) INSERT INTO people (lname, ssn) VALUES (‘SMITH’, ‘111-22-3333’) INSERT INTO people VALUES (‘123-45-6789’, ‘JANE’, ‘DOE’, ‘BOSTON’, ‘MA’) Attribute names may be omitted only when all attributes are supplied – in the same order as in the table definition.

SELECT Statement SELECT attribute, … FROM table_name, … WHERE condition GROUP BY attribute, … HAVING condition ORDER BY attribute, … Only the select and from clauses are required Examples: SELECT ssn, fname, lname FROM people WHERE state = ‘MA’ ORDER BY lname, fname SELECT * FROM people The * means all attributes (columns) No where clause means all rows

SELECT DISTINCT fname, lname FROM people The keyword distinct after select, ensures that there are no duplicate rows in the output. SELECT DISTINCT fname, lname FROM people There may be two rows with the same value for fname, or the same value for lname, but no two rows that are completely identical.

UPDATE Statement UPDATE table SET attribute = value, … WHERE condition UPDATE people SET city = ‘Boston’, state = ‘MA’ WHERE ssn = ‘012-34-5678’ If there is no where clause, all rows in the table will be updated!

DELETE Statement DELETE FROM table_name WHERE condition DELETE from people WHERE ssn = ‘111-22-3333’ WHERE state <> ‘MA’ If there is no where clause, all rows are deleted!!!

Aggregate Functions min max sum avg count SELECT avg(damages) FROM accidents WHERE accident_date > ‘2009-12-31’

Aggregate Functions (2) Number of accidents in the database: select count(*) from accidents Number of accidents where the damaged vehicle had a driver: select count(driver_ssn) from accidents Number of different people who have been drivers in accidents: select count (distinct driver_ssn) from accidents select distinct count(driver_ssn) from accidents

Grouping After rows are filtered by the where clause, they may be grouped with a group by clause. The output will include only one row for each group. Only the attributes by which rows are grouped and aggregate values can be selected. Groups may be filtered with a having clause (similar to how rows are filtered by the where clause).

Grouping (2) SELECT city, state, count(ssn) FROM people WHERE lname < ‘G’ GROUP BY city, state HAVING count(ssn) > 5

Selecting from multiple tables If two or more tables are listed in the from clause, the tables are first joined by taking their cartesian product. The rows are concatenated in all possible combinations. The cartesian product of a table of i columns and m rows with a table of j columns and n rows produces a table of i+j columns and m*n rows.

Multiple tables (2) Often times we want a natural join, where attributes with the same names must match values. SELECT fname, lname, vin FROM people, owns WHERE people.ssn = owns.ssn Without the where clause, every name would appear with every car.

Tuple Variables (renaming tables) Sometimes useful just to save typing: SELECT fname, lname, vin FROM people p, owns o WHERE p.ssn = o.ssn Sometimes necessary (e.g. find vin’s of cars that are jointly owned by more than one person): SELECT o1.vin FROM owns o1, owns o2 WHERE o1.vin = o2.vin AND o1.ssn <> o2.ssn

Joins Form a new relation by combining tuples from two other relations. Typically used in from clause. Join condition defines which tuples in the two relations match, and what attributes are present in the result of the join. Join type defines how tuples in each relation that do not match any tuple in the other relation (based on the join condition) are treated.

Join Conditions Natural Using (A1, A2, ... ) On <predicate> Tuples match if attributes with the same names have the same values. Two attributes with the same name are combined into a single column. Using (A1, A2, ... ) Each of the listed attributes (A1, A2, ... ) must appear in both relations. Like natural join, except that only the listed attributes have to match, instead of all attributes with the same names. The result will have only one column for each of the listed attributes. On <predicate> The predicate can specify any arbitrary join condition. Every column from both relations is retained, even if the predicate specifies that some attributes must be equal.

Join Type inner join left outer join right outer join Each row of the result combines matching tuples from the two joined relations. If a tuple from one of the relations doesn't match any tuple in the other relation, it does not appear in the result. The keyword inner is optional. (MySQL does not allow the keyword inner.) left outer join If a tuple from the relation on the left hand side does not match any tuple from the relation on the right hand side, it is included in the result with null values supplied for attributes of the right hand relation. right outer join If a tuple from the relation on the right hand side does not match any tuple from the relation on the left hand side, it is included in the result with null values supplied for attributes of the left hand relation. full outer join (not supported in MySQL) All tuples from both joined relations are included in the result, with null values supplied where neccessary.

Join Examples (for MySQL omit optional keywords inner and outer) Find names of car owners and the vin numbers of the cars they own: SELECT fname, lname, vin FROM people NATURAL INNER JOIN owns SELECT fname, lname, vin FROM people INNER JOIN OWNS USING (ssn) SELECT fname, lname, vin FROM people INNER JOIN OWNS ON people.ssn = owns.ssn

To include all people (even if they don't own any cars):   SELECT fname, lname, vin FROM people NATURAL LEFT OUTER JOIN owns SELECT fname, lname, vin FROM owns NATURAL RIGHT OUTER JOIN people

Find number of agents who live in each state Find number of agents who live in each state. Include states with zero agents: SELECT state, count(agents.ssn) FROM agents NATURAL RIGHT JOIN people GROUP BY state Find names of people who do not own cars: SELECT fname, lname FROM people NATURAL LEFT OUTER JOIN owns WHERE vin IS NULL Find ssn's of people who either own cars or have been the driver in an accident, but not both: SELECT ssn, driver_ssn FROM owns FULL OUTER JOIN accidents ON ssn = driver_ssn WHERE ssn IS NULL OR driver_ssn IS NULL

NULL Values The value NULL, indicates that the attribute is missing in the database. It means the value is unknown. It does not mean “zero”. It does not mean the attribute has no value. Aggregate functions ignore rows where the aggregated attribute is NULL. Except in the expression: count(*) which counts all rows.

NULL Values (2) SQL uses a 3 valued boolean logic with values true, false, and unknown: AND T F U OR T F U

NULL Values (3) Explain: select count(ssn) from agents where salary > 60000 ==> 20 select count(ssn) from agents where salary <= 60000 ==> 10 select count(ssn) from agents where salary > 60000 OR salary <= 60000 ==> 30 select count(ssn) from agents ==> 32 select count(ssn) from agents where salary = NULL ==> 0

JDBC Can be used by applications and applets(with restrictions) Provides an interface to SQL

Java application JDBC Driver Manager Bridge JDBC/ODBC Driver Driver JDBC ODBC DB DB

Basic Steps in Accessing a DB Load the JDBC drivers Use the DriverManager to create a Connection Use the Connection object to create a Statement object Use the Statement object to execute queries and obtain ResultSet objects Close the Connection

Connecting to a Database Connection con; Statement stmt; try { Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" ); String url = "jdbc:odbc:myDB.mdb"; String user = "user"; String password = "****"; con = DriverManager.getConnection( url, user, password ); } catch (SQLException e) { // … }

Creating a Statement try { stmt = con.createStatement(); } catch( SQLException ex ) { // … }

Executing a Query try { ResultSet rs; rs = stmt.executeQuery( ”select * from student” ); while( rs.next() ){ System.out.println( “ID: “ + rs.getString(1)); “Name: “ + rs.getString(2)); “Major: “ + rs.getString(3) + “\n”); } rs.close(); } catch (SQLException e) { … }

Executing an Update TextField id; TextField name; // ... stmt.executeUpdate( ”insert into student (sid, name) ” + “values (‘“ + id.getText() + “’, ‘“ + name.getText() + “’)” );

public interface Connection A Connection represents a session with a specific database. Within the context of a Connection, SQL statements are executed and results are returned. A Connection's database is able to provide information describing its tables, its supported SQL grammar, its stored procedures, the capabilities of this connection, etc. This information is obtained with the getMetaData method.

createStatement( ) prepareStatement ( String sql) public abstract Statement createStatement() throws SQLException SQL statements without parameters are normally executed using Statement objects. If the same SQL statement is executed many times, it is more efficient to use: public abstract PreparedStatement prepareStatement ( String sql) throws SQLException A SQL statement can be pre-compiled and stored in a PreparedStatement object. This object can then be used to efficiently execute this statement multiple times.

Automatic Commit For a connection in auto-commit mode, all SQL statements are executed and committed as individual transactions. Otherwise, its SQL statements are grouped into transactions that are terminated by either commit() or rollback(). By default, new connections are in auto-commit mode. The commit occurs when the statement completes or the next execute occurs, whichever comes first. In the case of statements returning a ResultSet, the statement completes when the last row of the ResultSet has been retrieved or the ResultSet has been closed.

Automatic Commit (2) public abstract void setAutoCommit( boolean autoCommit ) throws SQLException Sets autocommit. public abstract boolean getAutoCommit( ) throws SQLException Returns true if autocommit set, returns false otherwise.

commit( ) & rollback( ) public abstract void commit( ) throws SQLException Commit makes all changes made since the previous commit/rollback permanent and releases any database locks currently held by the Connection. This method should only be used when auto commit has been disabled. public abstract void rollback( ) throws SQLException Rollback drops all changes made since the previous commit/rollback and releases any database locks currently held by the Connection. This method should only be used when auto commit has been disabled.

close( ) & isClosed( ) public abstract void close() throws SQLException This method provides immediate release of Connection's database and JDBC resources instead of waiting for them to be automatically released. A Connection is automatically closed when it is garbage collected. public abstract boolean isClosed() throws SQLException Returns true if Connection is closed.

getMetaData( ) public abstract DatabaseMetaData getMetaData( ) throws SQLException A Connection's database can provide information describing its tables, its supported SQL grammar, its stored procedures, the capabilities of this connection, etc. This information is made available through a DatabaseMetaData object.

public interface Statement A Statement object is used for executing a static SQL statement and obtaining the results produced by it. Only one ResultSet per Statement can be open at any point in time. Therefore, if the reading of one ResultSet is interleaved with the reading of another, each must have been generated by different Statements. All statement execute methods implicitly close a statement's current ResultSet if an open one exists.

Executing SQL statements public abstract ResultSet executeQuery( String sql ) throws SQLException Execute a SQL statement that returns a single ResultSet public abstract int executeUpdate( String sql ) throws SQLException Execute a SQL INSERT, UPDATE or DELETE statement.

Controlling the size of the ResultSet public abstract int getMaxRows( ) throws SQLException public abstract void setMaxRows( int max ) throws SQLException The maxRows limit is the maximum number of rows that a ResultSet can contain. If the limit is exceeded, the excess rows are silently dropped.

Query time-out public abstract void setQueryTimeout( int seconds ) throws SQLException The queryTimeout limit is the number of seconds the driver will wait for a Statement to execute. If the limit is exceeded, a SQLException is thrown. If seconds == 0 wait forever. public abstract int getQueryTimeout( ) throws SQLException If zero returned : unlimited wait time

public interface ResultSet A ResultSet provides access to a table of data generated by executing a Statement. The table rows are retrieved in sequence. Within a row its column values can be accessed in any order. A ResultSet maintains a cursor pointing to its current row of data. Initially the cursor is positioned before the first row. The 'next' method moves the cursor to the next row.

next( ) public abstract boolean next() throws SQLException A ResultSet is initially positioned before its first row. The first call to next makes the first row the current row. The second call makes the second row the current row, etc. Returns true if the new current row is valid; false if there are no more rows

getXXX Methods The getXXX methods retrieve column values for the current row. Values can be retrieved by index number of the column (more efficient), or by the name of the column. Columns are numbered from 1. Column names used as input to getXXX methods are case insensitive.

getXXX Methods (2) The getXXX methods retrieve column values for the current row attempting to convert the underlying data to the specified Java type. (JDBC specification defines mappings from SQL types to Java types) All of them have the follow the specification patterns: public abstract XXX getXXX (int columnIndex) throws SQLException (String columnName) throws SQLException

String getString boolean getBoolean byte getByte short getShort int getInt long getLong float getFloat double getDouble byte[ ] getBytes Date getDate Time getTime Timestamp getTimestamp

Scrolling and Updates A default ResultSet object is not updatable and has a cursor that moves forward only. It is possible to produce ResultSet objects that are scrollable and/or updatable: Statement stmt = con.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE); The statement object will produce result sets that are scrollable (but not sensitive to changes by others) and updatable.

Scrolling methods first() last() beforeFirst() afterLast() next() previous() absolute(n) relative(n) moveToInsertRow()

updateXXX Methods Similar to the getter methods, getXXX All of them have the follow the specification patterns: public abstract void updateXXX (int columnIndex, XXX value) throws SQLException (String columnName, XXX value) throws SQLException

Updating an existing row // move the cursor to the fifth row of rs rs.absolute(5); // update the NAME column of row 5 to be SMITH rs.updateString("NAME", ”SMITH"); // update the row in the data source rs.updateRow();

Inserting a new row // Go to the special “insert row” // Store values in the columns with updateXXX // Use insertRow to update the data source // Return cursor to previous position rs.moveToInsertRow(); rs.updateString(1, ”SMITH"); rs.updateInt(2, 35); rs.updateBoolean(3, true); rs.insertRow(); rs.moveToCurrentRow();

ResultSet - other methods public abstract ResultSetMetaData getMetaData() throws SQLException The number, types and properties of a ResultSet's columns are provided by the getMetaData method. public abstract int findColumn( String columnName ) throws SQLException Maps a Resultset column name to a ResultSet column index.

public interface ResultSetMetaData Provides the information about the types and properties of the columns in a ResultSet. Some methods: int getColumnCount() int isNullable(int column) String getColumnName(int column) String getTableName(int column) int getColumnType(int column) boolean isReadOnly(int column) boolean isWritable(int column)

Reading blobs ResultSet rs = statement.executeQuery( "select * from agents where photo is not null"); while (rs.next()) { String ssn = rs.getString(1); Blob photo = rs.getBlob(3); int length = (int)photo.length(); InputStream is = photo.getBinaryStream(); byte[] data = new byte[length]; is.read(data, 0, length); new ImageViewer(data, ssn); // not standard }

Writing blobs (mysql) FileInputStream fis = new FileInputStream("office.jpg"); dbConnection.setAutoCommit(false); PreparedStatement ps = dbConnection.prepareStatement ( "update agents set photo = ? where ssn = ?"); ps.setBinaryStream(1, fis, 10047); ps.setString(2, “238-96-0401”); ps.executeUpdate(); ps.close(); dbConnection.commit(); dbConnection.setAutoCommit(true);

Writing blobs (oracle) FileInputStream fis = new FileInputStream("office.jpg"); dbConnection.setAutoCommit(false); statement.executeUpdate("update agents set photo = empty_blob() where ssn = '282-16-3333'"); ResultSet rs = statement.executeQuery ( "select * from agents where ssn = '282-16-3333'"); rs.next(); oracle.sql.BLOB photo = ((oracle.jdbc.driver.OracleResultSet) rs).getBLOB(3); byte[] data = new byte[10047]; fis.read(data, 0, 10047); photo.putBytes(1, data); // in oracle.sql.BLOB and jdk 1.4 dbConnection.commit(); dbConnection.setAutoCommit(true);