Upload
jana-catherine
View
224
Download
0
Tags:
Embed Size (px)
Citation preview
Copyright Oracle Corporation, 1998. All rights reserved.
22
Java and Databases: An Overview
Java and Databases: An Overview
Copyright Oracle Corporation, 1998. All rights reserved. 2-2-22
ObjectivesObjectives
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
Copyright Oracle Corporation, 1998. All rights reserved. 2-2-33
OverviewOverview
This lesson introduces the technologies involved in accessing databases from a Java program
This lesson introduces the technologies involved in accessing databases from a Java program
DatabaseDatabaseApplet or Applet or applicationapplication
JDBCJDBC
Copyright Oracle Corporation, 1998. All rights reserved. 2-2-44
Accessing Databases in the Non-Java World
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
OracleOracleserverserver
ProgramCalls OCI functions
Copyright Oracle Corporation, 1998. All rights reserved. 2-2-55
Dealing with Different Databases 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 driver Other driver
OracleOracle Other dbOther db
Program 1, calls OCI functions
Program 2, calls other CLI functions
Copyright Oracle Corporation, 1998. All rights reserved. 2-2-66
ODBC: A Standard CLIODBC: A Standard CLI
ODBC provides a standard interface to any databaseODBC provides a standard interface to any database
ODBC driver for Oracle ODBC driver for other db
OracleOracle Other dbOther db
ODBC Driver Manager
Program 1, calls ODBC functions
Program 2, calls ODBC functions
Copyright Oracle Corporation, 1998. All rights reserved. 2-2-77
From ODBC to JDBCFrom 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 Oracle JDBC driver for other db
OracleOracle Other dbOther db
JDBC Driver Manager
Java Program 1, uses JDBC classes/interfaces
Java Program 2, uses JDBC classes/interfaces
Copyright Oracle Corporation, 1998. All rights reserved. 2-2-88
What Is JDBC?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 interfacesExample JDBC interfaces JDBC driver, such as OracleJDBC driver, such as Oracle
Copyright Oracle Corporation, 1998. All rights reserved. 2-2-99
Using JDBCUsing 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
Copyright Oracle Corporation, 1998. All rights reserved. 2-2-1010
Oracle JDBC DriversOracle 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
Copyright Oracle Corporation, 1998. All rights reserved. 2-2-1111
Oracle JDBC Thin DriverOracle 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 driverOracleOracle
Java applet or application
Copyright Oracle Corporation, 1998. All rights reserved. 2-2-1212
Oracle JDBC OCI DriverOracle 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
OracleOracle
Copyright Oracle Corporation, 1998. All rights reserved. 2-2-1313
AppBuilder Support for JDBCAppBuilder 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
Copyright Oracle Corporation, 1998. All rights reserved. 2-2-1414
AppBuilder Data ComponentsAppBuilder 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
Copyright Oracle Corporation, 1998. All rights reserved. 2-2-1515
JBCL Data Components Example
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 neededin this example
This applet connects to an Oracle8 database, and performs a simple query
• Results of query are displayed in a grid
• No code neededin this example
Copyright Oracle Corporation, 1998. All rights reserved. 2-2-1616
Dynamic SQL in JavaDynamic 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");
Copyright Oracle Corporation, 1998. All rights reserved. 2-2-1717
Static SQL in JavaStatic 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”
Copyright Oracle Corporation, 1998. All rights reserved. 2-2-1818
SQL
What Is SQLJ?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
Copyright Oracle Corporation, 1998. All rights reserved. 2-2-1919
SQLJ ExampleSQLJ 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
Copyright Oracle Corporation, 1998. All rights reserved. 2-2-2020
Precompiling SQLJ CodePrecompiling 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 translatorSQLJ translator
JDBC callsJDBC calls
afile.sqlj afile.java
Copyright Oracle Corporation, 1998. All rights reserved. 2-2-2121
SummarySummary
• 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