Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright  Oracle Corporation, 1998. All rights reserved. 2 Java and Databases: An Overview.

Similar presentations


Presentation on theme: "Copyright  Oracle Corporation, 1998. All rights reserved. 2 Java and Databases: An Overview."— Presentation transcript:

1 Copyright  Oracle Corporation, All rights reserved. 2 Java and Databases: An Overview

2 Copyright  Oracle Corporation, All rights reserved. 2-2 Objectives After completing this lesson, you should be able to do the following: Describe how to use JDBC to access a database from Java Describe how to use JBCL data components to access a database Describe how to use SQLJ to access a database After completing this lesson, you should be able to do the following: Describe how to use JDBC to access a database from Java Describe how to use JBCL data components to access a database Describe how to use SQLJ to access a database

3 Copyright  Oracle Corporation, All rights reserved. 2-3 Overview This lesson introduces the technologies involved in accessing databases from a Java program Database Applet or application JDBC

4 Copyright  Oracle Corporation, All rights reserved. 2-4 Accessing Databases in the Non-Java World Database vendors provide APIs that programs can call to access a database – Known as a Call Level Interface (CLI) – For example, Oracle Call Interface (OCI) Vendor provides a driver to receive CLI calls and forward to database Database vendors provide APIs that programs can call to access a database – Known as a Call Level Interface (CLI) – For example, Oracle Call Interface (OCI) Vendor provides a driver to receive CLI calls and forward to database Oracle OCI driver Oracle server Program Calls OCI functions

5 Copyright  Oracle Corporation, All rights reserved. 2-5 Dealing with Different Databases Different databases have their own CLI to access different databases Need to use different CLIs Need a driver for each CLI Different databases have their own CLI to access different databases Need to use different CLIs Need a driver for each CLI Oracle OCI driverOther driver Oracle Other db Program 1, calls OCI functions Program 2, calls other CLI functions

6 Copyright  Oracle Corporation, All rights reserved. 2-6 ODBC: A Standard CLI ODBC provides a standard interface to any database ODBC driver for OracleODBC driver for other db Oracle Other db ODBC Driver Manager Program 1, calls ODBC functions Program 2, calls ODBC functions

7 Copyright  Oracle Corporation, All rights reserved. 2-7 From ODBC to JDBC JDBC plays a similar role in Java JDBC defines standard interfaces and classes that you can call from Java JDBC plays a similar role in Java JDBC defines standard interfaces and classes that you can call from Java JDBC driver for OracleJDBC driver for other db Oracle Other db JDBC Driver Manager Java Program 1, uses JDBC classes/interfaces Java Program 2, uses JDBC classes/interfaces

8 Copyright  Oracle Corporation, All rights reserved. 2-8 What Is JDBC? JDBC defines standard interfaces Import java.sql package in a Java app Interfaces implemented by JDBC drivers JDBC defines standard interfaces Import java.sql package in a Java app Interfaces implemented by JDBC drivers interface Driver{…} interface Connection{…} interface Statement{…} interface ResultSet{…} class AAA implements Driver{…} class BBB implements Connection{…} etc… Example JDBC interfaces JDBC driver, such as Oracle

