1 Lecture 05: Database Programming (JDBC). 2 Outline JDBC overview JDBC API Reading: Chapter 10.5 Pointbase Developer Manual.

Slides:



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

Basic JDBC Celsina Bignoli What is JDBC Industry standard for database- connectivity between the Java language and a wide range of.
Database programming in Java An introduction to Java Database Connectivity (JDBC)
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.
15-Jun-15 JDBC. JDBC is a Sun trademark It is often taken to stand for Java Database Connectivity Java is very standardized, but there are many versions.
1 JDBC Java Database Connectivity. 2 c.pdf
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.
1 Lecture 29 More on JDBC Overview  Objectives of this lecture  JDBC and its Drivers  Connecting to Databases (Java’s Connection class)  Querying a.
Three-Tier Architecture Oracle DB Server Apache Tomcat App Server Microsoft Internet Explorer HTML Tuples HTTP Requests JDBC Requests Java Server Pages.
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.
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.
1 CSC 440 Database Management Systems JDBC This presentation uses slides and lecture notes available from
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke; edited K. Shomper1 Database Application Development Chapter 6.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 8 Object Oriented Programming in Java Advanced Topics Java Database.
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 ASE. Java Database Connectivity (JDBC) l JDBC – provides an interface to Relational Data Sources l JDBC library provides the.
 2004 Keith Vander Linden. All Rights Reserved. Calvin CollegeDepartment of Computer Science(1/25) Database Programming with JDBC Keith Vander Linden.
