Copyright  Oracle Corporation, 1998. All rights reserved. 6 Accessing a Database Using the JDBC API.

Slides:



Advertisements
Similar presentations
5 Copyright © 2005, Oracle. All rights reserved. Accessing the Database with Servlets.
Advertisements

16 Copyright © 2005, Oracle. All rights reserved. Using JDBC to Access the Database.
Copyright  Oracle Corporation, All rights reserved. 2 Java and Databases: An Overview.
1 Lecture 05: Database Programming (JDBC). 2 Outline JDBC overview JDBC API Reading: Chapter 10.5 PostgreSQL JDBC interface documentation
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.
1 CSE5200 JDBC and JDeveloper JDBC java.sql package java.sql classes.
1 JDBC Java Database Connectivity. 2 c.pdf
1 Lecture 05: Database Programming (JDBC). 2 Outline JDBC overview JDBC API Reading: Chapter 10.5 Pointbase Developer Manual.
1 C. Shahabi Application Programming for Relational Databases Cyrus Shahabi Computer Science Department University of Southern California
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.
Three-Tier Architecture Oracle DB Server Apache Tomcat App Server Microsoft Internet Explorer HTML Tuples HTTP Requests JDBC Requests Java Server Pages.
Basic JDBC Use Oracle JDBC Drivers must be in the CLASSPATH
JDBC Java API for Database Connectivity. Layout of this recitation Introduction to JDBC API JDBC Architecture Understanding the design of JDBC API –Classes.
UFCE4Y UFCE4Y-20-3 Components and Services Julia Dawson.
Objectives Describe JDBC Describe JDBC Driver Types
Advance Computer Programming Java Database Connectivity (JDBC) – In order to connect a Java application to a database, you need to use a JDBC driver. –
Database Connectivity ODBC, JDBC and SQLJ CS2312.
1 CSC 440 Database Management Systems JDBC This presentation uses slides and lecture notes available from
Overview of JDBC and Pro*C 1 Overview of JDBC,Pro*C and Oracle connectivity on Omega CSE 5330 – Database Systems.
JDBC. What is JDBC JDBC is an acronym for –Java Data Base Connectivity. It allows java/jsp program to connect to any database.
Beginning Databases with JDBC Mike Bradley Adapted from and notes by Kevin Parker, Ph.D.
JDBC. JDBC Drivers JDBC is an alternative to ODBC and ADO that provides database access to programs written in Java.
Databases: Queries with Java Dr Andy Evans. JDBC SQL Three methods: Statements: Standard, simple, SQL. PreparedStatements: Compiled SQL statements that.
 2004 Keith Vander Linden. All Rights Reserved. Calvin CollegeDepartment of Computer Science(1/25) Database Programming with JDBC Keith Vander Linden.