9 Copyright  Oracle Corporation, All rights reserved. 2-9 Using JDBC A simplified example: A simplified example: import java.sql.*; public class MyClass { public void MyMethod() { Connection con = DriverManager.getConnection(…); Statement st = con.createStatement(); ResultSet res; res = st.executeQuery("select * from emp"); // … Loop through result set, one row at a time

10 Copyright  Oracle Corporation, All rights reserved Oracle JDBC Drivers Oracle provides two different types of JDBC driver – “JDBC Thin” driver – “JDBC OCI” driver Use one or the other, depending on which type of Java program you are building Oracle provides two different types of JDBC driver – “JDBC Thin” driver – “JDBC OCI” driver Use one or the other, depending on which type of Java program you are building

11 Copyright  Oracle Corporation, All rights reserved Oracle JDBC Thin Driver The Oracle JDBC Thin driver is written in 100% pure Java Can be downloaded over the network, with a Java applet Use this driver for Java applets or applications The Oracle JDBC Thin driver is written in 100% pure Java Can be downloaded over the network, with a Java applet Use this driver for Java applets or applications Oracle JDBC Thin driver Oracle Java applet or application

12 Copyright  Oracle Corporation, All rights reserved Oracle JDBC OCI Driver The Oracle JDBC OCI driver makes OCI calls to a preinstalled “native” driver on the client Use for Java applications only The Oracle JDBC OCI driver makes OCI calls to a preinstalled “native” driver on the client Use for Java applications only oci803jdbc.dll Java application Oracle JDBC OCI driver Oracle

13 Copyright  Oracle Corporation, All rights reserved AppBuilder Support for JDBC AppBuilder for Java includes: – JDBC classes / interfaces in java.sql – Sun JDBC-ODBC Bridge driver The installation also provides: – Oracle Thin drivers for Oracle7 and Oracle8 – Oracle JDBC-OCI drivers for Oracle7 and Oracle8 AppBuilder for Java includes: – JDBC classes / interfaces in java.sql – Sun JDBC-ODBC Bridge driver The installation also provides: – Oracle Thin drivers for Oracle7 and Oracle8 – Oracle JDBC-OCI drivers for Oracle7 and Oracle8

14 Copyright  Oracle Corporation, All rights reserved AppBuilder Data Components The JavaBeans Component Library (JBCL) provides many data components – Some are visual “data-aware” controls – Some are nonvisual “worker” controls Allows a Java program to manipulate a database with the minimum of code – Hides much of the complexity The JavaBeans Component Library (JBCL) provides many data components – Some are visual “data-aware” controls – Some are nonvisual “worker” controls Allows a Java program to manipulate a database with the minimum of code – Hides much of the complexity

15 Copyright  Oracle Corporation, All rights reserved JBCL Data Components Example This applet connects to an Oracle8 database, and performs a simple query Results of query are displayed in a grid No code needed in this example This applet connects to an Oracle8 database, and performs a simple query Results of query are displayed in a grid No code needed in this example

16 Copyright  Oracle Corporation, All rights reserved Dynamic SQL in Java The JBCL data components use JDBC to achieve database connectivity The JDBC calls use “dynamic SQL” – SQL strings are parsed at run time – Not precompiled The JBCL data components use JDBC to achieve database connectivity The JDBC calls use “dynamic SQL” – SQL strings are parsed at run time – Not precompiled Statement stmt; stmt = conn.execute("select * from EMP " + "where SAL > 1000 " + "order by HIREDATE");

17 Copyright  Oracle Corporation, All rights reserved Static SQL in Java Oracle also provides the capability to provide “static SQL” in a Java program – SQL statements are precompiled, and converted to equivalent Java code – Allows SQL statements to be verified against database at compile time – Fewer run time errors This ability is provided by “SQLJ” Oracle also provides the capability to provide “static SQL” in a Java program – SQL statements are precompiled, and converted to equivalent Java code – Allows SQL statements to be verified against database at compile time – Fewer run time errors This ability is provided by “SQLJ”

18 Copyright  Oracle Corporation, All rights reserved SQL What Is SQLJ? SQLJ is a way to embed static SQL in Java programs Looks like standard Java, with a small number of localized extensions Translates embedded SQL into calls to JDBC SQLJ is a way to embed static SQL in Java programs Looks like standard Java, with a small number of localized extensions Translates embedded SQL into calls to JDBC

19 Copyright  Oracle Corporation, All rights reserved SQLJ Example A simplified example of how to use SQLJ Inserts a new row into emp table: A simplified example of how to use SQLJ Inserts a new row into emp table: import java.sql.*; import sqlj.runtime.ref.*; #sql { insert into EMP values('Thomas', 1200) }; afile.sqlj

20 Copyright  Oracle Corporation, All rights reserved Precompiling SQLJ Code SQLJ code must be precompiled to Java, using the SQLJ translator Checks the SQL code at compile time Converts afile.sqlj into afile.java Translates embedded SQL into appropriate calls to JDBC SQLJ code must be precompiled to Java, using the SQLJ translator Checks the SQL code at compile time Converts afile.sqlj into afile.java Translates embedded SQL into appropriate calls to JDBC SQLJ translator JDBC calls afile.sqlj afile.java

21 Copyright  Oracle Corporation, All rights reserved Summary JDBC defines a standard database interface for Java programs Two primary types of Oracle JDBC driver: JDBC Thin, and JDBC OCI AppBuilder for Java provides many data-aware components SQLJ can be used to embed static SQL code in Java programs JDBC defines a standard database interface for Java programs Two primary types of Oracle JDBC driver: JDBC Thin, and JDBC OCI AppBuilder for Java provides many data-aware components SQLJ can be used to embed static SQL code in Java programs

22 Copyright  Oracle Corporation, All rights reserved. 2-22


Download ppt "Copyright  Oracle Corporation, 1998. All rights reserved. 2 Java and Databases: An Overview."

Similar presentations


Ads by Google