SQL DML and DB Applications, JDBC

Similar documents
CSCC43H: Introduction to Databases. Lecture 9

Non-interactive SQL. EECS Introduction to Database Management Systems

SQL: Programming Midterm in class next Thursday (October 5)

The Relational Model Constraints and SQL DDL

Embedded SQL. Introduction

Embedded SQL. Davood Rafiei

JDBC Architecture. JDBC API: This provides the application-to- JDBC Manager connection.

Instructor: Jinze Liu. Fall 2008

ERwin and JDBC. Mar. 6, 2007 Myoung Ho Kim

You write standard JDBC API application and plug in the appropriate JDBC driver for the database the you want to use. Java applet, app or servlets

SQL: Programming. Announcements (September 25) Motivation. CPS 116 Introduction to Database Systems. Pros and cons of SQL.

SQL in a Server Environment

Kyle Brown Knowledge Systems Corporation by Kyle Brown and Knowledge Systems Corporation

Outline. Lecture 10: Database Connectivity -JDBC. Java Persistence. Persistence via Database

Questions and Answers. A. A DataSource is the basic service for managing a set of JDBC drivers.

Embedded SQL. csc343, Introduction to Databases Renée J. Miller and Fatemeh Nargesian and Sina Meraji Winter 2018

EMBEDDED SQL. SE 3DB3 Fall 2016 MICHAEL LIUT DEPARTMENT OF COMPUTING AND SOFTWARE MCMASTER UNIVERSITY

Accessing databases in Java using JDBC

Introduction to Databases

JDBC [Java DataBase Connectivity]

Lecture 2. Introduction to JDBC

Real SQL Programming 1

Embedded SQL. csc343, Introduction to Databases Diane Horton with examples from Ullman and Widom Fall 2014

Programming in Java

Self-test Database application programming with JDBC

Overview. Database Application Development. SQL in Application Code. SQL in Application Code (cont.)

Database Application Development

Database Application Development

Lecture 9&10 JDBC. Mechanism. Some Warnings. Notes. Style. Introductory Databases SSC Introduction to DataBases 1.

Database Application Development

This lecture. Databases - JDBC I. Application Programs. Database Access End Users

JDBC, Transactions. Niklas Fors JDBC 1 / 38

Database Programming. Week 9. *Some of the slides in this lecture are created by Prof. Ian Horrocks from University of Oxford

UNIT III - JDBC Two Marks

Introduction to JDBC. JDBC: Java Database Connectivity. Why Access a Database with Java? Compilation. Six Steps. Packages to Import

Cyrus Shahabi Computer Science Department University of Southern California C. Shahabi

Application Programming for Relational Databases

Logging and Recovery. 444 Section, April 23, 2009

Database Design and Programming

Database Programming Overview. COSC 304 Introduction to Database Systems. Database Programming. JDBC Interfaces. JDBC Overview

The Design of JDBC The Structured Query Language Basic JDBC Programming Concepts Query Execution Scrollable and Updatable Result Sets

Schedule. Feb. 12 (T) Advising Day. No class. Reminder: Midterm is Feb. 14 (TH) Today: Feb. 7 (TH) Feb. 21 (TH) Feb. 19 (T)

JDBC Programming: Intro

Persistency Patterns. Repository and DAO

PERSİSTENCE OBJECT RELATİON MAPPİNG

CSC System Development with Java. Database Connection. Department of Statistics and Computer Science. Budditha Hettige

DataBase Lab JAVA-DATABASE CONNECTION. Eng. Haneen El-masry

Discuss setting up JDBC connectivity. Demonstrate a JDBC program Discuss and demonstrate methods associated with JDBC connectivity

Database Application Development

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

CSCI/CMPE Object-Oriented Programming in Java JDBC. Dongchul Kim. Department of Computer Science University of Texas Rio Grande Valley

SQL and Java. Database Systems Lecture 20 Natasha Alechina

JAVA AND DATABASES. Summer 2018

Database Application Development

e-pg Pathshala Subject: Computer Science Paper: Web Technology Module: JDBC INTRODUCTION Module No: CS/WT/26 Quadrant 2 e-text

CSC309: Introduction to Web Programming. Lecture 6

