Copyright  Oracle Corporation, 1998. All rights reserved. 7 Accessing a Database Using SQLJ.

Slides:



Advertisements
Similar presentations
16 Copyright © 2005, Oracle. All rights reserved. Using JDBC to Access the Database.
Advertisements

CE203 - Application Programming Autumn 2013CE203 Part 51 Part 5.
Copyright  Oracle Corporation, All rights reserved. 2 Java and Databases: An Overview.
Copyright © 2004 Pearson Education, Inc.. Chapter 9 More SQL: Assertions, Views, and Programming Techniques.
JDBC. Java Database Connectivity (JDBC) Use the java.sql package to query and update the database. JDBC is an API that allows java to communicate with.
Copyright  Oracle Corporation, All rights reserved. 6 Writing Correlated Subqueries.
Embedded SQL John Ortiz. Lecture 15Embedded SQL2 Why Isn’t Interactive SQL Enough?  How to do this using interactive SQL?  Print a well-formatted transcript.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Introduction to SQL Programming Techniques.
Distributed Application Development B. Ramamurthy.
1 JDBC Java Database Connectivity. 2 c.pdf
Java Database Connectivity (JDBC) java.sql package to query and update the database. JDBC is an API that allows java to communicate with a database server.
Java Database Connectivity By: Abe Marji CS616. Agenda 1.Quick Review of Databases 2.What is SQL? 3.What is JDBC? 4.Advanced Functions of JDBC 5.Summary.
Three-Tier Architecture Oracle DB Server Apache Tomcat App Server Microsoft Internet Explorer HTML Tuples HTTP Requests JDBC Requests Java Server Pages.
Copyright  Oracle Corporation, All rights reserved. 4 Creating Functions.
Embedded SQL Host Language (record-oriented) DBMS (set-oriented) 1. Query 3. Process a tuple at a time 4. Close Cursor 2. Evaluate query. Provide cursor.
Advance Computer Programming Java Database Connectivity (JDBC) – In order to connect a Java application to a database, you need to use a JDBC driver. –
1 Java Database Connection (JDBC) There are many industrial-strength DBMS's commercially available in the market. Oracle, DB2, and Sybase are just a few.
Overview of JDBC and Pro*C 1 Overview of JDBC,Pro*C and Oracle connectivity on Omega CSE 5330 – Database Systems.
Helena Pomezná, ciz034 St. skupina: L392 FEI, VŠB-TUO Ak. rok. 2002/2003 Download:
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke; edited K. Shomper1 Database Application Development Chapter 6.
Beginning Databases with JDBC Mike Bradley Adapted from and notes by Kevin Parker, Ph.D.
Announcements Read JDBC Project Step 5, due Monday.
Database Environment Chapter 2 AIT632 Sungchul Hong.
 2004 Keith Vander Linden. All Rights Reserved. Calvin CollegeDepartment of Computer Science(1/25) Database Programming with JDBC Keith Vander Linden.
Lecture 4 PL/SQL language. PL/SQL – procedural SQL Allows combining procedural and SQL code PL/SQL code is compiled, including SQL commands PL/SQL code.
MySQL, Java, and JDBC CSE 3330 Southern Methodist University.
VICTORIA UNIVERSITY OF WELLINGTON Te Whare Wananga o te Upoko o te Ika a Maui COMP 302 Database Systems Java Data Base Connectivity Lecturer Dr Pavle Mogin.
1 Introducing Eclipse DTP for Ingres An Introduction GIUA Juni 2007 J. Peel Ingres Corp.
CS 405G: Introduction to Database Systems Database programming.
Overview of JDBC and Pro*C 1 CSE 5330 – Database Systems.
Copyright  Oracle Corporation, All rights reserved. 1 Oracle Architectural Components.
Object-Oriented Software How does it differ from procedural? How is it similar to procedural? Why has it become so popular? Does it replace or supplement.
Servlets Database Access. Agenda:  Setup Java Environment  Install Database  Install Database Drivers  Create Table and add records  Accessing a.
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.
Copyright  Oracle Corporation, All rights reserved. 4 Accessing a Database Using JBCL.
JDBC  The JDBC (Java Database Connectivity) API helps a Java program to access a database in a standard way  JDBC is a specification that tells the.
JDBC Java and Databases. RHS – SOC 2 JDBC JDBC – Java DataBase Connectivity An API (i.e. a set of classes and methods), for working with databases in.
Accessing Database using JDBC. JDBC Objectives Gain basic knowledge of Java JDBC Become familiar with the basics of interacting with a database using.
Copyright  Oracle Corporation, All rights reserved. 6 Accessing a Database Using the JDBC API.
8 1 Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
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.
Copyright  Oracle Corporation, All rights reserved. 5 More on Creating Database Forms Using AppBuilder.
Copyright © 2002 ProsoftTraining. All rights reserved. Building Database Client Applications Using JDBC 2.0.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 31.1 Reviewing the Bookstore Application 31.2.
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.
1 CS 430 Database Theory Winter 2005 Lecture 14: Additional SQL Topics.
Li Tak Sing COMPS311F. Database programming JDBC (Java Database Connectivity) Java version of ODBC (Open Database Connectivity) ODBC provides a standard.
Dr Gordon Russell, Napier University Unit Embedde SQL - V2.0 1 Embedded SQL Unit 5.1.
16 Java Database Connectivity. 2 Understand the JDBC Understand the steps of the JDBC: 1.) Importing packages 2.) Opening a connection to a database 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.
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
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 I IS Why do we have databases?
5 Copyright © 2004, Oracle. All rights reserved. PL/SQL Server Pages.
CS422 Principles of Database Systems JDBC and Embedded SQL Chengyu Sun California State University, Los Angeles.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe.
6 Copyright © 2009, Oracle. All rights reserved. Using Dynamic SQL.
C Copyright © 2009, Oracle. All rights reserved. Using SQL Developer.
Lec - 14.
Introduction to SQL Programming Techniques
JDBC Database Management Database connectivity
HW#4 Making Simple BBS Using JDBC
Interacting with the Oracle Server
Writing Correlated Subqueries
Objectives In this lesson, you will learn about:
Interacting with Database
Presentation transcript:

