2006 JavaOne SM Conference | Session BOF-0367 JDBC™Technology-based Metadata Recipes Mahmoud Parsian Server Architect LimeLife, Inc.

Slides:



Advertisements
Similar presentations
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Introduction to SQL Programming Techniques.
Advertisements

1 JDBC Java Database Connectivity. 2 c.pdf
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 32 Java Database.
1 C. Shahabi Application Programming for Relational Databases Cyrus Shahabi Computer Science Department University of Southern California
1 Foundations of Software Design Lecture 27: Java Database Programming Marti Hearst Fall 2002.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2010 All Rights Reserved. 1.
DAT702.  Standard Query Language  Ability to access and manipulate databases ◦ Retrieve data ◦ Insert, delete, update records ◦ Create and set permissions.
Phil Brewster  One of the first steps – identify the proper data types  Decide how data (in columns) should be stored and used.
Data Persistence and Object-Relational Mapping Slides by James Brucker, used with his permission 1.
JDBC / ODBC JDBC is the java API that facilitate interaction of a java application with the DBMS. FIRST APPROACH:
CSCI 6962: Server-side Design and Programming
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.
CSE470 Software Engineering Fall Database Access through Java.
Intro to JDBC To effectively use Java Data Base Connectivity we must understand: 1.Relational Database Management Systems (RDBMS) 2.JDBC Drivers 3.SQL.
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.
A CCESSING D ATABASES WITH JDBC CH 24 C S 442: A DVANCED J AVA P ROGRAMMING.
CIS 270—Application Development II Chapter 25—Accessing Databases with JDBC.
Eurotrace Hands-On The Eurotrace File System. 2 The Eurotrace file system Under MS ACCESS EUROTRACE generates several different files when you create.
MySQL, Java, and JDBC CSE 3330 Southern Methodist University.
Dr R R DOCSIT, Dr BAMU. Basic Java : Introduction to JDBC 2 Objectives of This Session State what is Java Database Connectivity State different.
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L12_JDBC_MySQL 1 MySQL and JDBC.
Servlets Database Access. Agenda:  Setup Java Environment  Install Database  Install Database Drivers  Create Table and add records  Accessing a.
Introduction to JDBC Michelle Lee, Ye Wu & Jeff Offutt SWE 432 Design and Implementation of Software for the Web.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2015, Fred McClurg, All Rights.
Using Procedures & Functions Oracle Database PL/SQL 10g Programming Chapter 9.
JDBC. Preliminaries Database Database Collection of data Collection of data DBMS DBMS Database management system Database management system Stores and.
Hibernate 3.0. What is Hibernate Hibernate is a free, open source Java package that makes it easy to work with relational databases. Hibernate makes it.
COMP201 Java Programming Topic 15: Database Connectivity JDBC Reading: Chapter 4, Volume 2.
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.
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  Oracle Corporation, All rights reserved. 7 Accessing a Database Using SQLJ.
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.
DATABASE CONNECTIVITY TO MYSQL. Introduction =>A real life application needs to manipulate data stored in a Database. =>A database is a collection of.
DataBases and SQL INFSY 547 Spring Course Wrap Up April 12: Complete Work on Servlets Review of Team Projects Close of Portfolio Work April 19:
Web Programming MySql JDBC Web Programming.
What is MySQL? MySQL is a relational database management system (RDBMS) based on SQL (Structured Query Language). First released in January, Many.
Access Databases from Java Programs via JDBC Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale
Basics of JDBC Session 14.
1 JDBC – Java Database Connectivity CS , Spring 2010.
In the Name Of Almighty Allah. Java Application Connection To Mysql Created by Hasibullah (Sahibzada) Kabul Computer Science Faculty Afghanistan.
6-1 JAVA DATABASE CONNECTOR Colorado Technical University IT420 Tim Peterson.
SQL pepper. Why SQL File I/O is a great deal of code Optimal file organization and indexing is critical and a great deal of code and theory implementation.
CS 440 Database Management Systems Stored procedures & OR mapping 1.
Pan-European infrastructure for Ocean & Marine Data management An EU Integrated research Infrastructure Initiative (I3) How to implement CDI ? M. Fichaut,
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.
16 Copyright © 2004, Oracle. All rights reserved. Testing the Migrated Oracle Database.
CS320 Web and Internet Programming Database Access with JDBC Chengyu Sun California State University, Los Angeles.
DEPTT. OF COMP. SC & APPLICATIONS
CS3220 Web and Internet Programming Database Access with JDBC
Java Access to RDB Relational databases (RDBs) dominate today, due to:
JDBC Database Management Database connectivity
Metadata.
HW#4 Making Simple BBS Using JDBC
JDBC.
Design and Implementation of Software for the Web
Interacting with Database
MSIS 655 Advanced Business Applications Programming
Java Database Connectivity
Using a Database with JDBC
JDBC API.
Java API for Database Connectivity
CS3220 Web and Internet Programming Database Access with JDBC
CS3220 Web and Internet Programming Database Access with JDBC
Presentation transcript:

2006 JavaOne SM Conference | Session BOF-0367 JDBC™Technology-based Metadata Recipes Mahmoud Parsian Server Architect LimeLife, Inc. Session ID#: BOF-0367

2006 JavaOne SM Conference | Session BOF-0367 | 2 Learn how to use JDBC ™ metadata in homogenous and heterogeneous database environments. Goal of This Talk

2006 JavaOne SM Conference | Session BOF-0367 | 3 Agenda: JDBC ™ Metadata Recipes What is metadata? What is Database metadata? What is JDBC metadata? What is a reverse engineering of a DB? Metadata in a homogenous environment Metadata in a heterogeneous environment

2006 JavaOne SM Conference | Session BOF-0367 | 4 Agenda: JDBC ™ Metadata Recipes What is “metadata”? What is Database metadata? What is JDBC metadata? What is a reverse engineering of a DB? Metadata in a homogenous environment Metadata in a heterogeneous environment

2006 JavaOne SM Conference | Session BOF-0367 | 5 What is Metadata? ● Metadata Definition ● Metadata Examples ● Database Metadata Examples ● Purpose of Metadata

2006 JavaOne SM Conference | Session BOF-0367 | 6 Metadata Definition Metadata is data about data, which provide structured, descriptive information about other data. Metadata (Greek: meta-+ Latin: data “information”), literally “data about data”, is information that describes another set of data.

2006 JavaOne SM Conference | Session BOF-0367 | 7 Metadata Examples Example-1: a library catalog card, which contains data about the contents and location of a book: It is data about the data in the book referred to by the card.

2006 JavaOne SM Conference | Session BOF-0367 | 8 Metadata Examples: A Library Catalog Card A library catalog is an organized, searchable list of records that identify, describe, and locate materials in one or more library collections. Each record includes information about the material, such as its: ● Author(s) ● Title ● Publisher ● Publication Date ● Subject Heading(s) ● Physical Appearance (size, number of pages, maps, illustrations, etc.) ● Location within a collection

2006 JavaOne SM Conference | Session BOF-0367 | 9 Database Metadata Examples Example-1: what is the list of tables and views owned by database user Alex? Example-2: what is the signature of a stored procedure called printPayroll ? Example-3: what is the list of SQL keywords supported by a Connection object?

2006 JavaOne SM Conference | Session BOF-0367 | 10 Purpose of Metadata The purpose of the metadata is to make database objects in the database more accessible to users and to provide basic information about the objects in the database's collection. Each object within the database is described in a record by fields, such as: ● Table/View Names ● Stored Procedure names & their signatures ● Connection Properties ● Result Set Properties

2006 JavaOne SM Conference | Session BOF-0367 | 11 Agenda: JDBC ™ Metadata Recipes What is “metadata”? What is Database metadata? What is JDBC metadata? What is a reverse engineering of a DB? Metadata in a homogenous environment Metadata in a heterogeneous environment