Enterprise Java Unit 1- Chapter 6 Prof. Sujata Rizal

Calling SQL from a host language (Java and Python) Kathleen Durant CS 3200

Chapter 13 Introduction to SQL Programming Techniques

SQL: Transactions. Announcements (October 2) Transactions. CPS 116 Introduction to Database Systems. Project milestone #1 due in 1½ weeks

Unit 2 JDBC Programming

JDBC Drivers Type. JDBC drivers implement the defined interfaces in the JDBC API for interacting with your database server.

Chapter 9 SQL in a server environment

MANTHLY TEST SEPTEMBER 2017 QUESTION BANK CLASS: XII SUBJECT: INFORMATICS PRACTICES (065)

Introduction to SQL & Database Application Development Using Java

Chapter 3 DB-Gateways

Databases 2012 Embedded SQL

Part I: Stored Procedures. Introduction to SQL Programming Techniques. CSC 375, Fall 2017

INTRODUCTION TO JDBC - Revised spring

INTRODUCTION TO JDBC - Revised Spring

Database-Connection Libraries

CMPUT 391 Database Management Systems. JDBC in Review. - Lab 2 -

JDBC BASIC 19/05/2012. Objectives. Java Database Connectivity. Definitions of JDBC. Part 1. JDBC basic Working with JDBC Adv anced JDBC programming

CSE 308. Database Issues. Goals. Separate the application code from the database

Java E-Commerce Martin Cooke,

Chapter 3 DB-Gateways

Java.sql.sqlexception closed connection at. oracle.jdbc.driver.physicalconnection.pr eparestatement

Database in Applica.on Development. Debapriyo Majumdar DBMS Fall 2016 Indian Statistical Institute Kolkata

Java and the Java DataBase Connectivity (JDBC) API. Todd Kaufman April 25, 2002

Java Database Connectivity (JDBC) 25.1 What is JDBC?

Database Application Development

Server-side Web Programming

The Web Application Developer s. Red Hat Database. View. October 30, Webcast. Patrick Macdonald, Fernando Nasser. Red Hat Database Engineering

JDBC 3.0. Java Database Connectivity. 1 Java

Database-Connection Libraries. Java Database Connectivity PHP

IBM DB2 9 Application Developer. Download Full Version :

Chapter 4 Application Programs and Object-Relational Capabilities

Java Database Connectivity

Java Database Connectivity

JDBC SHORT NOTES. Abstract This document contains short notes on JDBC, their types with diagrams. Rohit Deshbhratar [ address]

Pieter van den Hombergh. March 25, 2018

Tutorial: Using Java/JSP to Write a Web API

Lab # 9. Java to Database Connection

Java Database Connectivity

Databases and MySQL. COMP 342: Programming Methods. 16 September Databases and MySQL

Vendor: IBM. Exam Code: Exam Name: DB2 9 Application Developer. Version: Demo

Top 50 JDBC Interview Questions and Answers

SQL Environment: Module Types. System Aspects of SQL. SQL Environment: Introduction. SQL Environment: Introduction. SQL Environment: Privileges

Working with Databases and Java

Database Applications (15-415)

Transcription:

SQL DML and DB Applications, JDBC Week 4.2 Week 4 MIE253-Consens 1

Schedule Week Date Lecture Topic 1 Jan 9 Introduction to Data Management 2 Jan 16 The Relational Model 3 Jan. 23 Constraints and SQL DDL 4 Jan. 30 SQL DML, DB Applications, JDBC 5 Feb 6 Relational Algebra 6 Feb 13 Advanced SQL - Feb 20 [Reading Week] 7 Feb 27 Review and Midterm (Mar 1) 8 Mar 5 OLAP 9 Mar 12 ER Conceptual Modelling 10 Mar 19 Normalization 11 Mar 26 XML and Data Integration 12 Apr 2 Transactions and the Internet, Query Processing 13 Apr 9 Final Review This week s reading: Chapter 5 (5.3) Chapter 8 (8.5, 8.2.5) Previous week s: Chapters 1-2 Chapter 3 Week 4 MIE253-Consens 2