Copyright  Oracle Corporation, All rights reserved. 7 Accessing a Database Using SQLJ

Copyright  Oracle Corporation, All rights reserved. 7-2 Objectives After completing this lesson, you should be able to do the following: Describe the design goals of SQLJ Connect to a database using SQLJ Perform DML and DDL operations Use AppBuilder for Java to precompile SQLJ code After completing this lesson, you should be able to do the following: Describe the design goals of SQLJ Connect to a database using SQLJ Perform DML and DDL operations Use AppBuilder for Java to precompile SQLJ code

Copyright  Oracle Corporation, All rights reserved. 7-3 Overview Java file compiled SQL code first parse Development time Run time Java with JDBC SQLJ Java file compiled SQLJ file pre- compiled SQL code first parse SQL JDBC

Copyright  Oracle Corporation, All rights reserved. 7-4 SQLJ is passed through a precompiler: Checks SQL against the database Generates Java code with JDBC calls SQLJ is passed through a precompiler: Checks SQL against the database Generates Java code with JDBC calls Using SQLJ SQLJ code Java code with JDBC calls Java compiler SQLJ preprocessor Oracle Regular Java class

Copyright  Oracle Corporation, All rights reserved. 7-5 Design Goals of SQLJ Allows easy embedding of SQL statements in Java source code – More concise than JDBC Early checking of SQL statements eliminates many run time errors: – SQL syntax errors – Incorrect assumption of table structures – Java/SQL type mismatch Allows easy embedding of SQL statements in Java source code – More concise than JDBC Early checking of SQL statements eliminates many run time errors: – SQL syntax errors – Incorrect assumption of table structures – Java/SQL type mismatch SQL

