Database Connectivity in Java

Similar documents
COSC344 Database Theory and Applications. Java and SQL. Lecture 12

Services. Relational. Databases & JDBC. Today. Relational. Databases SQL JDBC. Next Time. Services. Relational. Databases & JDBC. Today.

Chapter 9 Java and SQL. Wang Yang wyang@njnet.edu.cn

SQL and Java. Database Systems Lecture 19 Natasha Alechina

Why Is This Important? Database Application Development. SQL in Application Code. Overview. SQL in Application Code (Contd.

Java DataBase Connectivity (JDBC)

CS 377 Database Systems SQL Programming. Li Xiong Department of Mathematics and Computer Science Emory University

The JAVA Way: JDBC and SQLJ

Java Server Pages and Java Beans

Database Access from a Programming Language: Database Access from a Programming Language

Database Access from a Programming Language:

Application Development A Cocktail of Java and MCP. MCP Guru Series Dan Meyer & Pramod Nair

What is ODBC? Database Connectivity ODBC, JDBC and SQLJ. ODBC Architecture. More on ODBC. JDBC vs ODBC. What is JDBC?

Database Programming. Week *Some of the slides in this lecture are created by Prof. Ian Horrocks from University of Oxford

DATABASDESIGN FÖR INGENJÖRER - 1DL124

LSINF1124 Projet de programmation

Performance Tuning for the JDBC TM API

Self-test Database application programming with JDBC

CS/CE 2336 Computer Science II

JDBC. It is connected by the Native Module of dependent form of h/w like.dll or.so. ex) OCI driver for local connection to Oracle

Overview. Database Application Development. SQL in Application Code (Contd.) SQL in Application Code. Embedded SQL. Embedded SQL: Variables

Database Access Through Java Technologies

Brazil + JDBC Juin 2001, douin@cnam.fr

JDBC (Java / SQL Programming) CS 377: Database Systems

1 SQL Data Types and Schemas

Supplement IV.D: Tutorial for MS Access. For Introduction to Java Programming By Y. Daniel Liang

Database Application Development. Overview. SQL in Application Code. Chapter 6

Applets, RMI, JDBC Exam Review

MAKING ORACLE AND SQLJ WORK FOR YOU John Jay King, King Training Resources

Course Objectives. Database Applications. External applications. Course Objectives Interfacing. Mixing two worlds. Two approaches

Using the DataDirect Connect for JDBC Drivers with the Sun Java System Application Server

NGASI AppServer Manager SaaS/ASP Hosting Automation for Cloud Computing Administrator and User Guide

How To Use The Database In Jdbc.Com On A Microsoft Gdbdns.Com (Amd64) On A Pcode (Amd32) On An Ubuntu (Amd66) On Microsoft

CSc 230 Software System Engineering FINAL REPORT. Project Management System. Prof.: Doan Nguyen. Submitted By: Parita Shah Ajinkya Ladkhedkar

CSI 2132 Lab 8. Outline. Web Programming JSP 23/03/2012

CS2506 Operating Systems II Lab 8, 8 th Tue/03 /2011 Java API

Using the DataDirect Connect for JDBC Drivers with WebLogic 8.1

Java and Databases. COMP514 Distributed Information Systems. Java Database Connectivity. Standards and utilities. Java and Databases

Chapter 1 JDBC: Databases The Java Way! What Is The JDBC? The JDBC Structure ODBC s Part In The JDBC Summary

Java MySQL Connector & Connection Pool

Apéndice C: Código Fuente del Programa DBConnection.java

Java Programming. JDBC Spring Framework Web Services

Course Number: IAC-SOFT-WDAD Web Design and Application Development

Evaluation. Copy. Evaluation Copy. Chapter 7: Using JDBC with Spring. 1) A Simpler Approach ) The JdbcTemplate. Class...

Geronimo Quartz Plugins

Jaybird 2.1 JDBC driver. Java Programmer's Manual

Using DOTS as Apache Derby System Test

Chapter 4: SQL. Schema Used in Examples

TABLE OF CONTENTS...2 INTRODUCTION...3 APPLETS AND APPLICATIONS...3 JAVABEANS...4 EXCEPTION HANDLING...5 JAVA DATABASE CONNECTIVITY (JDBC)...

Using Netbeans and the Derby Database for Projects Contents

Spring,2015. Apache Hive BY NATIA MAMAIASHVILI, LASHA AMASHUKELI & ALEKO CHAKHVASHVILI SUPERVAIZOR: PROF. NODAR MOMTSELIDZE

Apache OFBiz Advanced Framework Training Video Transcription