MySQL, Java, and JDBC CSE 3330 Southern Methodist University.
JDBC (Java Database Connectivity) SNU OOPSLA Lab. October 2005.
JAVA Database Access. JDBC The Java Database Connectivity (JDBC) API is the industry standard for database- independent connectivity between the Java.
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.
CS 405G: Introduction to Database Systems Database programming.
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 Enterprise Systems Programming. JDBC  Java Database Connectivity  Database Access Interface provides access to a relational database (by allowing.
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.
Chapter 25 Databases. Chapter Scope Database concepts Tables and queries SQL statements Managing data in a database Java Foundations, 3rd Edition, Lewis/DePasquale/Chase25.
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.
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 CS 124. JDBC Java Database Connectivity Database Access Interface provides access to a relational database (by allowing SQL statements to be sent.
Session 30 Basics of JDBC. Java Simplified / Session 30 / 2 of 33 Review A Swing menu consists of a menubar, menuitems and menus. Trees are used to depict.
Java and Databases. JDBC Architecture Java Application JDBC API Data Base Drivers AccessSQL Server DB2InformixMySQLSybase.
COMP 321 Week 4. Overview Normalization Entity-Relationship Diagrams SQL JDBC/JDBC Drivers hsqldb Lab 4-1 Introduction.
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.
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.
Access Databases from Java Programs via JDBC Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale
JDBC (Java Database Connectivity)
Database Management Systems 1 Raghu Ramakrishnan Database Application Development Chpt 6 Xin Zhang.
JAVA DATABASE OOP Praktek dengan Java Miswar,S.st Sumber : Eddy Muntina Dharma,ST,MT.
Database Programming With Java & JDBC Reading: DD Ch. 18, pp al/jdbc/index.html, or anything covering JDBC.
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.
Java and database. 3 Relational Databases A relational Database consists of a set of simple rectangular tables or relations The column headings are.
JDBC. What is JDBC JDBC is an acronym for –Java Data Base Connectivity. It allows java program to connect to any database.
Instructor: Jinze Liu Fall /8/2016Jinze University of Kentucky 2 Database Project Database Architecture Database programming.
Interacting with Database
1st Semester, 2017 Sanghyun Park
Lec - 14.
JDBC 15-Apr-18.
JDBC – Java Database Connectivity
JDBC 21-Aug-18.
HW#4 Making Simple BBS Using JDBC
Prof: Dr. Shu-Ching Chen TA: Sheng Guan
JDBC 15-Nov-18.
Objectives In this lesson, you will learn about:
Interacting with Database
Bolat Azamat, Kim Dongmin
JDBC Example.
Presentation transcript:

1 Lecture 05: Database Programming (JDBC)

2 Outline JDBC overview JDBC API Reading: Chapter 10.5 Pointbase Developer Manual

3 Embedded SQL Direct SQL (= ad-hoc SQL) is rarely used In practice: SQL is embedded in some application code –user interaction, devices, programming logic SQL code is enbedded using special syntax into a host language

4 JDBC (Java DB Connectivity) Java application {... "SELECT... FROM... WHERE"... } DBMS

5 JDBC Drivers Java application JDBC- Driver manager Native Protocol driver JDBC- Net-driver Native API-driver JDBC-ODBC bridge Client library DB- Middleware ODBC Client library JDBC-API

6 Running a JDBC Application PhaseTaskRelevant java.sql classes Initialisation Processing Termination Load driver Create connection Generate SQL statements Process result data Terminate connection Release data structures DriverManager Connection Statement ResultSet etc. Connection Statement etc.

7 A Simple JDBC application loadDriver getConnection createStatement execute(SQL) Result handling Last execution ? closeStatment closeConnection no yes InitialContext ic = new InitialContext(); DataSource ds = (DataSource) ic.lookup("java:comp/env/jdbc/aberer"; ); out.println("db found"); connection = ds.getConnection(); out.println("connection established"); Statement stmt = con.Statement ( „SELECT * FROM data WHERE date = ' ' “); ResultSet rset = stmt.executeQuery(); while (rset.next()) {... } stmt.close(); con.close();

8 Loading of Driver Creates an instance of the driver Registers driver in the driver manager Explicit loading String l_driver = "com.pointbase.jdbc.jdbcUniversalDriver"; Class.forName(l_driver).newInstance(); Several drivers can be loaded and registered

9 Addressing Database A connection is a session with one database Databases are addressed using a URL of the form "jdbc: : " Examples "jdbc:pointbase:embedded:sample" "jdbc:pointbase:server:// /sample"

10 Connecting to Database Connection is established conn = DriverManager.getConnection(URL,USERID,PWD); Connection is closed con.close();

11 Implicit Driver Loading Create DataSource object jdbcDataSource ds = new jdbcDataSource(); ds.setDatabaseName(l_URL); ds.setUser(l_UID); ds.setPassword(l_PWD); ds.setCreateDatabase(true); Establish connection with the database and return a Connection object conn = ds.getConnection();

12 Simple SQL Statements Statement object for invocation stmt = conn.createStatement(); ResultSet rset= stmt.executeQuery( "SELECT address,script,type FROM worklist"); ResultSet object for result processing

13 Impedance Mismatch Example: SQL in Java: –Java uses int, char[..], objects, etc –SQL uses tables Impedance mismatch = incompatible types Why not use only one language? –SQL cannot do everything that the host language can do Solution: use cursors

14 Using Cursors Access to tuples –ResultSet object manages a cursor for tuple access –Example Statement stmt=con.createStatement(); ResultSet rset=stmt.executeQuery (“SELECT …”); while (rset.next()) { … } rset.close(); c1c2 c3c4

15 Accessing Attributes (Columns) Access to columns of a tuple –Using column index or column name –Example while (rset.next()) { String address = rset.getString(1); String type = rset.getString(“type”); String script = rset.getString( rset.findColumn(“script”));... } c1c2 c3c4

16 More on Cursors Cursors can also modify a relation rset.updateString("script", "ebay"); The cursor can be a scrolling one: can go forward, backward first(), last(), next(), previous() We can determine the order in which the cursor will get tuples by the ORDER BY clause in the SQL query

17 Dynamic JDBC Statements Variables within SQL statement Precompiled once, multiple executions PreparedStatement for invocation PreparedStatement stmt = con.prepareStatement ( "SELECT * FROM data WHERE date = ?"); stmt.setDate (' ', j_date); ResultSet rset = stmt.executeQuery();

18 SQL Data Types For passing parameters to prepared statements specific SQL data types are needed Example java.util.Date jd = new java.util.Date(); java.sql.Date j_date = new java.sql.Date(jd.getTime());

19 Update Statements Updates have no result set int result = stmt.executeUpdate("delete from worklist"); Return value of executeUpdate –DDL-statement: always 0 –DML-statement: number of tuples

20 Error Handling Each SQL statement can generate errors –Thus each SQL method should be put into a try-block Exceptions are reported through exceptions of class SQLException

21 Example Import java.sql.*; public class JdbcDemo { public static void main(String[] args) { try {Class. forName(com.pointbase.jdbc.jdbcUniversalDriver); } catch (ClassNotFoundException exc) {System.out.println(exc.getMessage());} try {Connection con = DriverManager.getConnection(“jdbc:jdbc:demo",”tux”,”penguin”); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(“SELECT * FROM data”); while (rs.next()) {… process result tuples …} } catch (SQLException exc) {System.out.println(“SQLException: “ + exc.getMessage());} } }

22 Metadata Metadata allows to develop schema independent applications for databases –Generic output methods –Type dependent applications Two types of metadata are accessible –on result sets –on the database

23 ResultSet Metadata java.sql.ResultSetMetaData describes the structure of a result set object Information about a ResultSet object –Names, types and access properties of columns

24 Database Metadata java.sql.DatabaseMetaData provides information about the database (schema etc.) Information about the database –Name of database –Version of database –List of all tables –List of supported SQL types –Support of transactions

25 Example ResultSet rset = stmt.executeQuery(“SELECT * FROM data”); ResultSetMetaData rsmeta = rset.getMetaData(); int numCols = rsmeta.getColumnCount(); for (int i=1; i<=numCols; i++) { int ct = rsmeta.getColumnType(i); String cn = rsmeta.getColumnName(i); String ctn = rsmeta.getColumnTypeName(i); System.out.println(“Column #” + i + “: “ + cn + “ of type “ + ctn + “ (JDBC type: “ + ct + “)”); }