Why Is This Important? Database Application Development. SQL in Application Code. Overview. SQL in Application Code (Contd.



Similar documents
Database Application Development. Overview. SQL in Application Code. Chapter 6

Overview. Database Application Development. SQL in Application Code (Contd.) SQL in Application Code. Embedded SQL. Embedded SQL: Variables

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

CS 377 Database Systems SQL Programming. Li Xiong Department of Mathematics and Computer Science Emory University

COSC344 Database Theory and Applications. Java and SQL. Lecture 12

Services. Relational. Databases & JDBC. Today. Relational. Databases SQL JDBC. Next Time. Services. Relational. Databases & JDBC. Today.

Database Access from a Programming Language: Database Access from a Programming Language

Database Access from a Programming Language:

The JAVA Way: JDBC and SQLJ

JDBC (Java / SQL Programming) CS 377: Database Systems

Java and Databases. COMP514 Distributed Information Systems. Java Database Connectivity. Standards and utilities. Java and Databases

CS346: Database Programming.

1 SQL Data Types and Schemas

Chapter 9 Java and SQL. Wang Yang wyang@njnet.edu.cn

What is ODBC? Database Connectivity ODBC, JDBC and SQLJ. ODBC Architecture. More on ODBC. JDBC vs ODBC. What is JDBC?

SQL and Java. Database Systems Lecture 19 Natasha Alechina

Course Objectives. Database Applications. External applications. Course Objectives Interfacing. Mixing two worlds. Two approaches

CS/CE 2336 Computer Science II

Self-test Database application programming with JDBC

Chapter 13. Introduction to SQL Programming Techniques. Database Programming: Techniques and Issues. SQL Programming. Database applications

More SQL: Assertions, Views, and Programming Techniques

CS2506 Operating Systems II Lab 8, 8 th Tue/03 /2011 Java API

Using Netbeans and the Derby Database for Projects Contents

Chapter 9, More SQL: Assertions, Views, and Programming Techniques

LSINF1124 Projet de programmation

Database Access Through Java Technologies

How To Use The Database In Jdbc.Com On A Microsoft Gdbdns.Com (Amd64) On A Pcode (Amd32) On An Ubuntu (Amd66) On Microsoft

Performance Tuning for the JDBC TM API

Working With Derby. Version 10.2 Derby Document build: December 11, 2006, 7:06:09 AM (PST)

Package sjdbc. R topics documented: February 20, 2015

Abstract. Introduction. Web Technology and Thin Clients. What s New in Java Version 1.1

DEVELOPING MULTITHREADED DATABASE APPLICATION USING JAVA TOOLS AND ORACLE DATABASE MANAGEMENT SYSTEM IN INTRANET ENVIRONMENT

Supplement IV.D: Tutorial for MS Access. For Introduction to Java Programming By Y. Daniel Liang

Using DOTS as Apache Derby System Test

SQL/PSM. Outline. Database Application Development Oracle PL/SQL. Why Stored Procedures? Stored Procedures PL/SQL. Embedded SQL Dynamic SQL

Part IV: Java Database Programming

Chapter 5. SQL: Queries, Constraints, Triggers

Java DB Performance. Olav Sandstå Sun Microsystems, Trondheim, Norway Submission ID: 860

Java DataBase Connectivity (JDBC)

SQL: Queries, Programming, Triggers

Example Instances. SQL: Queries, Programming, Triggers. Conceptual Evaluation Strategy. Basic SQL Query. A Note on Range Variables

SQL: Queries, Programming, Triggers

A Brief Introduction to MySQL

Statement Level Interface. Call Level Interface. Static SQL. Status. Connections. Transactions. To connect to an SQL database, use a connect statement

Application Development A Cocktail of Java and MCP. MCP Guru Series Dan Meyer & Pramod Nair

SQL and Programming Languages. SQL in Programming Languages. Applications. Approaches

Mimer SQL. Programmer s Manual. Version 8.2 Copyright 2000 Mimer Information Technology AB

Making Oracle and JDBC Work For You

SQL and programming languages

Applets, RMI, JDBC Exam Review

TECH TUTORIAL: EMBEDDING ANALYTICS INTO A DATABASE USING SOURCEPRO AND JMSL

Week 5: Embedded SQL. Embedded SQL 4. Application Program. Interactive vs. Non-Interactive SQL. Update Statements

CS506 Web Design and Development Solved Online Quiz No. 01

Database Migration from MySQL to RDM Server

Chapter 1 JDBC: Databases The Java Way! What Is The JDBC? The JDBC Structure ODBC s Part In The JDBC Summary

Embedded SQL programming

How To Use A Sas Server On A Java Computer Or A Java.Net Computer (Sas) On A Microsoft Microsoft Server (Sasa) On An Ipo (Sauge) Or A Microsas (Sask

Configuring Apache Derby for Performance and Durability Olav Sandstå

Oracle8/ SQLJ Programming

Programming Database lectures for mathema

Object Oriented Design with UML and Java. PART XVIII: Database Technology

Supplement IV.C: Tutorial for Oracle. For Introduction to Java Programming By Y. Daniel Liang

Using JML to protect Java code against SQL injection. Johan Janssen June 26, 2007

Database System Concepts

CHAPTER 3. Relational Database Management System: Oracle. 3.1 COMPANY Database

SQL Programming. CS145 Lecture Notes #10. Motivation. Oracle PL/SQL. Basics. Example schema:

Darshan Institute of Engineering & Technology PL_SQL

Java Server Pages and Java Beans

The release notes provide details of enhancements and features in Cloudera ODBC Driver for Impala , as well as the version history.

SQL: Programming. Introduction to Databases CompSci 316 Fall 2014

Prof. Edwar Saliba Júnior

Porting from Oracle to PostgreSQL

Jaybird 2.1 JDBC driver. Java Programmer's Manual

ANDROID APPS DEVELOPMENT FOR MOBILE GAME

Java (12 Weeks) Introduction to Java Programming Language

Real SQL Programming. Embedded SQL Call-Level Interface Java Database Connectivity

Querying Microsoft SQL Server

Real SQL Programming 1

Java Programming. JDBC Spring Framework Web Services

The Data Access Handbook

SQL is capable in manipulating relational data SQL is not good for many other tasks

JDBC. It is connected by the Native Module of dependent form of h/w like.dll or.so. ex) OCI driver for local connection to Oracle

Boats bid bname color 101 Interlake blue 102 Interlake red 103 Clipper green 104 Marine red. Figure 1: Instances of Sailors, Boats and Reserves

Java Application Developer Certificate Program Competencies

ERserver. DB2 Universal Database for iseries SQL Programming with Host Languages. iseries. Version 5

Using SQL Server Management Studio

FileMaker 11. ODBC and JDBC Guide

An Introduction to SQL Injection Attacks for Oracle Developers. January 2004 INTEGRIGY. Mission Critical Applications Mission Critical Security

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Hadoop Integration Guide

Course ID#: W 35 Hrs. Course Content

Microsoft SQL Server Features that can be used with the IBM i

Database Programming with PL/SQL: Learning Objectives

Chapter 4: SQL. Schema Used in Examples

Transcription:

Why Is This Important? Database Application Development Chapter 6 So far, accessed DBMS directly through client tools Great for interactive use How can we access the DBMS from a program? Need an interface between programming language and DBMS Many different options Our focus: JDBC 1 2 Overview SQL in application code Embedded SQL Cursors JDBC Stored procedures SQL in Application Code SQL commands can be called from within a host language (e.g., C++ or Java) program. SQL statements can refer to host variables (including special variables used to return status). Must include a statement to connect to the right database. Two main integration approaches: Embed SQL in the host language (Embedded SQL, SQLJ) Create special API to call SQL commands (JDBC) 3 4 SQL in Application Code (Contd.) Impedance mismatch: SQL relations are (multi-) sets of records, with no a priori bound on the number of records. No such data structure existed traditionally in procedural programming languages such as C. SQL supports a mechanism called a cursor to handle this. Cursor essentially is a more powerful iterator Embedded SQL Approach: Embed SQL in the host language. A preprocessor converts SQL statements into special API calls. Then a regular compiler is used to compile the code. Language constructs: Connecting to a database: EXEC SQL CONNECT Declaring variables: EXEC SQL BEGIN (END) DECLARE SECTION Statements: EXEC SQL Statement; 5 6

Embedded SQL in C: Variables EXEC SQL BEGIN DECLARE SECTION char c_sname[20]; long c_sid; short c_rating; float c_age; EXEC SQL END DECLARE SECTION Two special error variables: SQLCODE (long, is negative if an error has occurred) SQLSTATE (char[6], predefined codes for common errors) Cursors Can declare a cursor on a relation or query statement (which generates a relation). Can open a cursor and repeatedly fetch a tuple, then move the cursor until all tuples have been retrieved. Can use a special clause, called ORDER BY, in queries that are accessed through a cursor, to control the order in which tuples are returned. Fields in ORDER BY clause must also appear in SELECT clause. Can also modify/delete tuple pointed to by a cursor. 7 8 Cursor: Get names of sailors who reserved a red boat, in alphabetical order EXEC SQL DECLARE sinfo CURSOR FOR SELECT S.sname FROM Sailors S, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND B.color= red ORDER BY S.sname Cannot replace S.sname by, say, S.sid in the ORDER BY clause above (Why?) Can we add S.sid to the SELECT clause and replace S.sname by S.sid in the ORDER BY clause? Embedding SQL in C: An Example char SQLSTATE[6]; EXEC SQL BEGIN DECLARE SECTION char c_sname[20]; short c_minrating; float c_age; EXEC SQL END DECLARE SECTION c_minrating = random(); EXEC SQL DECLARE sinfo CURSOR FOR SELECT S.sname, S.age FROM Sailors S WHERE S.rating > :c_minrating ORDER BY S.sname; do { EXEC SQL FETCH sinfo INTO :c_sname, :c_age; printf( %s is %d years old\n, c_sname, c_age); while (SQLSTATE!= 02000 ); EXEC SQL CLOSE sinfo; 9 10 Database APIs: Alternative to embedding Rather than modify compiler, add library with database calls (API) Advantage: executable is also DBMS-independent Embedded is SQL DBMS-independent only at source-code level Pass SQL strings from language, present result sets in a language-friendly way Sun s JDBC: Java API Supposedly DBMS-neutral A driver traps the calls and translates them into DBMS-specific code Driver loaded dynamically and on-demand Database can be across a network JDBC Architecture Components Application Initiates and terminates connections, submits SQL statements Driver manager Loads JDBC driver, passes JDBC calls from app to correct driver Driver Connects to data source, transmits requests and returns/translates results and error codes Data source (DBMS) Processes SQL statements 11 12

JDBC Architecture (Pure Java) JDBC Architecture (Not Pure Java) Left side: type 4 driver Allows direct call from client to DBMS, pure Java Converts JDBC calls into network protocol used by DBMS Right side: type 3 driver Translates JDBC calls into middleware protocol Middleware translates this to DBMS protocol Useful when connecting to many different DBMSes Source: java.sun.com 13 Left side: type 1 driver JDBC access via ODBC drivers Each client using the bridge must have ODBC binary code Right side: type 2 driver Converts JDBC calls into calls on the DBMS client API Needs binary code on client machine Source: java.sun.com 14 JDBC Classes and Interfaces Steps to submit a database query: Load the JDBC driver Connect to the data source Execute SQL statements Connecting to A DBMS private Connection getdbconnection() { Connection con = null; // Load the driver Class.forName(myDbDriver).newInstance(); catch (InstantiationException e) { e.printstacktrace(); catch (IllegalAccessException e) { e.printstacktrace(); catch (ClassNotFoundException e) { e.printstacktrace(); Important: make sure you include the driver in the classpath Driver jar file sqljdbc4.jar needs to be in the classpath Should be there by default on Windows lab machines String connectionurl = myurl ; con = DriverManager.getConnection(connectionURL); catch (SQLException e) { e.printstacktrace(); return con; 15 16 Connection Data Connections in JDBC MSFT JDBC driver for SQL Server dbdriver = "com.microsoft.sqlserver.jdbc.sqlserverdriver ; connectionurl = jdbc:sqlserver://address:1433; + databasename=xyz;user=you;password=secret; ; In the JDBC API 4.0, the DriverManager.getConnection method is enhanced to load JDBC drivers automatically. Do not need to call the Class.forName method to register or load the driver when using the sqljdbc4.jar class library. When the getconnection method of the DriverManager class is called, an appropriate driver is located from the set of registered JDBC drivers. sqljdbc4.jar file includes "META-INF/services/java.sql.Driver" file, which contains the com.microsoft.sqlserver.jdbc.sqlserverdriver as a registered driver. Notice: We interact with a data source through sessions. Each connection identifies a logical session. JDBC URL: jdbc:<subprotocol>:<otherparameters> Multiple users: each has his/her own session(s) 17 18

Important Imports For JDBC import java.sql.connection; import java.sql.drivermanager; import java.sql.resultset; import java.sql.sqlexception; import java.sql.statement; Running A Simple SQL Query public List getspeciesnames() { Connection con = getdbconnection(); List species = new ArrayList(); Statement S = con.createstatement(); // Get query results ResultSet rs = S.executeQuery( "SELECT DISTINCT " + speciescolname + " FROM " + scorestablename); // Copy results into list while (rs.next()) { String speciesname = rs.getstring(speciescolname); species.add(speciesname); rs.close(); con.close(); catch (SQLException e) { e.printstacktrace(); return species; 19 20 Connection Interface Can set auto-commit mode Auto-commit on: each statement considered its own transaction, no need for explicit commit() boolean getautocommit(), void setautocommit(boolean autocommit) Can set transaction isolation level Connection.TRANSACTION_READ_UNCOMMITTED, Connection.TRANSACTION_READ_COMMITTED, Connection.TRANSACTION_REPEATABLE_READ, or Connection.TRANSACTION_SERIALIZABLE int gettransactionisolation(), void settransactionisolation(int level) Isolation, auto-commit covered later, for now use default Connection Interface (Contd.) Better performance possible for read-only access boolean isreadonly(), void setreadonly(boolean readonly) Check whether connection is still open boolean isclosed(), void close() Commit or abort transaction Use only when autocommit is false void commit(), void rollback() 21 22 Statement Interface Used to execute SQL statement and return its results execute(string sql) to execute any SQL statement executequery(string sql) to obtain single ResultSet object executeupdate(string sql) for INSERT, UPDATE, or DELETE Sub-interface PreparedStatement Precompiled SQL statement for efficiently executing a statement multiple times. Structure fixed, parameters determined at runtime PreparedStatement pstmt = connection.preparestatement("update EMPLOYEES SET SALARY =? WHERE ID =?"); pstmt.setbigdecimal(1, 153833.00); pstmt.setint(2, 110592); Sub-interface CallableStatement For calling SQL stored procedures through standard way for all RDBMSes SQL Stored Procedures What is a stored procedure? Program executed through a single SQL statement Executed in the process space of the server Advantages: Can encapsulate application logic while staying close to the data Reuse of application logic by different users Avoid tuple-at-a-time return of records through cursors Only final result is returned to Java app 23 24

Example Stored Procedure CREATE PROCEDURE getreservations @Name varchar(50), AS SET NOCOUNT ON; SELECT bid, date FROM Reserves R, Sailors S WHERE R.sid = S.sid AND S.name = @Name; GO Calling A Stored Procedure EXECUTE getreservations Joe ; -- Or EXEC getreservations @Name = Joe ; GO -- Or, if this procedure is the first statement within a batch: getreservations Joe ; Syntax for SQL Server, will be different for other DBMS 25 26 Stored Procedure with Output Parameters Calling Stored Procedures with Output CREATE PROCEDURE getreservationcnt @SailorID int, @ResCnt int OUT AS SET NOCOUNT ON; SET @ResCnt = (SELECT COUNT(*) FROM Reserves R WHERE R.sid = @SailorID); GO DECLARE @ResCnt int EXECUTE getreservationcnt 101, @ResCnt OUT PRINT The sailor made + RTRIM(CAST(@ResCnt AS varchar(20))) + reservations.' 27 28 Calling Stored Procedures from JDBC CallableStatement cs = null; // Procedure without parameters cs = con.preparecall( {call mystoredprocname ); cs.execute(); // Procedure with input parameters only cs = connection.preparecall("{call getreservations(?)"); cs.setstring(1, Joe ); cs.execute(); // Procedure with input and output parameters cs = connection.preparecall("{call getreservationcnt(?,?)"); cs.setint(1, 101); cs.registeroutparameter(2, Types.INT); // For parameters that are used for both input and output, // have both the set and registeroutparameter statement cs.execute(); int result = cs.getint(2); catch (SQLException e) { 29 ResultSet Interface Maintains a cursor, initially positioned before first row Next() method advances cursor, returns false if no more rows Default: not updateable, cursor can only move forward Can be changed, of course Can update database by updating ResultSet Update column values of current row or delete entire row Insert new row by setting values of special insert row GetString(), getboolean() etc. to retrieve values in columns Access through column number or name 30

Matching Java and SQL Data Types Exceptions and Warnings SQL Type BIT CHAR VARCHAR DOUBLE FLOAT Java class Boolean String String Double Double ResultSet get method getboolean() getstring() getstring() getdouble() getdouble() Most of java.sql can throw an SQLException if an error occurs. SQLWarning is a subclass of SQLException Not as severe Not thrown and their existence has to be explicitly tested INTEGER Integer getint() REAL Double getfloat() DATE java.sql.date getdate() TIME java.sql.time gettime() TIMESTAMP java.sql.timestamp gettimestamp() 31 32 Exceptions and Warnings (Contd.) Examining Database Metadata stmt=con.createstatement(); warning=con.getwarnings(); while(warning!= null) { // handle SQLWarnings; warning = warning.getnextwarning(): con.clearwarnings(); stmt.executeupdate(querystring); warning = con.getwarnings(); //end try catch( SQLException SQLe) { // handle the exception DatabaseMetaData object gives information about the database system and the catalog. DatabaseMetaData md = con.getmetadata(); // Print information about the driver System.out.println( Name: + md.getdrivername() + version: + md.getdriverversion()); 33 34 Database Metadata (Contd.) Summary DatabaseMetaData md=con.getmetadata(); ResultSet trs=md.gettables(null,null,null,null); String tablename; While(trs.next()) { tablename = trs.getstring( TABLE_NAME ); System.out.println( Table: + tablename); // Print all attributes ResultSet crs = md.getcolumns(null,null,tablename, null); while (crs.next()) { System.out.println(crs.getString( COLUMN_NAME +, ); Embedded SQL allows execution of parameterized static queries within a host language Cursor mechanism allows retrieval of one record at a time and bridges impedance mismatch between host language and SQL APIs such as JDBC introduce a layer of abstraction between application and DBMS Stored procedures execute application logic directly at the server 35 36