public class ResultSetTable implements TabelModel { ResultSet result; ResultSetMetaData metadata; int num cols;

Database Migration from MySQL to RDM Server

Building Web Applications, Servlets, JSP and JDBC

Heterogeneous Data Replication

Making Oracle and JDBC Work For You

Accesssing External Databases From ILE RPG (with help from Java)

SQL and programming languages

Java DB Performance. Olav Sandstå Sun Microsystems, Trondheim, Norway Submission ID: 860

Microsoft SQL Server Features that can be used with the IBM i

Glassfish, JAVA EE, Servlets, JSP, EJB

Announcements. Comments on project proposals will go out by in next couple of days...

Version Overview. Business value

CHAPTER 3. Relational Database Management System: Oracle. 3.1 COMPANY Database

Part IV: Java Database Programming

Database System Concepts

Using the DataDirect Connect for JDBC Drivers with Tomcat

Generating XML from Relational Tables using ORACLE. by Selim Mimaroglu Supervisor: Betty O NeilO

Demonstration of Electronic Health Records (EHR) on Java Card 3.0 Technology-Based Devices

How To Use A Sas Server On A Java Computer Or A Java.Net Computer (Sas) On A Microsoft Microsoft Server (Sasa) On An Ipo (Sauge) Or A Microsas (Sask

Microsoft SQL Server Connector for Apache Hadoop Version 1.0. User Guide

TECH TUTORIAL: EMBEDDING ANALYTICS INTO A DATABASE USING SOURCEPRO AND JMSL

Object Oriented Design with UML and Java. PART XVIII: Database Technology

JBS-102: Jboss Application Server Administration. Course Length: 4 days

MS ACCESS DATABASE DATA TYPES

Customer Bank Account Management System Technical Specification Document

JAVA ENTERPRISE IN A NUTSHELL. Jim Farley and William Crawford. O'REILLY 4 Beijing Cambridge Farnham Koln Paris Sebastopol Taipei Tokyo.

Package sjdbc. R topics documented: February 20, 2015

DEVELOPING MULTITHREADED DATABASE APPLICATION USING JAVA TOOLS AND ORACLE DATABASE MANAGEMENT SYSTEM IN INTRANET ENVIRONMENT

Equipment Room Database and Web-Based Inventory Management

Performance Evaluation of Java Object Relational Mapping Tools

Implementing the Shop with EJB

Database access and JDBC. Tecniche di Programmazione A.A. 2013/2014

Spring Data JDBC Extensions Reference Documentation

Architecting the Future of Big Data

Using Apache Derby in the real world

OPENRULES. Database Integration. Open Source Business Decision Management System. Release 6.2.1

Getting Started with Web Applications

7 Web Databases. Access to Web Databases: Servlets, Applets. Java Server Pages PHP, PEAR. Languages: Java, PHP, Python,...

Introduction Web Portal Main Page Group Management Create group Modify Group Member List... 5

Core Java+ J2EE+Struts+Hibernate+Spring

Java and RDBMS. Married with issues. Database constraints

Security Module: SQL Injection

A Brief Introduction to MySQL

Exploiting Java Technology with the SAS Software Barbara Walters, SAS Institute Inc., Cary, NC

Still Aren't Doing. Frank Kim

CA IDMS Server r17. Product Overview. Business Value. Delivery Approach

Using Business Activity Monitoring

FileMaker 8. Installing FileMaker 8 ODBC and JDBC Client Drivers

Transcription:

Database Connectivity in Java SWE 642, Fall 2008 Nick Duan What is JDBC? Overview Types of JDBC Drivers JDBC APIs JDBC Data Types JDBC Connection Pool and Data Sources Object-to-Relational Database Mapping Summary 8/17/2008 Nick Duan 2 1

What is JDBC? A set of APIs for connecting database with Java applications. Part of the Java SE standard JDBC Driver: A run-time component (Java classes) to be loaded into the Java application Java Application JDBC API JVM JDBC Driver Database 8/27/2008 Nick Duan 3 Four Types of JDBC Drivers JDBC-ODBC bridge ODBC wrapped with JDBC APIs Part Java, Part Native Driver Driver implemented using vendor s APIs Gateway Database Access Server Driver implemented as a gateway server between application and database Pure Java Drivers Driver implemented to support vendor-specific database protocol 8/27/2008 4 2

JDBC APIs Java SE contains both java.sql and javax.sql packages. The current JDBC version is 4.0. java.sql.* contains the core JDBC APIs Making connections via DriverManager Creating and sending SQL statements to database Retrieving results from a select query APIs for representing SQL Data Types and user defined data types in SQL Retrieving of database metadata SQL Exceptions Javax.sql.* is the optional package for Java EE Getting connections from DataSource Connection Pooling and Statement pooling Distributed transactions (XAConenction and XADataSource) APIs for handling RowSet 8/27/2008 Nick Duan 5 JDBC Programming Steps 1. Initialize a DB driver or data source 2. Obtain a JDBC connection 3. Create and execute SQL statements 4. Retrieve the results by navigating the ResultSets if executing Select statement 5. Close the JDBCconnection! What happened if you leave the connection open? 8/27/2008 Nick Duan 6 3

JDBC Example Other alternatives for loading driver class are try { available Class.forName( org.hsqldb.jdbcdriver ); Connection con = DriverManager.getConnection ( "jdbc:mydriver:wombat", "mylogin","mypassword"); Statement stmt = con.createstatement(); ResultSet rs = stmt.executequery( "SELECT a, b, c FROM mytable"); while (rs.next()) { Enumeration API for int x = rs.getint("a"); navigating through the String s = rs.getstring("b"); ResultSet float f = rs.getfloat("c"); } } System.out.println( Result as follows: + x +, + s +, + f); stmt.close(); // not necessary if connection Must is closed call this method con.close(); to free up connection } resources catch (Exeption e) { } 8/27/2008 Nick Duan 7 PreparedStatement A SQL statement is precompiled and stored in a PreparedStatement object Support variable substitution Enable executing the SQL statement multiple times try { PreparedStatement pstmt = con.preparestatement ("UPDATE EMPLOYEES SET SALARY =? WHERE ID =?"); pstmt.setbigdecimal(1, 153833.00); pstmt.setint(2, 110592); } catch (SQLException e) { } 8/27/2008 Nick Duan 8 4

Mapping SQL Data Types SQL Types mapped to Java Types SQL Types Java Types SQL Types Java Types CHAR String DATE Java.sql.Date VARCHAR String TIME Java.sql.Time LONGVARCHAR String TIMESTAMP Java.sql.Timestamp TINYINT short INTEGER int JAVAOBJECT Object BIGINT Long BLOB Java.sql.Blob interface REAL Float CLOB Java.sql.Clob interface FLOAT Double ARRAY Java.sql.Array interface DOUBLE Double BIT boolean 8/27/2008 Nick Duan 9 Dealing with ResultSet Retrieving Results: ResultSet rs = statement.exeutequery( select * from student ); while (rs.next()) { // defined in an Enumeration API System.out.println(rs.getString( name ) + } Retrieving Result Metadata ResultSetMetaData metadata = rs.getmetadata(); int cnt = metadata.getcolumncount(); String cname = metadata.getcolumnname(n); int typeconstant = metadata.getcolumntype(n); Performing Selective Update Statement stmt = con.createstatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE); 8/27/2008 Nick Duan 10 5

Handling Database Transactions Transaction deals with state-changing SQL statements, i.e. insert, update and delete Transaction demarcations (begin, end) are defined by JDBC connection API Default connection setting for transaction is auto-commit, i.e. each statement execution is a transaction by itself To Turn off auto-commit: connection.setautocommit(false) Manual/explicit Transaction control via commit() and rollback() API 8/27/2008 Nick Duan 11 It s All About Connectivity Connection created via DriverManager representing a physical connection Single connection created at a time What s wrong with this? JDBC Connection Pool A set of homogenous JDBC connections managed in a Pool A set of logical connections that map to a set of physical connections Connection.close() doesn t close the physical connection to database 8/27/2008 Nick Duan 12 6

Connection Pool Management Java Application JDBC APIs JDBC Driver Physical Connections Database Server JVM Logical Connections Connection Pool 8/27/2008 Nick Duan 13 JDBC DataSource API Started in JDBC version 2.0, defined in javax.sql package Works in conjunction with Java Naming and Directory Interface (JNDI) to support enterprise database connectivity Support connection pooling and distributed transactions Connection to be created via the DataSource API Context initctx = new InitialContext(); Context envctx = (Context) initctx.lookup("java:comp/env"); DataSource ds = (DataSource) envctx.lookup("jdbc/employeedb"); Connection conn = ds.getconnection(); // use this connection to access the database... conn.close(); 8/27/2008 Nick Duan 14 7

Configuring JDBC DataSource DataSources are to be registered/configured with the JNDI registry embedded in the application server Java Apps Java Java Apps Java Apps Java Apps Servlets Servlet Container Data Data Source Source JNDI Registry Web Application Server (Tomcat) 8/27/2008 Nick Duan 15 Object-to-Relational Database Mapping A set of Java APIs on top of JDBC so that developers don t have to be concerned with dealing with relational data Enables relational data to be handled as object Map database table to class definition Map database record to object Use JavaBeans naming conventions (i.e. getters and setters) Creational/Factory pattern to create Bean objects OO-to-Relational database framework Sun JDO, http://java.sun.com/jdo/index.jsp Hibernate, http://www.hibernate.org/ Apache ibatis, http://ibatis.apache.org/ Oracle TopLink, http://www.oracle.com/technology/products/ias/toplink/index. html 8/27/2008 Nick Duan 16 8

Summary JDBC provides a set of convenient APIs for accessing relational databases Drivers are to be provided by database vendors Different types of drivers for different applications Five steps database access via JDBC Connection pool to be used for large scale applications, especially web applications Use DataSource in conjunction with JNDI for creating pooled connections 8/27/2008 Nick Duan 17 Quiz What type of database drivers should be use in a Java application if you want to interact with multiple databases at the same time? Configure the use of HyperSonic database in Tomcat to support connection pool Modify the sample database applications to use pooled connections 8/27/2008 Nick Duan 18 9