2006 JavaOne SM Conference | Session BOF-0367 | 12 What is Database Metadata? You can use database metadata to ● Discover database schema and catalog information. ● Discover database users, tables, views, and stored procedures. ● Understand and analyze the result sets returned by SQL queries. ● Find out the table, view, or column privileges. ● Determine the signature of a specific stored procedure in the database. ● Identify the primary(PK)/foreign(FK) keys for a given table.

2006 JavaOne SM Conference | Session BOF-0367 | 13 Agenda: JDBC ™ Metadata Recipes What is metadata? What is Database metadata? What is JDBC metadata? What is a reverse engineering of a DB? Metadata in a homogenous environment Metadata in a heterogeneous environment

2006 JavaOne SM Conference | Session BOF-0367 | 14 What is JDBC ™ Metadata? Metadata as Low-Level Objects Metadata API ● DatabaseMetaData ● ResultSetMetaData ● ParameterMetaData ● DriverPropertyInfo ● RowSetMetaData Metadata Examples

2006 JavaOne SM Conference | Session BOF-0367 | 15 Metadata as Low-Level Objects

2006 JavaOne SM Conference | Session BOF-0367 | 16 Metadata API ● java.sql.DatabaseMetaData ● java.sql.ResultSetMetaData ● java.sql.ParameterMetaData ● java.sql.DriverPropertyInfo ● javax.sql.RowSetMetaData