JDBC Tutorial MIE456 - Information Systems Infrastructure II Vinod Muthusamy November 4, 2004.
JDBC (Java Database Connectivity) SNU OOPSLA Lab. October 2005.
Overview of JDBC and Pro*C 1 CSE 5330 – Database Systems.
Connecting to Oracle using Java November 4, 2009 David Goldschmidt, Ph.D. David Goldschmidt, Ph.D.
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.
Introduction to JDBC Michelle Lee, Ye Wu & Jeff Offutt SWE 432 Design and Implementation of Software for the Web.
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.
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.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
JDBC. Preliminaries Database Database Collection of data Collection of data DBMS DBMS Database management system Database management system Stores and.
JDBC – Java Database Concentricity
Copyright  Oracle Corporation, All rights reserved. 7 Accessing a Database Using SQLJ.
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 © 2002 ProsoftTraining. All rights reserved. Building Database Client Applications Using JDBC 2.0.
Lecture 2 Introduction to JDBC. Introducing JDBC  According to Sun, JDBC is not an acronym, but is commonly misinterpreted to mean Java DataBase Connectivity.
JDBC Database Programming in Java Prepared by., Mrs.S.Amudha AP/SWE.
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.
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.
JDBC™ Fundamentals (a.k.a. Java Database Connectivity, although technically not an acronym) ©SoftMoore ConsultingSlide 1.
Access Databases from Java Programs via JDBC Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale
Basics of JDBC Session 14.
JDBC (Java Database Connectivity)
Umair Javed©2005 Enterprise Application Development Java Database Connectivity (JDBC) JDBC1.
Introduction to JDBC Instructor: Mohamed Eltabakh 1.
1 JDBC – Java Database Connectivity CS , Spring 2010.
Using Oracle JDBC How to Run JDBC on Your Account Communication Mechanism Using Metadata Building a Database Auto Commit v.s Atomic Transaction.
CS422 Principles of Database Systems JDBC and Embedded SQL Chengyu Sun California State University, Los Angeles.
JDBC Statements The JDBC Statement, CallableStatement, and PreparedStatement interfaces define the methods and properties that enables to send SQL or PL/SQL.
JDBC. What is JDBC JDBC is an acronym for –Java Data Base Connectivity. It allows java program to connect to any database.
JDBC.
CS320 Web and Internet Programming Database Access with JDBC Chengyu Sun California State University, Los Angeles.
Topic: JDBC Programming
CS JDBC.
Using JDBC to Access the Database
CS3220 Web and Internet Programming Database Access with JDBC
Java Access to RDB Relational databases (RDBs) dominate today, due to:
1st Semester, 2017 Sanghyun Park
JDBC – Java Database Connectivity
CS320 Web and Internet Programming Database Access with JDBC
HW#4 Making Simple BBS Using JDBC
Design and Implementation of Software for the Web
Introduction to Server-Side Web Development using JSP and Databases
Interacting with Database
CS3220 Web and Internet Programming Database Access with JDBC
CS3220 Web and Internet Programming Database Access with JDBC
Presentation transcript:

Copyright  Oracle Corporation, All rights reserved. 6 Accessing a Database Using the JDBC API

Copyright  Oracle Corporation, All rights reserved. 6-2 Objectives After completing this lesson, you should be able to do the following: Connect to a database using JDBC Perform queries using JDBC Invoke prepared statements and stored procedures Use transaction commit and rollback Use Oracle JDBC extensions After completing this lesson, you should be able to do the following: Connect to a database using JDBC Perform queries using JDBC Invoke prepared statements and stored procedures Use transaction commit and rollback Use Oracle JDBC extensions

Copyright  Oracle Corporation, All rights reserved. 6-3 JDBC class library Overview Javasoft driver ODBC C library Oracle driver OCI C library Oracle driver Java sockets JBCL components Your Java code ODBC-basedOCI-basedPure Java

Copyright  Oracle Corporation, All rights reserved. 6-4 JDBC is a standard Java class library, to query and modify relational data – Import java.sql.* in your Java code JDBC is modeled after ODBC – Supports SQL92 syntax and types – Allows for vendor-specific extensions – Many Oracle extensions JDBC is a standard Java class library, to query and modify relational data – Import java.sql.* in your Java code JDBC is modeled after ODBC – Supports SQL92 syntax and types – Allows for vendor-specific extensions – Many Oracle extensions What Is JDBC?

Copyright  Oracle Corporation, All rights reserved. 6-5 Relationship Between JDBC Classes ResultSet ResultSetMetaData DatabaseMetaData Statement Connection DriverManager

Copyright  Oracle Corporation, All rights reserved. 6-6 Registering a JDBC Driver JDBC drivers must register themselves with the DriverManager Drivers register themselves automatically when they are loaded JDBC drivers must register themselves with the DriverManager Drivers register themselves automatically when they are loaded try { Class c = Class.forName( "oracle.jdbc.driver.OracleDriver"); } catch (ClassNotFoundException e) { e.printStackTrace(); }

Copyright  Oracle Corporation, All rights reserved. 6-7 DriverManager is used to open a connection to a database The database is specified using a URL, which identifies the JDBC driver jdbc: : Example: DriverManager is used to open a connection to a database The database is specified using a URL, which identifies the JDBC driver jdbc: : Example: Connecting to a Database : :

Copyright  Oracle Corporation, All rights reserved. 6-8 Example: Connecting to Oracle The following code connects to an Oracle database Uses the Oracle JDBC Thin driver The following code connects to an Oracle database Uses the Oracle JDBC Thin driver Connection conn; try { conn = DriverManager.getConnection( "theUser", "thePassword"); } catch (SQLException e) {…}