Copyright  Oracle Corporation, All rights reserved. 7-6 What Does SQLJ Look Like? SQL code is embedded in Java code File typically has a.sqlj extension Equivalent code using JDBC: SQL code is embedded in Java code File typically has a.sqlj extension Equivalent code using JDBC: void myJavaMethod() { #sql { create table EMP(EMPNO number(5) }; } void myJavaMethod() { Statement st = conn.createStatement(); st.execute("create table EMP(EMPNO number(5))"); } myfile.sqlj another.java

Copyright  Oracle Corporation, All rights reserved. 7-7 Imports needed SQLJ statement begins with #sql SQL statement placed in braces - can throw SQLException Imports needed SQLJ statement begins with #sql SQL statement placed in braces - can throw SQLException SQLJ Syntax: A Closer Look import java.sql.*; import sqlj.runtime.*; import sqlj.runtime.ref.*; class X { void myJavaMethod() { try { #sql{update EMP set SAL = SAL where SAL < 1500}; } catch (SQLException e) {…} }

Copyright  Oracle Corporation, All rights reserved. 7-8 Loading the JDBC Driver SQLJ requires that the JDBC driver class is loaded This can be performed in the same way as for JDBC: SQLJ requires that the JDBC driver class is loaded This can be performed in the same way as for JDBC: try { Class.forName("oracle.jdbc.driver.OracleDriver"); } catch (ClassNotFoundException e) { System.out.println("Could not load the driver"); }

Copyright  Oracle Corporation, All rights reserved. 7-9 Specifying a Connection Context All SQLJ statements execute in a “connection context” Defines the database schema, session, and transaction All SQLJ statements execute in a “connection context” Defines the database schema, session, and transaction try { Class.forName(…); DefaultContext.setDefaultContext( new DefaultContext( "theUser", "thePassword") ); } catch (Exception e) {…}

Copyright  Oracle Corporation, All rights reserved Once the JDBC driver has been loaded, and a connection context has been set, SQLJ statements can be executed Executing the SQL Statement try { Class.forName(…); DefaultContext.setDefaultContext(…); #sql { update EMP set SAL = SAL where SAL < 1500 }; } catch (Exception e) {…}

Copyright  Oracle Corporation, All rights reserved Passing Host Variables into a SQLJ Statement A host variable is a variable in your Java program Host variables can be used in a SQLJ statement as follows: A host variable is a variable in your Java program Host variables can be used in a SQLJ statement as follows: void deleteHighEarners(BigDecimal amt) { try { #sql {delete from EMP where SAL >= :amt}; } catch (SQLException e) {…} }

Copyright  Oracle Corporation, All rights reserved Guided Practice: SQLJ and JDBC This example uses JDBC to give a pay raise to selected employees. How would it look in SQLJ? This example uses JDBC to give a pay raise to selected employees. How would it look in SQLJ? BigDecimal amt, low; … Class.forName(…); Connection conn = DriverManager.getConnection(…); PreparedStatement stmt = conn.prepareStatement ("update EMP set SAL = SAL + ? where SAL < ?"); stmt.setBigDecimal(1, amt); stmt.setBigDecimal(2, low); stmt.execute();

Copyright  Oracle Corporation, All rights reserved Assigning Results to Host Variables Host variables can be assignment targets, for values retrieved by SQL operations void printJobTitle(BigDecimal empno) { String job; try { #sql { select JOB into :job from EMP where EMPNO = :empno }; System.out.println("Job title is " + job); } catch (SQLException e) {} }

Copyright  Oracle Corporation, All rights reserved Dealing with Query Result Sets SQLJ can be used to execute queries that return a result set To process the result set, define an “iterator” type – Specifies the data type of each column – Use the iterator to retrieve columns SQLJ can be used to execute queries that return a result set To process the result set, define an “iterator” type – Specifies the data type of each column – Use the iterator to retrieve columns … select ENAME, SAL from EMP …

Copyright  Oracle Corporation, All rights reserved Defining a Named Iterator An iterator type can be defined as shown here, to retrieve columns by name: SQLJ translates this into a Java class called MyIter, with these methods: An iterator type can be defined as shown here, to retrieve columns by name: SQLJ translates this into a Java class called MyIter, with these methods: #sql iterator MyIter(String ENAME, String JOB); String ENAME()… // Get ENAME column String JOB()… // Get JOB column boolean next()… // Go to next row

Copyright  Oracle Corporation, All rights reserved Using a Named Iterator The iterator can be used to extract columns by name, in a type-safe manner #sql iterator MyIter(String ENAME, String JOB); class MyClass { void aMethod() { MyIter iter; #sql iter = { select ENAME, JOB from EMP }; while(iter.next()) { String ename = iter.ENAME(); String job = iter.JOB(); } …

Copyright  Oracle Corporation, All rights reserved Defining By-Position Iterators By-position iterators define the type of each column, but not a name Columns must be fetched by position: By-position iterators define the type of each column, but not a name Columns must be fetched by position: #sql iterator MyIter(String, String); … MyIter iter; String name, job; #sql iter = { select ENAME, JOB from EMP }; while(true) { #sql {fetch :iter into :name, :job}; if (iter.endFetch()) break; System.out.println(name + " is a " + job); …

Copyright  Oracle Corporation, All rights reserved Calling Stored Procedures and Stored Functions A SQLJ statement can call a stored procedure as follows: Stored functions are called as follows: A SQLJ statement can call a stored procedure as follows: Stored functions are called as follows: String ename = …; #sql { call PayBonus(:ename, 100) }; String dname = …; BigDecimal topSal; #sql topSal = { values( GetTopSal(:dname) ) };

Copyright  Oracle Corporation, All rights reserved Specifying a Different Database Connection A different connection context can be specified for SQLJ statements: #sql context MyDBContext; class MyClass { void aMethod() { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); MyDBContext ctxt = new MyDBContext( "jdbc:odbc:MyOtherDb", "user", "password"); #sql (ctxt) { select * from TABLEX }; … ctxt.close();

Copyright  Oracle Corporation, All rights reserved Using SQLJ in AppBuilder.sqlj files can be added directly to an AppBuilder project

Copyright  Oracle Corporation, All rights reserved Enabling Compile-Time SQL Checking Select Project—> Project Properties from Menu bar Check this check box Select Project—> Project Properties from Menu bar Check this check box

Copyright  Oracle Corporation, All rights reserved Specifying Connection Properties

Copyright  Oracle Corporation, All rights reserved Summary SQLJ enables you to embed SQL statements directly in Java code SQLJ statements are compiled statically, whereas JDBC is dynamic SQLJ statements begin with #sql Iterators and contexts can be defined – #sql iterator – #sql context SQLJ enables you to embed SQL statements directly in Java code SQLJ statements are compiled statically, whereas JDBC is dynamic SQLJ statements begin with #sql Iterators and contexts can be defined – #sql iterator – #sql context

Copyright  Oracle Corporation, All rights reserved Practice 7-1 Overview Create and build SQLJ files using AppBuilder tools Connect to a database using SQLJ Create and execute a query using SQJL Iterate a result set Perform an update operation Perform an update operation with parameters Create and build SQLJ files using AppBuilder tools Connect to a database using SQLJ Create and execute a query using SQJL Iterate a result set Perform an update operation Perform an update operation with parameters

Copyright  Oracle Corporation, All rights reserved Full Notes Page for Practices

Copyright  Oracle Corporation, All rights reserved Full Notes Page for Practices