Access Databases from Java Programs via JDBC Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale

Slides:



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

Basic JDBC Celsina Bignoli What is JDBC Industry standard for database- connectivity between the Java language and a wide range of.
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 Oracle Database Applications Database Connectivity.
UFCE4Y UFCE4Y-20-3 Components and Services Julia Dawson.
JDBC / ODBC JDBC is the java API that facilitate interaction of a java application with the DBMS. FIRST APPROACH:
Dale Roberts 8/24/ Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
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.
CSCI 6962: Server-side Design and Programming JDBC Database Programming.
Java Database Connectivity Vijayan Sugumaran Department of DIS Oakland University.
© Wang Bin 2004 JDBC ----Java Database Connectivity.
Beginning Databases with JDBC Mike Bradley Adapted from and notes by Kevin Parker, Ph.D.
Java Database Connectivity (JDBC) Introduction to JDBC JDBC is a simple API for connecting from Java applications to multiple databases. Lets you smoothly.
Java Database Connectivity ASE. Java Database Connectivity (JDBC) l JDBC – provides an interface to Relational Data Sources l JDBC library provides the.
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 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.
WEB/DB1 DATABASE PROGRAMMING 3JDBC by the ASU Scholars.
JDBC – Java Database Concentricity
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.
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.
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 CHAPTER-2. JDBC - Java Database Connectivity. JDBC from Sun Microsystems provides API or Protocol to interact with different databases. With the.
12/6/2015B.Ramamurthy1 Java Database Connectivity B.Ramamurthy.
Li Tak Sing COMPS311F. Database programming JDBC (Java Database Connectivity) Java version of ODBC (Open Database Connectivity) ODBC provides a standard.
UNIT III - JDBC JDBC Overview – JDBC implementation – Connection class – Statements - Catching Database Results, handling database Queries. Networking–
DATABASE CONNECTIVITY TO MYSQL. Introduction =>A real life application needs to manipulate data stored in a Database. =>A database is a collection of.
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 CS 260 Database Systems. Overview  Introduction  JDBC driver types  Eclipse project setup  Programming with JDBC  Prepared statements  SQL.
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.
Basics of JDBC Session 14.
JDBC (Java Database Connectivity)
CS122B: Projects in Databases and Web Applications Winter 2016
JDBC - Java Database Connectivity. JDBC provides Java applications with access to most database systems via SQL The architecture and API closely resemble.
Database Programming With Java & JDBC Reading: DD Ch. 18, pp al/jdbc/index.html, or anything covering JDBC.
Introduction to JDBC Instructor: Mohamed Eltabakh 1.
1 JDBC – Java Database Connectivity CS , Spring 2010.
6-1 JAVA DATABASE CONNECTOR Colorado Technical University IT420 Tim Peterson.
Java and database. 3 Relational Databases A relational Database consists of a set of simple rectangular tables or relations The column headings are.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe.
Copyright © 1997 Alex Chaffee JDBC and Database Programming in Java Alexander Day Chaffee Version 1.1, 14 Feb 98.
Interacting with Database
JDBC – Java Database Connectivity
How to connect natively?
Part 4 FaaDoOEngineers.com IBM.
Objectives In this lesson, you will learn about:
Interacting with Database
JDBC – Java Database Connectivity
Java Database Connectivity
Java API for Database Connectivity
JDBC Example.
Presentation transcript:

Access Databases from Java Programs via JDBC Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale Room - Faner

Outline – Introduction to JDBC – Connect to a database using Java Database Connectivity (JDBC) – Create and execute a query using JDBC – Process and manipulate the result of a query – Disconnect from the database – Overview of SQLJ 2

JDBC – Java DataBase Connectivity – Industry standard interface for connecting to relational databases from Java – Independent of any DBMS – Allows three things Establish a connection with Relational databases Send SQL statements Process the results – Provides two APIs API for application writers API for driver writers Introduction to JDBC 3

JDBC Architecture ApplicationJDBCDriver Java code calls JDBC library JDBC loads a driver Driver talks to a particular database Can have more than one driver -> more than one database Ideal: can change database engines without changing any application code 4

Is an interpreter that translates JDBC method calls to vendor- specific database commands Implements interfaces in java.sql Can also provide a vendor’s extensions to the JDBC standard Driver JDBC calls Database commands Database A JDBC Driver 5

JDBC Drivers Type I: “Bridge” Type II: “Native” Type III: “Middleware” Type IV: “Pure” 6

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

Overview of Querying a Database With JDBC Query Close Process Results Connect 8