Copyright  Oracle Corporation, All rights reserved. 6-9 Obtaining Database Metadata Connection can be used to get a DatabaseMetaData object This provides many methods to obtain metadata for the database Connection can be used to get a DatabaseMetaData object This provides many methods to obtain metadata for the database Connection conn; … try { DatabaseMetaData dm = conn.getMetaData(); String s1 = dm.getURL(); String s2 = dm.getSQLKeywords(); boolean b1 = dm.supportsTransactions(); boolean b2 = dm.supportsSelectForUpdate(); } catch (SQLException e) {…}

Copyright  Oracle Corporation, All rights reserved Three interfaces are defined, providing the following capabilities: Execute queries and other DML/DDL operations Execute precompiled statements Call stored procedures Three interfaces are defined, providing the following capabilities: Execute queries and other DML/DDL operations Execute precompiled statements Call stored procedures Statements Statement PreparedStatement CallableStatement

Copyright  Oracle Corporation, All rights reserved Statements and Queries The Statement class can be used as follows, to execute a query executeQuery() executes a SQL query, and returns a ResultSet The Statement class can be used as follows, to execute a query executeQuery() executes a SQL query, and returns a ResultSet try { Statement stmt = conn.createStatement(); ResultSet rset = stmt.executeQuery ("select ENAME, SAL from EMP"); } catch (SQLException e) {…}

Copyright  Oracle Corporation, All rights reserved Processing Query Results ResultSet holds a table of result data returned by a SQL query Support for cursors – Cursor starts at beginning of data set – Use next() to move to next record Retrieve data using getXXX() methods, mapping results to equivalent Java types ResultSet holds a table of result data returned by a SQL query Support for cursors – Cursor starts at beginning of data set – Use next() to move to next record Retrieve data using getXXX() methods, mapping results to equivalent Java types

