Embedded SQL. Database management system. program SQL. storage. files database. embedded. CMPT 354: Database I -- ODBC and JDBC 2

Similar documents
Chapter 4: SQL. Schema Used in Examples. Basic Structure. The select Clause. modifications and enhancements! A typical SQL query has the form:

Database System Concepts

Chapter 5: Advanced SQL

Chapter 4: SQL. Schema Used in Examples

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

The JAVA Way: JDBC and SQLJ

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

ODBC Sample Application for Tandem NonStop SQL/MX

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

Database Access from a Programming Language:

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

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

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

SQL and Java. Database Systems Lecture 19 Natasha Alechina

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

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

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

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

Apéndice C: Código Fuente del Programa DBConnection.java

Raima Database Manager 11.0

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

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

SQL and programming languages

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

ForDBC. Jörg Kuthe. Fortran Database Connectivity. QT software GmbH Konstanzer Str. 10 D Berlin Germany

IBM soliddb IBM soliddb Universal Cache Version 6.3. Programmer Guide SC

CS346: Database Programming.

Part 16: Application. Programming I. 16. Application Programming I (Embedded SQL, ODBC, JDBC) References:

ODBC Applications: Writing Good Code

FileMaker 14. ODBC and JDBC Guide

ForDBC. Jörg Kuthe. Fortran Database Connectivity. QT software GmbH Konstanzer Str. 10 D Berlin Germany

FileMaker 13. ODBC and JDBC Guide

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

ASP.NET Programming with C# and SQL Server

Developing an ODBC C++ Client with MySQL Database

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

Security Module: SQL Injection

More SQL: Assertions, Views, and Programming Techniques

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

CS/CE 2336 Computer Science II

Adaptive Server Enterprise ODBC Driver by Sybase

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

HOW-TO. Access Data using BCI. Brian Leach Consulting Limited.

LSINF1124 Projet de programmation

FileMaker 12. ODBC and JDBC Guide

Adaptive Server Enterprise ODBC Driver by Sybase

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

FileMaker 11. ODBC and JDBC Guide

Using Temporary Tables to Improve Performance for SQL Data Services

1 SQL Data Types and Schemas

DATABASDESIGN FÖR INGENJÖRER - 1DL124

Using IRDB in a Dot Net Project

Database Access via Programming Languages

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

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

Real SQL Programming 1

MAKING ORACLE AND SQLJ WORK FOR YOU John Jay King, King Training Resources

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

Spring,2015. Apache Hive BY NATIA MAMAIASHVILI, LASHA AMASHUKELI & ALEKO CHAKHVASHVILI SUPERVAIZOR: PROF. NODAR MOMTSELIDZE

A Brief Introduction to MySQL

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

2. Follow the installation directions and install the server on ccc

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

MySQL for Beginners Ed 3

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

PHP Language Binding Guide For The Connection Cloud Web Services

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

Java Server Pages and Java Beans

SOLID Programmer Guide

Writing MySQL Scripts With Python's DB-API Interface

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

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

1 File Processing Systems

Using Netbeans and the Derby Database for Projects Contents

Database Toolkit: Portable and Cost Effective Software

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

Programming Database lectures for mathema

CSc 230 Software System Engineering FINAL REPORT. Project Management System. Prof.: Doan Nguyen. Submitted By: Parita Shah Ajinkya Ladkhedkar

N3458: Simple Database Integration in C++11

