Computer Engineering Lab

Slides:



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

Java Database Connectivity (JDBC). 2/24 JDBC (Java DataBase Connectivity) - provides access to relational database systems JDBC is a vendor independent.
Distributed Application Development B. Ramamurthy.
Object Oriented Programming Java Java’s JDBC Allows access to any ANSI SQL-2 DBMS Does its work in terms of SQL The JDBC has classes that represent:
1 Lecture 05: Database Programming (JDBC). 2 Outline JDBC overview JDBC API Reading: Chapter 10.5 Pointbase Developer Manual.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 32 Java Database.
JDBC Overview Autumn 2001 Lecturer: C. DeJong. Relational Databases widespread use used via SQL (Structured Query Language) freely available powerful.
V OOP: JDBC1 V Week 8 v Objective –to give some background on JDBC to help with the lab exercises Fall, 2002 Introduction to Java Database.
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.
Adv. CoE Lab: JDBC 1 Advanced Computer Engineering Lab Objective – –to give some background on JDBC to help with the lab exercises , Semester.
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
Overview 1. What is JDBC? 2. The JDBC-ODBC Bridge 3. JDBC Pseudocode 4. simpJDBC.java.
Lab: JDBC 1 Computer Engineering Lab III v Objective –to give some background on JDBC to help with the lab exercises , Semester 1,
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
Java Database Connectivity (JDBC). Introduction Database –Collection of data DBMS –Database management system –Storing and organizing data SQL –Relational.
1 Java Database Connection (JDBC) There are many industrial-strength DBMS's commercially available in the market. Oracle, DB2, and Sybase are just a few.
Java Database Connectivity Vijayan Sugumaran Department of DIS Oakland University.
© Wang Bin 2004 JDBC ----Java Database Connectivity.
CSE470 Software Engineering Fall Database Access through Java.
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.
Database Programming in Java Corresponds with Chapter 32, 33.
CIS 270—Application Development II Chapter 25—Accessing Databases with JDBC.
Georgia Institute of Technology Making Text for the Web part 5 Barb Ericson Georgia Institute of Technology March 2006.
JDBC Tutorial MIE456 - Information Systems Infrastructure II Vinod Muthusamy November 4, 2004.
JDBC (Java Database Connectivity) SNU OOPSLA Lab. October 2005.
Designing and Developing WS B. Ramamurthy. Plans We will examine the resources available for development of JAX-WS based web services. We need an IDE,
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.
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.
Chapter 8 Databases.
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.
JDBC – Java Database Concentricity
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 (JDBC). Topics 1. The Vendor Variation Problem 2. SQL and Versions of JDBC 3. Creating an ODBC Data Source 4. Simple Database.
Copyright © 2002 ProsoftTraining. All rights reserved. Building Database Client Applications Using JDBC 2.0.
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 CS 124. JDBC Java Database Connectivity Database Access Interface provides access to a relational database (by allowing SQL statements to be sent.
12/6/2015B.Ramamurthy1 Java Database Connectivity B.Ramamurthy.
COMP 321 Week 4. Overview Normalization Entity-Relationship Diagrams SQL JDBC/JDBC Drivers hsqldb Lab 4-1 Introduction.
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.
Database Access Using JDBC BCIS 3680 Enterprise Programming.
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.
Basics of JDBC Session 14.
Advanced Java Session 5 New York University School of Continuing and Professional Studies.
Database Programming With Java & JDBC Reading: DD Ch. 18, pp al/jdbc/index.html, or anything covering JDBC.
Intro to JDBC Joseph Sant Applied Computing and Engineering Sciences Sheridan ITAL.
JSP/Database Connectivity Instructor: Dr. M. Anwar Hossain.
Java and database. 3 Relational Databases A relational Database consists of a set of simple rectangular tables or relations The column headings are.
DEPTT. OF COMP. SC & APPLICATIONS
Lec - 14.
JDBC – Java DataBase Connectivity
Interacting with Database
Java Database Connectivity
Java API for Database Connectivity
JDBC Example.
Presentation transcript:

Computer Engineering Lab 242-301, Semester 1, 2015-2016 3SA03. Introduction to JDBC Objective to give some background on JDBC to help with the lab exercises

Overview 1. What is JDBC? 2. The JDBC-ODBC Bridge 3. Four Types of JDBC Drivers 4. JDBC Pseudocode 5. SimpleJDBC.java Continued

6. Meta Data 7. Using MS Access 8. More Information

1. What is JDBC? JDBC is a Java library which allows Java programs to execute SQL inside databases.

JDBC in Use Oracle DB driver for Oracle Sybase DB Java JDBC driver program JDBC driver for Sybase connectivity data processing : // many more utilities non-MS driver for Access Access DB Green means "Java code" jdbc-odbc bridge odbc driver

2. The JDBC-ODBC Bridge ODBC (Open Database Connectivity) is a Microsoft API that allows C/C++ programs to execute SQL inside databases ODBC is supported by many database companies. Continued

The JDBC-ODBC bridge used to come free with Java: The JDBC-ODBC bridge allowed Java code to use the C/C++ interface of ODBC The JDBC-ODBC bridge used to come free with Java: discontinued in Java 8 Instead I will use the free "UCanAccess" non-Microsoft driver for Access databases. this is a type 4 driver for JDBC

3. Four Types of JDBC Driver 1. JDBC-ODBC Bridge (type 1) translate Java to the ODBC API used by many Windows-based databases, e.g. MS-Access 2. Database Protocol Driver (type 4) Independent from the OS/hardware because the driver is in Java. Continued

3. Native API Connection Driver (type 2) connected by a OS native module, dependent on the OS or hardware (e.g. DLLs on Windows) 4. Net Connection Driver (type 3) use Java to access the database via networking middleware (usually TCP/IP) required for networked applications

Using UCanAccess Download UCanAccess-3.0.0-bin.zip from: http://sourceforge.net/projects/ucanaccess/ unzip in directory with my code and batch files Documentation at: http://ucanaccess.sourceforge.net/site.html

Some UCanAccess Features Supports many old Access formats SELECT, INSERT, UPDATE, DELETE DDL: CREATE table with primary key, DROP Transactions and savepoints Concurrent access from multiple users ANSI 92 SQL, core SQL-2008, MS Access SQL LIKE operator, wildcard character

Other JDBC Drivers Lists of drivers (freeware, shareware, and commercial) can be found at: http://en.wikipedia.org/wiki/JDBC_driver http://www.oracle.com/technetwork/java/ index-136695.html

4. JDBC as a Diagram SQL data make link to driver Green means creates DriveManager creates Connection Statement creates ResultSet SQL data Driver make link to driver Green means "Java code" data SQL

DriveManager It is responsible for establishing the connection to the database through the driver. e.g. Class.forName( "net.ucanaccess.jdbc.UcanaccessDriver"); Connection conn = DriveManager.getConnection(url); name of the database

Name the Database The name and location of the database is given as a URL the details of the URL vary depending on the type of database that is being used

UCanAccess Database URL jdbc:ucanaccess:// host.domain.com: 2048 c:/file The comms protocol The machine holding the database. The port used for the connection. The path to the database on the machine (accdb or mdb) e.g. jdbc:ucanaccess://Books.accdb

Statement Object The Statement object provides a ‘workspace’ where SQL queries can be created, executed, and results collected. e.g. Statement st = conn.createStatement(): ResultSet rs = st.executeQuery( “select * from Authors”); : st.close();

ResultSet Object Stores the results of a SQL query. A ResultSet object is similar to a ‘table’ of answers, which can be examined by moving a ‘pointer’ (cursor). Continued

Cursor operations: Typical code: 23 John 5 Mark 17 Paul 98 Peter Cursor operations: first(), last(), next(), previous(), etc. Typical code: while( rs.next() ) { // process the row; }

5. SimpleJDBC.java // SimpleJDBC.java // Displays the firstnames and lastnames // of the Authors table in the Books db. import java.sql.*; public class SimpleJDBC { public static void main(String[] args) { // The URL for the Books database. String url = UcanaccessDriver.URL_PREFIX + "Books.accdb"; :

try {. // load the UCanAccess driver Class. forName( "net. ucanaccess try { // load the UCanAccess driver Class.forName( "net.ucanaccess.jdbc.UcanaccessDriver"); // connect to db using DriverManager Connection conn = DriverManager.getConnection(url); // Create a statement object Statement statement = conn.createStatement(); // Execute the SQL query ResultSet rs = statement.executeQuery( "SELECT lastName, firstName FROM Authors" ); :

// Print the result set while( rs.next() ) System.out.println( rs.getString("lastName") + ", " + rs.getString("firstName") ); // Close down statement.close(); conn.close(); } :

catch (ClassNotFoundException e) { System. out catch (ClassNotFoundException e) { System.out.println( "Could not load UCanAccess library: "+e); } catch (SQLException e) { System.out.println("SQL Exception: "+e); } } // end of main() } // end of SimpleJDBC class

Correct Execution

No UCanAccess Folder

No Books.accdb in Folder

5.1. Accessing a ResultSet The ResultSet class contains many methods for accessing the value of a column of the current row can use the column name or position e.g. get the value in the lastName column: rs.getString("lastName") Continued

There are many methods for accessing the data, e.g. getString(), getDate(), getInt(), getFloat(), getObject() JDBC documentation starts at: http://docs.oracle.com/javase/7/docs/api/ java/sql/package-summary.html look in "ResultSet"

6. Meta Data Meta data is the information about the database: e.g. the number of columns, the types of the columns meta data is the schema information meta data ID Name Course Mark 007 James Bond Shooting 99 008 Aj. Andrew Kung Fu 1

One important use for metadata is for formatting result set data e.g. instead of displaying the results as text, display them in a Java table with headers, rows, columns see TableDisplay.java in the Exercises

6.1. Accessing Meta Data The getMetaData() method can be used on a ResultSet object to create its meta data object. e.g. ResultSetMetaData md = rs.getMetaData();

6.2. Using Meta Data int numCols = md.getColumnCount(); for (int i = 1; i <= numCols; i++) { if (md.getColumnType(i) == Types.CHAR) System.out.println( md.getColumnName(i) ) }

6.3. More Meta Data Methods etc. getTableName() getPrecision() number of decimal digits in the column isSigned() returns true if column has signed numbers isCurrency() etc.

ResultSetMetaData Docs JDBC documentation starts at: http://docs.oracle.com/javase/7/docs/api/ java/sql/package-summary.html look in "ResultSetMetaData"

7. Using MS Access MS Access changed its file formats when Access 2007 was released: for Access 2003 (and earlier) you should use Books.mdb for Access 2007 and later, you should use Books.accdb both versions are in the lab's website.

Access and SQL How to use SQL in Access is described at: http://www.jaffainc.com/SQLStatementsInAccess.htm And on the website, in sqlAccess2007.txt and sql_intr.pdf

TableRelationships in Books.accdb (and Books.mdb) Under Database Tools > Relationships

8. More Information The JDBC tutorial is very good Java: How to Program, 10th edition Paul Deitel and Harvey Deitel Pearson, 2015, Chapter 24 I've placed an extract of that chapter on the website It will only be there for 1-2 weeks The JDBC tutorial is very good http://docs.oracle.com/javase/tutorial/jdbc