Copyright  Oracle Corporation, All rights reserved Processing Results: Example try { ResultSet rset = stmt.executeQuery( "select ENAME, SAL from EMP"); while (rset.next()) { String ename = rset.getString(1); BigDecimal sal = rset.getBigDecimal(2, 2); // Can also access columns by name: // String ename = rset.getString("ENAME"); // BigDecimal sal = rset.getBigDecimal("SAL"); } catch (SQLException e) {…}

Copyright  Oracle Corporation, All rights reserved Obtaining ResultSet Metadata ResultSet can be used to get a ResultSetMetaData object Provides result set metadata ResultSet can be used to get a ResultSetMetaData object Provides result set metadata try { ResultSet rset = … ; ResultSetMetaData md = rset.getMetaData(); while (rset.next()) { for (int i = 0; i < md.getColumnCount(); i++) { String lbl = md.getColumnLabel(); String typ = md.getColumnTypeName(); … } } catch (SQLException e) {…}

Copyright  Oracle Corporation, All rights reserved Mapping Database Types to Java Types ResultSet maps database types to Java types ResultSet rset = stmt.executeQuery ("select EMPNO, ENAME, HIREDATE from EMP"); BigDecimal empno = rset.getBigDecimal(1, 2); String ename = rset.getString(2); Date hiredate = rset.getDate(3); Col Name EMPNO ENAME HIREDATE Type NUMBER VARCHAR2 DATE

Copyright  Oracle Corporation, All rights reserved Prepared Statements If you need to execute a statement several times, with different bind variables: Use a PreparedStatement object Identify bind variables with a ? sign If you need to execute a statement several times, with different bind variables: Use a PreparedStatement object Identify bind variables with a ? sign try { Connection conn = DriverManager.getConnection(…); PreparedStatement pstmt = conn.prepareStatement("update EMP set SAL = ?"); … } catch (SQLException e) {…}

Copyright  Oracle Corporation, All rights reserved Binding Variables and Executing a PreparedStatement Specify bind variables using the methods setXXX() in PreparedStatement try { PreparedStatement pstmt = conn.prepareStatement("update EMP set SAL = ?"); … pstmt.setBigDecimal(1, new BigDecimal(55000)); pstmt.executeUpdate(); pstmt.setBigDecimal(1, new BigDecimal(65000)); pstmt.executeUpdate(); … } catch (SQLException e) {…}

Copyright  Oracle Corporation, All rights reserved Stored Procedures To call a stored procedure, you need a CallableStatement object IN parameters are set using setXXX(), as for a PreparedStatement Return values and OUT parameters must be registered, to specify their types To call a stored procedure, you need a CallableStatement object IN parameters are set using setXXX(), as for a PreparedStatement Return values and OUT parameters must be registered, to specify their types

Copyright  Oracle Corporation, All rights reserved Calling Stored Procedures getSal(v_ename in varchar2, v_job out varchar2) return numeric … CallableStatement cs = conn.prepareCall( "{? = call getSal(?,?)}" ); cs.registerOutParameter(1, Types.NUMERIC); cs.setString(2, "smith"); cs.registerOutParameter(3, Types.VARCHAR); cs.executeUpdate(); System.out.println("Smith makes " + cs.getFloat(1) + " as a " + cs.getString(3));

Copyright  Oracle Corporation, All rights reserved Transactions Transactions are governed by the autoCommit property in Connection – true initially, causing a separate transaction per SQL statement If you want to take control: Transactions are governed by the autoCommit property in Connection – true initially, causing a separate transaction per SQL statement If you want to take control: Connection conn = DriverManager.getConnection(…); conn.setAutoCommit(false); // No autocommits now … // Issue SQL statements conn.commit(); … or … // Commit transaction conn.rollback(); // Rollback transaction

Copyright  Oracle Corporation, All rights reserved PreparedStatement CallableStatement Oracle Extensions Oracle provides many extensions to standard JDBC, for example: Connection Statement ResultSet OraclePreparedStatement OracleCallableStatement OracleStatement OracleResultSet OracleConnection

Copyright  Oracle Corporation, All rights reserved Prefetching Rows in a Query Oracle JDBC drivers enable you to set the number of rows to prefetch to the client – Can be defined for the Connection – Alternatively, define for individual Statements Reduces the number of round trips to the server Oracle JDBC drivers enable you to set the number of rows to prefetch to the client – Can be defined for the Connection – Alternatively, define for individual Statements Reduces the number of round trips to the server

Copyright  Oracle Corporation, All rights reserved Prefetching Rows: Example Connection conn = DriverManager.getConnection(…); ((OracleConnection)conn).setDefaultRowPrefetch(7); Statement st = conn.createStatement(); ResultSet rs = st.executeQuery("select * from EMP"); while (rs.next()) System.out.println(rs.getString(1)); ((OracleStatement)st).setRowPrefetch(2); rs = st.executeQuery("select * from EMP"); while (rs.next()) System.out.println(rs.getString(1)); Connection conn = DriverManager.getConnection(…); ((OracleConnection)conn).setDefaultRowPrefetch(7); Statement st = conn.createStatement(); ResultSet rs = st.executeQuery("select * from EMP"); while (rs.next()) System.out.println(rs.getString(1)); ((OracleStatement)st).setRowPrefetch(2); rs = st.executeQuery("select * from EMP"); while (rs.next()) System.out.println(rs.getString(1));

Copyright  Oracle Corporation, All rights reserved Prespecifying Column Types When a query is performed, JDBC uses a network round trip to identify result types Oracle JDBC enables column types to be predefined – Eliminates one network round trip When a query is performed, JDBC uses a network round trip to identify result types Oracle JDBC enables column types to be predefined – Eliminates one network round trip ((OracleStatement)st).clearDefines(); ((OracleStatement)st).defineColumnType(col, type);

Copyright  Oracle Corporation, All rights reserved Prespecifying Types: Example Statement st = conn.createStatement(); // Ask for the EMPNO column as a String. // This avoids a round trip to get the column type. // EMPNO is converted to a string on the server. ((OracleStatement)st).defineColumnType (1, Types.VARCHAR); ResultSet rs = st.executeQuery ("select EMPNO from EMP"); while (rs.next()) System.out.println (rs.getString(1));

Copyright  Oracle Corporation, All rights reserved Batched Updates of Prepared Statements By default, prepared statements are executed as soon as executeUpdate() is called Oracle JDBC enables prepared statements to be batched: By default, prepared statements are executed as soon as executeUpdate() is called Oracle JDBC enables prepared statements to be batched: ((OraclePreparedStatement)ps).setExecuteBatch(3); ((OraclePreparedStatement)ps).executeBatch(); ((OraclePreparedStatement)ps).getExecuteBatch();

Copyright  Oracle Corporation, All rights reserved Guided Practice: When Are the Statements Executed? OraclePreparedStatement methods setExecuteBatch() and sendBatch() OraclePreparedStatement methods setExecuteBatch() and sendBatch() PreparedStatement ps = conn.prepareStatement ("update EMP set SAL = SAL + 100"); ((OraclePreparedStatement)ps).setExecuteBatch(3); ps.executeUpdate(); ((OraclePreparedStatement)ps).sendBatch()

Copyright  Oracle Corporation, All rights reserved The ROWID Pseudo Column Oracle JDBC drivers do not support getCursorName() for cursor usage Oracle provides a ROWID type instead – ROWID is a pseudo column in a query – Retrieve its value from a ResultSet, using getString() ROWID can also be bound to a parameter in a PreparedStatement Oracle JDBC drivers do not support getCursorName() for cursor usage Oracle provides a ROWID type instead – ROWID is a pseudo column in a query – Retrieve its value from a ResultSet, using getString() ROWID can also be bound to a parameter in a PreparedStatement

Copyright  Oracle Corporation, All rights reserved Example: Using ROWID for In-Place Updates ResultSet rset = stmt.executeQuery ("select ENAME, ROWID from EMP for update"); PreparedStatement pstmt = conn.prepareStatement ("update EMP set ENAME = ? where ROWID = ?"); while (rset.next()) { String ename = rset.getString(1); String rowid = rset.getString(2); pstmt.setString(1, ename.toLowerCase()); pstmt.setString(2, rowid); pstmt.executeUpdate(); }

Copyright  Oracle Corporation, All rights reserved Oracle Extensions for Stored Subprograms Stored subprograms in standard JDBC: Oracle JDBC supports Oracle escape syntax, and PL/SQL anonymous blocks: Stored subprograms in standard JDBC: Oracle JDBC supports Oracle escape syntax, and PL/SQL anonymous blocks: CallableStatement cs = conn.prepareCall ( "{? = call func(?,?)}" ); CallableStatement cs2 = conn.prepareCall ("begin :1 = func(:2,:3); end;");

Copyright  Oracle Corporation, All rights reserved Oracle CURSOR Type Oracle provides a CURSOR type, to iterate a result set returned by a stored procedure CallableStatement cs = conn.prepareCall ("begin open ? for select ENAME from EMP; end;"); cs.registerOutParameter(1, OracleTypes.CURSOR); cs.executeUpdate(); ResultSet cursor = ((OracleCallableStatement)cs).getCursor(1); while (cursor.next()) System.out.println(cursor.getString(1));

Copyright  Oracle Corporation, All rights reserved Suppressing Table Remarks DatabaseMetaData TABLE_REMARKS should be avoided Require slow outer joins To enable or disable table remarks: DatabaseMetaData TABLE_REMARKS should be avoided Require slow outer joins To enable or disable table remarks: ((OracleConnection)conn).setRemarksReporting(bool);

Copyright  Oracle Corporation, All rights reserved Summary JDBC provides classes and interfaces for database connectivity – Connect to a database – Perform DML and DDL operations – Prepared statements and stored procedures Oracle JDBC provides many extensions – Performance and flexibility JDBC provides classes and interfaces for database connectivity – Connect to a database – Perform DML and DDL operations – Prepared statements and stored procedures Oracle JDBC provides many extensions – Performance and flexibility

Copyright  Oracle Corporation, All rights reserved Practice 6-1 Overview Connect to a database, using JDBC Create and execute a query, using JDBC Iterate a result set Perform an update operation Perform an update operation with parameters Connect to a database, using JDBC Create and execute a query, using JDBC 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

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

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