JDBC Reading Material Chapter 8 Textbook (focus: Section 8.5) On-line JDBC tutorial http://docs.oracle.com/javase/tutorial/jdbc/i ndex.html See also, Lab handouts and code samples On-line JDBC reference http://docs.oracle.com/javase/1.3/docs/guide /jdbc/index.html Thinking in Java background reading (assigned in the Lab) Week 4 MIE253-Consens 3

JDBC Why? Gain a basic understanding of how to program Database-driven applications Gain exposure to enterprise class frameworks What is JDBC? - Call-level interface (CLI) for executing SQL from a Java program Call-level in Java = Method invocation SQL statements are constructed at run time as the value of a Java variable JDBC passes SQL statements to the underlying DBMS and receives the results Can interface any DBMS that has a JDBC driver Week 4 MIE253-Consens 4

JDBC Run-Time Architecture Access database JDBC-ODBC driver ODBC Data Source Java application driver manager SQLServer driver SQLServer database DB/2 driver DB/2 DBMS database Week 4 MIE253-Consens 5

Getting a connection import java.sql.*; -- import all classes in package java.sql Class.forName (driver name); // static method of class Class // loads specified driver Connection con = DriverManager.getConnection(Url, Id, Passwd); Static method of class DriverManager; attempts to connect to DBMS If successful, creates a connection object, con, for managing the connection Statement stat = con.createstatement (); Creates a statement object stat Statements have executequery() method Week 4 MIE253-Consens 6

JDBC Class Usage DriverManager Driver Connection Statement executequery ResultSet Week 4 MIE253-Consens 7

Executing a Query String query = SELECT T.StudId FROM Transcript T + WHERE T.CrsCode = MIE253 + AND T.Semester = S2012 ; ResultSet res = stat.executequery (query); Creates a result set object, res. Prepares and executes the query. Stores the result set produced by execution in res (analogous to opening a cursor). The query string can be constructed at run time (as above). The input parameters are plugged into the query when the string is formed (as above) Week 4 MIE253-Consens 8

Preparing a Query String query = SELECT T.StudId FROM Transcript T + WHERE T.CrsCode =? AND T.Semester =? ; placeholders PreparedStatement ps = con.preparestatement ( query ); Prepares the statement Creates a prepared statement object, ps, containing the prepared statement Placeholders (?) mark positions of in parameters; special API is provided to plug the actual values in positions indicated by the? s Week 4 MIE253-Consens 9

Executing a Prepared Query String crs_code, semester; ps.setstring(1, crs_code); // set value of first in parameter ps.setstring(2, semester); // set value of second in parameter ResultSet res = ps.executequery ( ); No Argument! Creates a result set object, res Executes the query Stores the result set produced by execution in res while ( res.next ( ) ) { // advance the cursor j = res.getint ( StudId ); // fetch output int-value process output value } Week 4 MIE253-Consens 10

JDBC Class Diagram Week 4 MIE253-Consens 11

Handling Exceptions try {...Java/JDBC code... } catch ( SQLException ex ) { exception handling code... } try/catch is the basic structure within which an SQL statement should be embedded If an exception is thrown, an exception object, ex, is created and the catch clause is executed Week 4 MIE253-Consens 12

Handling SQL Exceptions try { s.execute(); } catch (SQLException e) { out.println("sqlexception: " + e.getmessage()); out.println("sqlstate: " + e.getsqlstate()); out.println("vendorerror: " + e.geterrorcode()); } SQLException objects have methods to print an error message, return SQLSTATE from the DBMS, etc. Week 4 MIE253-Consens 13

Re-throwing Exceptions try { s.execute(); } catch (SQLException se ) { String SQLState = se.getsqlstate(); if ( SQLState.equals( "23505" ) ) { correctduplicatekey(); } else if ( SQLState.equals( "22003" ) ) { correctarithmeticoverflow(); } else { throw se; } } SQLExceptions can have some cases handled locally and then re-thrown for additional handling Week 4 MIE253-Consens 14

Transactions in JDBC Default for a connection is autocommit mode, where each SQL statement is a transaction Each statement committed (if completed ok) To group several statements into a transaction con.setautocommit (false) All the statements in transaction are committed con.commit() All the statements in transaction are aborted con.rollback() To change the isolation level of transactions con.settransactionisolationlevel (TRANSACTION_SERIALIZABLE) Week 4 MIE253-Consens 15