JDBC – Java Database Concentricity

Slides:



Advertisements
Similar presentations
CE203 - Application Programming Autumn 2013CE203 Part 51 Part 5.
Advertisements

JDBC - Java Database Connectivity The objectives of this chapter are: To describe the architecture of JDBC To outline the classes in the java.sql package.
Distributed Application Development B. Ramamurthy.
1 JDBC Java Database Connectivity. 2 c.pdf
1 JDBC: Java Database Connectivity. 2 Introduction to JDBC JDBC is used for accessing databases from Java applications Information is transferred from.
מסדי נתונים תשס " ג 1 JDBC Java Database Connectivity קורס מסדי נתונים.
1 C. Shahabi Application Programming for Relational Databases Cyrus Shahabi Computer Science Department University of Southern California
1 Lecture 29 More on JDBC Overview  Objectives of this lecture  JDBC and its Drivers  Connecting to Databases (Java’s Connection class)  Querying a.
1 JDBC "Java Database Connectivity". 2 Getting Started Guide: etstart/GettingStartedTOC.fm.html java.sql.
1 Oracle Database Applications Database Connectivity.
JDBC / ODBC JDBC is the java API that facilitate interaction of a java application with the DBMS. FIRST APPROACH:
Advance Computer Programming Java Database Connectivity (JDBC) – In order to connect a Java application to a database, you need to use a JDBC driver. –
Getting connected.  Java application calls the JDBC library.  JDBC loads a driver which talks to the database.  We can change database engines without.
Accessing Databases with JDBC. Introduction to JDBC JDBC provides a standard library for accessing databases by using the JDBC API. JDBC standardizes.
Java Database Connectivity Vijayan Sugumaran Department of DIS Oakland University.
© Wang Bin 2004 JDBC ----Java Database Connectivity.
Java Database Connectivity (JDBC) Introduction to JDBC JDBC is a simple API for connecting from Java applications to multiple databases. Lets you smoothly.
Database Programming in Java Corresponds with Chapter 32, 33.
JDBC and Database Programming in Java. Introduction u Database Access in Java u Find out any relevant background and interest of the audience u SQL gurus?
Topic : JDBC Kaster Nurmukan. Database Access in Java Find out any relevant background and interest of the audience –SQL gurus? –Visual Basic Database.
Dr R R DOCSIT, Dr BAMU. Basic Java : Introduction to JDBC 2 Objectives of This Session State what is Java Database Connectivity State different.
JDBC (Java Database Connectivity) SNU OOPSLA Lab. October 2005.
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.
CONTROLPANEL Java.sql package This package provides the APIs for accessing and processing data which is stored in the database especially relational.
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.
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.
1 JDBC – Java Database Connectivity. 2 Introduction to JDBC JDBC is used for accessing databases from Java applications Information is transferred from.
1 JDBC Aum Amriteshwaryai Namah. 2 2 JDBC – Java DataBase Connectivity.
JDBC. Preliminaries Database Database Collection of data Collection of data DBMS DBMS Database management system Database management system Stores and.
JDBC Enterprise Systems Programming. JDBC  Java Database Connectivity  Database Access Interface provides access to a relational database (by allowing.
Accessing Database using JDBC. JDBC Objectives Gain basic knowledge of Java JDBC Become familiar with the basics of interacting with a database using.
COMP201 Java Programming Topic 15: Database Connectivity JDBC Reading: Chapter 4, Volume 2.
WEB/DB1 DATABASE PROGRAMMING 3JDBC by the ASU Scholars.
Copyright  Oracle Corporation, All rights reserved. 6 Accessing a Database Using the JDBC API.
Java Database Connectivity (JDBC). Topics 1. The Vendor Variation Problem 2. SQL and Versions of JDBC 3. Creating an ODBC Data Source 4. Simple Database.
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.
Chapter 17 Accessing Databases with JDBC. JDBC JDBC provides a standard library for accessing relational databases. By using the JDBC API, you can access.
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.
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.
Database Access Using JDBC BCIS 3680 Enterprise Programming.
Access Databases from Java Programs via JDBC Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale
JDBC Part II CS 124. More about JDBC Types Statement versus PreparedStatement Timeout NULL values Meta-data close() methods Exceptions Transactions JDBC.
Basics of JDBC.
Basics of JDBC Session 14.
JDBC (Java Database Connectivity)
CS122B: Projects in Databases and Web Applications Winter 2016
Ch. NoNameMarks 01AWT24 02Networking18 03JDBC20 04Swing18 05Servlet20 Advance Java Programming.
JDBC and Database Programming in Java Seree Chinodom
1 JDBC: Java Database Connectivity. 2 Introduction to JDBC JDBC is used for accessing databases from Java applications Information is transferred from.
1 JDBC – Java Database Connectivity CS , Spring 2010.
6-1 JAVA DATABASE CONNECTOR Colorado Technical University IT420 Tim Peterson.
Using Oracle JDBC How to Run JDBC on Your Account Communication Mechanism Using Metadata Building a Database Auto Commit v.s Atomic Transaction.
Chapter 7 Chapter 7 Java Database Connectivity using JSP 1 (IS 203) WebProgramming (IS 203) Web Programming.
Java and database. 3 Relational Databases A relational Database consists of a set of simple rectangular tables or relations The column headings are.
JDBC.
1 JDBC – Java Database Connectivity THETOPPERSWAY.COM.
Copyright © 1997 Alex Chaffee JDBC and Database Programming in Java Alexander Day Chaffee Version 1.1, 14 Feb 98.
CS3220 Web and Internet Programming Database Access with JDBC
Interacting with Database
JDBC – Java Database Connectivity
Web Technologies IT230 Dr Mohamed Habib.
JDBC – Java Database Connectivity
Objectives In this lesson, you will learn about:
Interacting with Database
CS3220 Web and Internet Programming Database Access with JDBC
CS3220 Web and Internet Programming Database Access with JDBC
Presentation transcript:

JDBC – Java Database Concentricity Representation and Management of Data on the Internet

Relational Databases Data stored in records (tuples) which live in tables (relations) Maps row (record) to column (field) in a single table A B C D 1 2 3 4 4 3 2 1

A (very short) Reminder of SQL Creating tables: CREATE TABLE books ( isbn INTEGER primary key, title VARCHAR2(40) not null, abstract TEXT);

A (very short) Reminder of SQL Inserting tuples into the tables INSERT INTO books(isbn, title) VALUES (12345, “Foundations of Database”)

A (very short) Reminder of SQL Updating tuples in the tables UPDATE books SET title=“Foundation of Databases” WHERE isbn=12345;

A (very short) Reminder of SQL Deleting tuples from the tables DELETE FROM books WHERE title like ‘%goblin%;

A (very short) Reminder of SQL Queries SELECT title FROM books WHERE abstract like ‘%hobbit%;

Transactions Transaction = more than one statement which must all succeed (or all fail) together If one fails, the system must reverse all previous actions Also can’t leave DB in inconsistent state halfway through a transaction COMMIT = complete transaction ROLLBACK = abort

Working With Oracle Add to your .cshrc the following: if ($HOST == sol4) then setenv ORACLE_HOME /opt/oracle else setenv ORACLE_HOME /usr/local/oracle8i endif setenv PATH $ORACLE_HOME/bin:$PATH setenv ORACLE_SID stud

sqlplus snoopy/snoopy@stud.cs Calling Oracle If a student whose login is Snoopy wants to work directly with Oracle: sqlplus snoopy/snoopy@stud.cs Note: we use the login for a password!

Introduction to JDBC JDBC is used for accessing databases from Java applications Information is transferred from relations to objects and vice-versa databases optimized for searching/indexing objects optimized for engineering/flexibility

Overview RMI JDBC CORBA java.net TCP/IP Network OS

JDBC Drivers (Fig.) JDBC Type I “Bridge” ODBC ODBC Driver Type II “Native” CLI (.lib) Type III “Middleware” Middleware Server Type IV “Pure”

Java Package JDBC is implemented in Java in the package java.sql

Seven Steps Load the driver Define the Connection URL Establish the Connection Create a Statement object Execute a query Process the result Close the connection

Important Classes DriverManager Driver Connection Statement ResultSet loads, chooses drivers Driver connects to actual database Connection a series of SQL statements to and from the DB Statement a single SQL statement ResultSet the records returned from a Statement

DriverManager Driver Connection Statement ResultSet

Loading the Driver Registering the Driver directly automatically: Class.forName( “oracle.jdbc.driver.OracleDriver"); Calling Class.forName, automatically creates an instance of the driver registers the driver with the DriverManager

Another Option Another option is to create an instance of the driver and register it with the Driver Manager: Driver driver = new oracle.jdbc.OracleDriver(); DriverManager.registerDriver(driver);

The DriverManager The DriverManager tries all the drivers Uses the first one that works When a driver class is first loaded, it registers itself with the DriverManager Therefore, to load a driver, just register it!

Creating a Connection Use getConnection on the Driver Connection getConnection (String url, String user, String password) Connects to given JDBC URL with given user name and password Throws java.sql.SQLException returns a Connection object

URLS Gives the required information for making the connection to the database For example, Snoopy will use the URL jdbc:oracle:thin:snoopy/snoopy@sol4:1521:stud Path to driver User name Password Port Host Database name

Connection A Connection represents a session with a specific database Within the context of a Connection, SQL statements are executed and results are returned

Connections There can be multiple connections to a database A connection provides “metadata”, i.e., information about the database, tables, and fields Connection object has methods to deal with transactions

Creating a Connection String url = "jdbc:oracle:thin:snoopyo/snoopy@sol4:1521:stud"; try { Class.forName (“oracle.jdbc.OracleDriver"); Connection con = DriverManager.getConnection(url); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e)

Statements Why all these different kinds of statements? Statement createStatement() returns a new Statement object PreparedStatement prepareStatement(String sql) returns a new PreparedStatement object CallableStatement prepareCall(String sql) returns a new CallableStatement object Why all these different kinds of statements?

Statements A Statement object is used for executing a static SQL statement and obtaining the results produced by it executeQuery is used for statements that return an output result executeUpdate is used for statements that need not return an output

Executing Queries and Updates ResultSet executeQuery(String) Execute a SQL statement that returns a single ResultSet int executeUpdate(String) Execute a SQL INSERT, UPDATE or DELETE statement Used for CREATE TABLE, DROP TABLE and ALTER TABLE Returns the number of rows changed

Timeout Use setQueryTimeOut to set a timeout for the driver to wait for a statement to be completed If the operation is not completed in the given time, an SQLException is thrown What is it good for?

Cursor What is the result of a query? Is a remote connection different from a direct access to the database? How can a database send the result of a query through communication lines? The answer: using a cursor

Result Set A ResultSet provides access to a table of data generated by executing a Statement Only one ResultSet per Statement can be open at once The table rows are retrieved in sequence A ResultSet maintains a cursor pointing to its current row of data The 'next' method moves the cursor to the next row you can’t rewind

Working with ResultSet boolean next() activates the next row the first call to next() activates the first row returns false if there are no more rows void close() disposes of the ResultSet allows you to re-use the Statement that created it automatically called by most Statement methods

Getting Values from Rows Type getString(int columnIndex) returns the given field as the given type fields indexed starting at 1 (not 0) Type getString(String columnName) same, but uses name of field less efficient int findColumn(String columnName) looks up column index given column name getType

Example Code Example

Optimized Statements Prepared Statements Stored Procedures SQL calls that you make again and again allows driver to optimize (compile) queries created with Connection.prepareStatement() Stored Procedures written in DB-specific language stored inside database accesed with Connection.prepareCall()

Metadata Connection: ResultSet: DatabaseMetaData getMetaData() ResultSetMetaData getMetaData()

Useful Methods of Metadata getColumnCount getColumnDisplaySize getColumnName getColumnType isNullabale Imagine the case where you want to print the result

Transaction Management Transactions are not explicitly opened and closed Instead, the connection has a state called AutoCommit mode if AutoCommit is true, then every statement is automatically committed default case: true

AutoCommit Connection.setAutoCommit(boolean) if AutoCommit is false, then every statement is added to an ongoing transaction you must explicitly commit or rollback the transaction using Connection.commit() and Connection.rollback()

Connection Manager For a large threaded database server, create a Connection Manager object It is responsible for maintaining a certain number of open connections to the database When your applications need a connection, they ask for one from the CM’s pool Why? Because opening and closing connections takes a long time Warning: the CM should always setAutoCommit(false) when a connection is returned