public class ResultSetTable implements TabelModel { ResultSet result; ResultSetMetaData metadata; int num cols;

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

Package sjdbc. R topics documented: February 20, 2015

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

Trusted RUBIX TM. Version 6. ODBC Guide. Revision 7 RELATIONAL DATABASE MANAGEMENT SYSTEM TEL Infosystems Technology, Inc.

ODBC Driver for Omnis Data Files. TigerLogic Corporation

Accesssing External Databases From ILE RPG (with help from Java)

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

Simple SQL Queries (3)

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

Making Oracle and JDBC Work For You

Advance DBMS. Structured Query Language (SQL)

Exploiting Java Technology with the SAS Software Barbara Walters, SAS Institute Inc., Cary, NC

Applets, RMI, JDBC Exam Review

NGASI AppServer Manager SaaS/ASP Hosting Automation for Cloud Computing Administrator and User Guide

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

ODBC Chapter,First Edition

Database Access & Making Low-level Libraries Rackety. Ryan Culpepper PLT, University of Utah

ODBC Client Driver Help Kepware, Inc.

SQL: Programming. Introduction to Databases CompSci 316 Fall 2014

HP Neoview ODBC Drivers Manual

Transcription:

ODBC and JDBC

Embedded SQL Database management system program embedded SQL files database storage CMPT 354: Database I -- ODBC and JDBC 2

Embedded SQL is Insufficient Embedded SQL is product sensitive Different database products have different syntax Can we make one program work for all database servers? General tools for database access CMPT 354: Database I -- ODBC and JDBC 3

Connecting to Different Databases Application Embedded SQL Embedded SQL Oracle SQL Server CMPT 354: Database I -- ODBC and JDBC 4

Connecting to Different Databases Application Embedded SQL Oracle library SQL Server library Oracle SQL Server CMPT 354: Database I -- ODBC and JDBC 5

ODBC and JDBC API (application-program interface) for a program to interact with a database server Application makes calls to Connect with the database server Send SQL commands to the database server Fetch tuples of result one-by-one into program variables ODBC (Open Database Connectivity) works with C, C++, C#, and Visual Basic JDBC (Java Database Connectivity) works with Java CMPT 354: Database I -- ODBC and JDBC 6

ODBC Open DataBase Connectivity (ODBC) standard Standard for application program to communicate with a database server Application program interface (API) to Open a connection with a database Send queries and updates Get back results Applications such as GUI, spreadsheets, etc. can use ODBC CMPT 354: Database I -- ODBC and JDBC 7

ODBC (Cont.) Each database system supporting ODBC provides a "driver" library that must be linked with the client program When client program makes an ODBC API call, the code in the library communicates with the server to carry out the requested action and fetch results CMPT 354: Database I -- ODBC and JDBC 8

Using Drivers Application Embedded SQL Oracle library SQL Server library Oracle SQL Server CMPT 354: Database I -- ODBC and JDBC 9

ODBC (Cont) ODBC program first allocates an SQL environment, then a database connection handle Opens database connection using SQLConnect() Parameters for SQLConnect Connection handle The server to which to connect The user identifier Password Must also specify types of arguments SQL_NTS denotes previous argument is a null-terminated string CMPT 354: Database I -- ODBC and JDBC 10

ODBC Code int ODBCexample() { RETCODE error; HENV env; /* environment */ HDBC conn; /* database connection */ SQLAllocEnv(&env); SQLAllocConnect(env, &conn); SQLConnect(conn, "aura.bell-labs.com", SQL_NTS, "avi", SQL_NTS, "avipasswd", SQL_NTS); {. Do actual work } } SQLDisconnect(conn); SQLFreeConnect(conn); SQLFreeEnv(env); CMPT 354: Database I -- ODBC and JDBC 11

ODBC Code (Cont.) Program sends SQL commands to the database by using SQLExecDirect Result tuples are fetched using SQLFetch() SQLBindCol() binds C language variables to attributes of the query result When a tuple is fetched, its attribute values are automatically stored in the corresponding C variables Arguments to SQLBindCol() ODBC statement variable, attribute position in query result The type conversion from SQL to C The address of the variable For variable-length types like character arrays, the maximum length of the variable, the location to store actual length when a tuple is fetched Note: A negative value returned for the length field indicates null value Good programming requires checking results of every function call for errors CMPT 354: Database I -- ODBC and JDBC 12

Main Body of Program char branchname[80]; float balance; int lenout1, lenout2; HSTMT stmt; SQLAllocStmt(conn, &stmt); char * sqlquery = "select branch_name, sum (balance) from account group by branch_name"; error = SQLExecDirect(stmt, sqlquery, SQL_NTS); if (error == SQL_SUCCESS) { SQLBindCol(stmt, 1, SQL_C_CHAR, branchname, 80, &lenout1); SQLBindCol(stmt, 2, SQL_C_FLOAT, &balance, 0, &lenout2); while (SQLFetch(stmt) >= SQL_SUCCESS) { printf (" %s %g\n", branchname, balance); } } SQLFreeStmt(stmt, SQL_DROP); CMPT 354: Database I -- ODBC and JDBC 13

More ODBC Features Prepared Statement SQL statement prepared: compiled at the database Can have placeholders: E.g. insert into account values(?,?,?) Repeatedly executed with actual values for the placeholders Metadata features Finding all the relations in the database and Finding the names and types of columns of a query result or a relation in the database CMPT 354: Database I -- ODBC and JDBC 14

Atomicity By default, each SQL statement is treated as a separate transaction that is committed automatically Can turn off automatic commit on a connection SQLSetConnectOption(conn, SQL_AUTOCOMMIT, 0) Transactions must then be committed or rolled back explicitly by SQLTransact(conn, SQL_COMMIT) or SQLTransact(conn, SQL_ROLLBACK) CMPT 354: Database I -- ODBC and JDBC 15

ODBC Conformance Levels Conformance levels specify subsets of the functionality defined by the standard Core Level 1 requires support for metadata querying Level 2 requires ability to send and retrieve arrays of parameter values and more detailed catalog information. SQL Call Level Interface (CLI) standard similar to ODBC interface, but with some minor differences CMPT 354: Database I -- ODBC and JDBC 16

JDBC JDBC is a Java API for communicating with database systems supporting SQL JDBC supports a variety of features for querying and updating data, and for retrieving query results JDBC also supports metadata retrieval, such as querying about relations present in the database and the names and types of relation attributes CMPT 354: Database I -- ODBC and JDBC 17

Communication Model Open a connection Create a statement object Execute queries using the Statement object to send queries and fetch results Exception mechanism to handle errors CMPT 354: Database I -- ODBC and JDBC 18

JDBC Code public static void JDBCexample(String dbid, String userid, String passwd) { try { Class.forName ("oracle.jdbc.driver.oracledriver"); Connection conn = DriverManager.getConnection( "jdbc:oracle:thin:@aura.bell-labs.com:2000:bankdb", userid, passwd); Statement stmt = conn.createstatement(); Do Actual Work. stmt.close(); conn.close(); } catch (SQLException sqle) { System.out.println("SQLException : " + sqle); } } CMPT 354: Database I -- ODBC and JDBC 19

JDBC Code (Cont.) Update to database try { stmt.executeupdate( "insert into account values ('A-9732', 'Perryridge', 1200)"); } catch (SQLException sqle) { System.out.println("Could not insert tuple. " + sqle); } Execute query and fetch and print results ResultSetrset= stmt.executequery( "select branch_name, avg(balance) from account group by branch_name"); while (rset.next()) { System.out.println( rset.getstring("branch_name") + " " + rset.getfloat(2)); } CMPT 354: Database I -- ODBC and JDBC 20

JDBC Code Details Getting result fields: rs.getstring( branchname ) and rs.getstring(1) equivalent if branchname is the first argument of select result Dealing with Null values int a = rs.getint( a ); if (rs.wasnull()) Systems.out.println( Got null value ); CMPT 354: Database I -- ODBC and JDBC 21

Summary and To-Do List ODBC/JDBC One of the most useful utility for database access Understand how to use ODBC/JDBC in C# to work with SQL Server 2005 Consider the questions in assignment 1 again. If you are allowed to use embedded SQL in C++/Java, how they can be solved? CMPT 354: Database I -- ODBC and JDBC 22