2006 JavaOne SM Conference | Session BOF-0367 | 17 Metadata Example-1 Are Transactions Supported? java.sql.Connection conn = getConnection(“datasourceName”); java.sql.DatabaseMetaData meta = conn.getMetaData(); if (meta == null) { // metadata not supported by the Driver } else { // Check to see if transactions are supported if (meta.supportsTransactions()) { // Transactions are supported } else { // Transactions are not supported }

2006 JavaOne SM Conference | Session BOF-0367 | 18 Metadata Example-2: What Are the Table Names for a Database? private static final String[] DB_TABLE_TYPES = { "TABLE" }; private static final String[] DB_VIEW_TYPES = { "VIEW" }; java.sql.Connection conn = getConnection(“datasourceName”); java.sql.DatabaseMetaData meta = conn.getMetaData(); java.sql.ResultSet tables = null; java.sql.ResultSet views = null; … views = meta.getTables(null, null, null, DB_VIEW_TYPES); tables = meta.getTables(null, null, null, DB_TABLE_TYPES); while (tables.next()) { String tableName = rs.getString(“TABLE_NAME”); }

2006 JavaOne SM Conference | Session BOF-0367 | 19 Agenda: JDBC ™ Metadata Recipes What is metadata? What is Database metadata? What is JDBC metadata? What is a reverse engineering of a DB? Metadata in a homogenous environment Metadata in a heterogeneous environment

2006 JavaOne SM Conference | Session BOF-0367 | 20 What is a reverse engineering of a DB? Generate the original schema from database metadata: Examples: ● Torque: an object-relational mapper for Java ● SQL2Java:object-relational mapping tool. ● Abator: a code generator for the iBATIS data mapping framework

2006 JavaOne SM Conference | Session BOF-0367 | 21 Agenda: JDBC ™ Metadata Recipes What is metadata? What is Database metadata? What is JDBC metadata? What is a reverse engineering of a DB? Metadata in a homogenous environment Metadata in a heterogeneous environment

2006 JavaOne SM Conference | Session BOF-0367 | 22 Metadata in a Homogenous Environment Schema means the same for all DBs Catalog means the same for all DBs MySQL MySQL Driver MySQL DriverManager ConnectionDatabaseMetaData

2006 JavaOne SM Conference | Session BOF-0367 | 23 Agenda: JDBC ™ Metadata Recipes What is metadata? What is Database metadata? What is JDBC metadata? What is a reverse engineering of a DB? Metadata in a homogenous environment Metadata in a heterogeneous environment

2006 JavaOne SM Conference | Session BOF-0367 | 24 Metadata in a Heterogeneous Environment ● Metadata in a Heterogeneous Environment ● Solving the Metadata Problem ● Example-1: getDBNames() ● Example-2: getTableNames() ● DB Vendor Matters

2006 JavaOne SM Conference | Session BOF-0367 | 25 Metadata in a Heterogeneous Environment Schema does NOT mean the same for all DBs Catalog does NOT mean the same for all DBs MySQL MySQL Driver OraclePostgreSQL DriverManager ConnectionDatabaseMetaData DB2 Oracle Driver PostgreSQL Driver DB2 Driver

2006 JavaOne SM Conference | Session BOF-0367 | 26 Solving the Metadata Problem in a Heterogeneous Environment import java.sql.Connection; public class ConnectionObject { private Connection conn = null; private String databaseVendor = null; ConnectionObject(Connection conn, String databaseVendor) { this.conn = conn; this.databaseVendor = databaseVendor; } … get()/set() methods }

2006 JavaOne SM Conference | Session BOF-0367 | 27 Example-1: getDBNames() How do we get database names? ● Oracle treats “schema” as a database name, ● MySQL treats “catalog” as a database name. ● In order to get the name of databases from Oracle, you must use DatabaseMetaData.getSchemas() ● In order to get the name of databases from MySQL, you must use DatabaseMetaData.getCatalogs()

2006 JavaOne SM Conference | Session BOF-0367 | 28 Example-1: getDBNames() public static ResultSet getDBNames(ConnectionObject conn) throws SQLException { ResultSet dbNames = null; DatabaseMetaData meta = conn.getMetaData(); if (conn.isOracle()) { dbNames = meta.getSchemas(); } else if (conn.isMySQL()) { dbNames = meta.getCatalogs(); } else { … } return dbNames; }

2006 JavaOne SM Conference | Session BOF-0367 | 29 Example-2: getTableNames() ● DatabaseMetaData.getTables() method returns the table names for a given database connection object. ● The getTables() method works well for MySQL ● The getTables() does NOT work well for Oracle databases (in addition to user’s tables, it returns system tables, which are not needed) ● To get a list of user-defined tables and views, we use the Oracle’s metadata table called user_objects, which keeps track of objects (tables, views,...) owned by the user. We may use the following SQL query: select object_name from user_objects where object_type = 'TABLE';

2006 JavaOne SM Conference | Session BOF-0367 | 30 Example-2: getTableNames() public static ResultSet getTableNames(ConnectionObject conn) throws SQLException { ResultSet tableNames = null; DatabaseMetaData meta = conn.getMetaData(); if (conn.isOracle()) { tableNames = getOracleTableNames(conn); // cannot use metadata } else if (conn.isMySQL()) { tableNames = meta.getTables(…); } else { … } return tableNames; }

2006 JavaOne SM Conference | Session BOF-0367 | 31 DB Vendor Matters <database id="db1“, dbVendor=“mysql” jdbc:mysql://localhost/octopus org.gjt.mm.mysql.Driver root mysql... <database id="db2“, dbVendor=“oracle” oracle.jdbc.driver.OracleDriver scott tiger......

2006 JavaOne SM Conference | Session BOF-0367 | 32 Summary ● JDBC API enable us to get metadata ● JDBC metadata can be used to develop ● GUI database applications ● SQL Adapters and Connectors ● Reverse engineer the whole database ● In using JDBC metadata, make sure to consider the “vendor” factor (this will make your DB applications to work in homogenous and heterogeneous environments)

2006 JavaOne SM Conference | Session BOF-0367 | 33 For More Information ● Metadata Definition : ● JDBC™ Basics: ● JDBC™ API Tutorial and Reference, Third Edition By Maydene Fisher,…, Addison Wesley, ● JDBC™ Metadata, MySQL and Oracle Recipes, Mahmoud Parsian, Apress.com, 2006 ● Understanding JDBC™ Metadata by Kyle Brown BC_Metadata.htm

2006 JavaOne SM Conference | Session XXXX | 34 Q&A Mahmoud Parsian