Query Close Connect Process results Register the driver Connect to the database Stage 1: Connect 9

Registering a Driver Statically load driver Class.forName(“Driver Name”); Use the jdbc.drivers system property Every driver has its own name – For example mysql - com.mysql.jdbc.Driver Oracle - oracle.jdbc.driver.OracleDriver 10

Connection to the Database Handled by DriverManager Object Calls getConnection() Method Accepts – JDBC URL, – username – password Returns a Connection object Throws java.sql.SQLException 11

JDBC URLs JDBC uses a URL to identify the database connection jdbc:subprotocol:source Each driver has its own subprotocol Each subprotocol has its own syntax for the source 12

Connection A Connection represents a session with a specific database. Within the context of a Connection, SQL statements are executed and results are returned. Can have multiple connections to a database Also provides “metadata” -- information about the database, tables, and fields Also methods to deal with transactions 13

Obtaining a Connection String url = try { Class.forName ("oracle.jdbc.driver.OracleDriver"); Connection con = DriverManager.getConnection(url); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } 14

Close Connect Query Create a statement Process results Query the database Stage 2: Query 15

Statement A Statement object is used for executing a SQL statement and obtaining the results produced by it. Different types of Statements – Statement – PreparedStatement – CallabaleStatment 16

Statement – Used for executing a static SQL statement – Created by createStatement() method on the connection object – Returns a new Statement object 17

PreparedStatment Creates precompiled SQL statements - more efficient than Statement class Can also allow specifying parameters that can be filled during run time Usually used if the statements is going to execute more than once Created by prepareStatement() method on the connection object – Accepts the query – returns a new PreparedStatement object 18

CallableStatment Many DBMS can store individual or set of SQL statements in a database – Stored Procedures JDBC allows programs to invoke stored procedures using CallableStatment interface A Callable statement object holds parameters for calling stored procedures Created by prepareCall(String sql) – Accepts the query – returns a new CallableStatement object 19

Statement Methods executeQuery(String) – Accepts SQL query – Execute a SQL query that returns a single ResultSet executeUpdate(String) – Execute a SQL INSERT, UPDATE or DELETE statement. Returns the number of rows changed. Execute() – Execute a SQL statement that may return multiple results. 20

Close Query Step through the results Process results Assign results to Java variables Connect Stage 3: Process the Results 21

ResultSet 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. 22

ResultSet Methods boolean next() – activates the next row – the first call to next() activates the first row – returns false if there are no more rows 23

ResultSet Methods Type getType(int columnIndex) – returns the given field as the given type – fields indexed starting at 1 (not 0) Type getType(String columnName) – same, but uses name of field – less efficient int findColumn(String columnName) – looks up column index given column name 24

ResultSet Methods String getString(String columnName) boolean getBoolean(String columnName) byte getByte(String columnName) short getShort(String columnName) int getInt(String columnName) long getLong(String columnName) float getFloat(String columnName) double getDouble(String columnName) Date getDate(String columnName) Time getTime(String columnName) Timestamp getTimestamp(String columnName) 25

Connect Query Process results Close Close the result set Close the statement Close the connection Stage 4: Close 26

Close Close the ResultSet object void close() – disposes of the ResultSet – allows you to re-use the Statement that created it – automatically called by most Statement methods Close the Statement Object void close() Close the connection void close() 27

Step 1: import java.sql Package Step2: Load and register the driver Step 3:Connect to the database Step 4: Create statements Step 5: Execute statements & Process the Result Step 6: close ResultSet and Statement Step 7: Close the connection 28

JDBC Object Classes DriverManager – 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 29

Mapping Java Types to SQL Types SQL type Java Type CHAR, VARCHAR, LONGVARCHARString NUMERIC, DECIMALjava.math.BigDecimal BITboolean TINYINTbyte SMALLINTshort INTEGERint BIGINTlong REALfloat FLOAT, DOUBLEdouble BINARY, VARBINARY, LONGVARBINARYbyte[] DATEjava.sql.Date TIMEjava.sql.Time TIMESTAMPjava.sql.Timestamp 30

SQLJ SQLJ is a set of programming extensions that allow a programmer using the Java programming language to embed statements that provide SQL database requests Specifications for embedding SQL statements in Java programs Alternative to JDBC Syntax – #sql { ….} 31

SQLJ //SQLJ int n; #sql {INSERT INTO emp VALUES(:n)}; //JDBC int n; String query = “INSERT INTO emp VALUES(?)” Statement stmt= con.prepareStatment (query) stmt.setInt(1,n); stmt.execute(); stmt.close(); 32

SQLJ 33

34