Java DataBase Connectivity (JDBC)



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

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

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

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

Making Oracle and JDBC Work For You

SQL and Java. Database Systems Lecture 19 Natasha Alechina

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

Self-test Database application programming with JDBC

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

Database Access Through Java Technologies

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

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

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

Chapter 4: SQL. Schema Used in Examples

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

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

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

Performance Tuning for the JDBC TM API

CS/CE 2336 Computer Science II

The JAVA Way: JDBC and SQLJ

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

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

Database Access from a Programming Language:

Database Migration from MySQL to RDM Server

Part IV: Java Database Programming

Building Web Applications, Servlets, JSP and JDBC

LSINF1124 Projet de programmation

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

1 SQL Data Types and Schemas

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

Porting from Oracle to PostgreSQL

To create a universal SQL client for accessing local or remote database ( 35.2). To execute SQL statements in a batch mode ( 35.3).

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

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

Oracle WebLogic Server

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

Applets, RMI, JDBC Exam Review

Using SQL Server Management Studio

An Introduction to SQL Injection Attacks for Oracle Developers. January 2004 INTEGRIGY. Mission Critical Applications Mission Critical Security

Chapter 4: SQL. Schema Used in Examples. Basic Structure. The select Clause. modifications and enhancements! A typical SQL query has the form:

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

WS-JDBC. (Web Service - Java DataBase Connectivity) Remote database access made simple: 1.

HyperSQL User Guide. HyperSQL Database Engine (HSQLDB) 2.1. Edited by The HSQL Development Group, Blaine Simpson, and Fred Toussi

database abstraction layer database abstraction layers in PHP Lukas Smith BackendMedia

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

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

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

Using DOTS as Apache Derby System Test

Package sjdbc. R topics documented: February 20, 2015

FileMaker 11. ODBC and JDBC Guide

Java 7 Recipes. Freddy Guime. vk» (,\['«** g!p#« Carl Dea. Josh Juneau. John O'Conner

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

Database System Concepts

Jaybird 2.1 JDBC driver. Java Programmer's Manual

ODBC Chapter,First Edition

An Oracle White Paper August, Oracle JDBC Memory Management

Information Technology NVEQ Level 2 Class X IT207-NQ2012-Database Development (Basic) Student s Handbook

WHITE PAPER. An Introduction to SQL Injection Attacks for Oracle Developers

How To Create A Table In Sql (Ahem)

Oracle to MySQL Migration

Java Programming with Oracle JDBC

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

To Java SE 8, and Beyond (Plan B)

Brazil + JDBC Juin 2001, douin@cnam.fr

FileMaker 14. ODBC and JDBC Guide

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

Apache OFBiz Advanced Framework Training Video Transcription

Java MySQL Connector & Connection Pool

1 Changes in this release

Abstract. Introduction. Web Technology and Thin Clients. What s New in Java Version 1.1

Java Server Pages and Java Beans

SQL Server An Overview

Relational databases and SQL

Oracle8/ SQLJ Programming

Using the DataDirect Connect for JDBC Drivers with WebLogic 8.1

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

FREE ONLINE EDITION. (non-printable free online version) If you like the book, please support the author and InfoQ by. purchasing the printed book:

The release notes provide details of enhancements and features in Cloudera ODBC Driver for Impala , as well as the version history.

SQL. Short introduction

Programmers Reference. jconnect for JDBC 7.0

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

What is a database? COSC 304 Introduction to Database Systems. Database Introduction. Example Problem. Databases in the Real-World

Working With Derby. Version 10.2 Derby Document build: December 11, 2006, 7:06:09 AM (PST)

An Oracle White Paper June Migrating Applications and Databases with Oracle Database 12c

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

Java (12 Weeks) Introduction to Java Programming Language

Database Access with JDBC

A Brief Introduction to MySQL

Linas Virbalas Continuent, Inc.

Spring Data JDBC Extensions Reference Documentation

2. Accessing Databases via the Web

Advanced SQL. Jim Mason. Web solutions for iseries engineer, build, deploy, support, train

Transcription:

Java DataBase Connectivity (JDBC) Plan Presentation of JDBC JDBC Drivers Work with JDBC Interfaces and JDBC classes Exceptions SQL requests Transactions and exceptions 1

Presentation JDBC (Java Data Base Connectivity) is the basic API for accessing relational databases with the SQL language, from Java programs Provided by package java.sql The JDBC API is nearly completely independant from SGBDs Supplies homogenous access to RDBMSs, by providing an abstraction of the targeted databases SQL versions supported First JDBC versions supported only SQL-2 Entry Level JDBC 2, 3 and 4 support SQL3 JDBC 4 comes with Java 6 and add several goodies for easier use like «automatic driver loading» and support for LOBs, etc. 2

java.sql Many interfaces, few classes Interfaces = API for the developer JDBC do not provide the classes that implements these interfaces, it s the driver! JDBC drivers 3

JDBC Driver Classes that implements JDBC interfaces Database dependants All Database vendors propose a JDBC driver JDBC driver types Type 1 : JDBC-ODBC bridge Type 2 : uses native function from the Database API (often written in C) Type 3 : allow the use of a middleware driver Type 4 : 100% Java driver that uses the network protocol of the Database Modern drivers are all of type 4! 4

Type 4 : 100 % Java with direct access to the DB Java Application Driver in Java Methods from JDBC driver use sockets for communicating with the DB DB network protocol DB Work with JDBC 5

Work with JDBC Driver classes must be in the classpath.java files that use JDBC must import java.sql : import java.sql.*; Until JDBC 4, it was necessary to load in memory the driver classes for example: Class.forName("oracle.jdbc.OracleDriver"); No need to do this anymore with JDBC 4! Examples and explanations in a few slides Different steps to work with JDBC 1. Get a connection object (of type Connection) 2. Create SQL instructions (Statement, PreparedStatement or CallableStatement) 3. Execute these instructions : Query the DB (executequery) Modify the DB content (executeupdate) Or any other kind of order (execute) (4. Validate or invalidate the transaction with commit or rollback) 5. Close the connection (close()) 6

Classes and JDBC interfaces Most important interfaces Driver : returns an instance of Connection Connection : connection to the DB Statement : SQL order PreparedStatement : compiled once by the driver and may be executed many times with different parameters CallableStatement : stored procedures in the DB ResultSet : lines that came back after a SELECT order ResultSetMetaData : description of lines returned 7

Main classes DriverManager : manages drivers and connections Date : SQL date Time : SQL hours, minutes, seconds TimeStamp : date and hour, microsecond accuracy, Types : constants for SQL types (for conversion to Java types) Driver Interface The connect method from Driver takes as parameter a URL and other informations such as login/password and returns an instance of Connection This instance of Connection will be use to execute requests to the DB connect returns null if the driver is not correct or if parameters are not valid Used by DriverManager ; not visible by the developer. 8

URL of a Database (datasource) A URL for a database looks like this: jdbc:subprotocol:databasename Example: jdbc:oracle:thin:@sirocco.unice.fr:1521:info oracle:thin is a subprotocol (driver «thin» ; Oracle provides also another heavier driver) @sirocco.unice.fr:1521:info designs the INFO database located on a server named sirocco that runs an oracle DB on port 1521 DriverManager The DriverManager class is the one to use! Note: with JDBC 1-3 it was necessary to load by hand the driver class before using DriverManager. With JDBC 4 this is useless Class.forName("oracle.jdbc.OracleDriver"); 9

Obtain a connexion Ask the DriverManager class through its static method getconnection(): String url = "jdbc:oracle:thin:@sirocco.unice.fr:1521:info"; Connection conn = DriverManager.getConnection(url, "toto", "pass"); The DriverManager class tries all drivers registered in the classpath until one gives a response that is not null. Connections and threads Connections are costly objects, it takes time to obtain one, Furthermore, an instance of Connection cannot be shared by several threads, it is not thread-safe! Best practice for web applications: use a connexion pool provided by a DataSource object. Example next slide, note that we removed all necessary try catch finally etc. 10

Datasource example import javax.sql.datasource; import java.sql.date; @ApplicationScoped @JDBC public class BookRepositoryJDBCImpl implements BookRepository { @Resource(name = "jdbc/bookstore") // defined in the application server private DataSource datasource; addbook(string title, String description) { Connection connection = datasource.getconnection(); PreparedStatement preparestatement = connection.preparestatement("insert into book(title, description, price, pubdate) values (?,?,?,?)"); preparestatement.setstring(1, title); preparestatement.setstring(2, description); } int rowcount = preparestatement.executeupdate(); connection.close(); DataSource Provides access to connections in a shared resources environment Generally provides advanced features: Connection pooling PreparedStatement pooling Distributed transactions (XA protocol) Retrieved using the @Resource annotation in Servlets, Beans, EJBs Or retrieved from a JNDI naming service: Context ctx = new InitialContext(); DataSource ds = (DataSource)ctx.lookup("DataSourceName"); Connection c = ds.getconnection(); 11

ConnectionPoolDataSource Connection pool architecture: A connection pool contains a group of JDBC connections that are created when the connection pool is registered when starting up WebLogic Server or when deploying the connection pool to a target server or cluster. Connection pools use a JDBC driver to create physical database connections. Your application borrows a connection from the pool, uses it, then returns it to the pool by closing it. Transactions By default, a connection is in «auto-commit» mode: acommit is automatically performed after any SQL order that modifies the DB content. Best practice: set this setting off conn.setautocommit(false) The transaction will be manually validated or invalidates using: conn.commit() conn.rollback() 12

Connection best practice Create a method that does: Get the connexion, Set the proper settings for transactions Begin a transaction Run a SQL statement, get results, Commit or rollback the transaction, Close the connection (note that in case of a connection pool, this will not «really» close the connection to the DB) We will practice that with the «BookRepository using JDBC» exercice. Transactions A transaction is a sequence of actions that must be considered as a whole: it can be committed or rollbacked but cannot be partially executed The default mode is autocommit: each query is committed as soon as it is send The Connection offers methods to manage the transaction mode: c.setautocommit(false); c.commit(); c.rollback(); 13

Interactions Possible interactions between transactions: Dirty reads (DR): Transactions can see uncommitted changes Nonrepeatable reads (NR): Transaction A reads a row Transaction B changes the row Transaction A reads the same row (changed) Phantom reads (PR): Transaction A reads all rows satisfying a where Transaction B inserts a row satisfying the where Transaction A rereads and see the new row Isolation levels Isolation level can be set on the Connection. The higher the level, the worse the performance, the lower the risk Isolation levels: 1. TRANSACTION_READ_UNCOMMITTED: DR, NR, PR 2. TRANSACTION_READ_COMMITTED: NR, PR 3. TRANSACTION_REPEATABLE_READ: PR 4. TRANSACTION_SERIALIZABLE: no interaction, each transaction totally locks all the data it needs 14

Isolation level The transaction isolation level can be modified using a method from the Connection object: conn.settransactionisolation( Connection.TRANSACTION_SERIALIZABLE); Savepoints Savepoints provide fine-grained control of transaction Each savepoint is represented by a named object: Savepoint sp1 = c.setsavepoint("sp1"); A transaction can be then rollbacked to a savepoint: c.rollback(sp1); A savepoint can be released: c.releasesavepoint(sp1); 15

Execute a SQL order Simple SQL order Use an instance of Statement created using the createstatement() method from the Connection: Statement stmt = connexion.createstatement(); When a Statement has been created, we can use it for different kind of orders (executequery or executeupdate) Several statements can be retrieved from the same connection to be used concurrently 16

Execution of the simple SQL order The method to call depends on the nature of the SQL request: Search (select) : executequery returns a ResultSet for processing lines one by one Modify data (update, insert, delete) or DDL orders (create table, ) : executeupdate returns the number of lines affected If we don t know at execution time the nature of the SQL order: execute Accessing data (SELECT) Statement stmt = conn.createstatement(); // rset contains returned lines ResultSet rset = stmt.executequery("select namee FROM emp"); // Get each line one by one while (rset.next()) System.out.println (rset.getstring(1)); First column has number 1 // or... (rset.getstring("namee")); stmt.close(); 17

ResultSet Interface executequery() returns an instance of ResultSet ResultSet will give access of lines returned by select At the beginning, ResultSet is positionned before the first line, we must call next()first next() returns true if the next line exists, and false otherwise Interface ResultSet When ResultSet is positionned on one line, the getxxx methods are used to retrieve data: getxxx(int columnnumber) getxxx(string columnname) XXX is the Java type of the value we are going to get. For example String, Int or Double For example, getint returns an int 18

JDBC/SQL Types (class Types) CHAR, VARCHAR, LONGVARCHAR BINARY, VARBINARY, LONGVARBINARY BIT, TINYINT, SMALLINT, INTEGER, BIGINT REAL, DOUBLE, FLOAT DECIMAL, NUMERIC DATE, TIME, TIMESTAMP BLOB, CLOB SQL3 Types Types ARRAY, DISTINCT, STRUCT, REF JAVA_OBJECT Matching types with getxxx() getstring() can retrieve nearly any SQL type However, edicated methods are recommended: CHAR and VARCHAR : getstring, LONGVARCHAR : getasciistream and getcharacterstream BINARY and VARBINARY : getbytes, LONGVARBINARY : getbinarystream REAL : getfloat, DOUBLE and FLOAT : getdouble DECIMAL and NUMERIC : getbigdecimal DATE : getdate, TIME : gettime, TIMESTAMP : gettimestamp 19

SQL Date and Java Date From java.util.date to java.sql.date, use the gettime() method: java.util.date date = new java.util.date(); java.sql.date datesql = new java.sql.date(date.gettime()); java.sql.time time = new Time(date.getTime()); java.sql.timestamp time = new Timestamp(date.getTime()); NULL value Statement stmt = conn.createstatement(); ResultSet rset = stmt.executequery( "SELECT namee, reward FROM emp"); while (rset.next()) { name= rset.getstring("namee"); commission = rset.getdouble("reward"); if (rset.wasnull()) System.out.println(name + ": no reward"); else System.out.println(name + " has reward: " + commission + " "); 20

Data modification (INSERT, UPDATE, DELETE) Statement stmt = conn.createstatement(); String city= "NICE"; int nblinesmodified = stmt.executeupdate( "INSERT INTO dept (dept, name, place) " + "VALUES (70, 'DIRECTION'," + "'" + city+ "')"); Do not forget the space here! Parameterized SQL order / PreparedStatement Most DB can analyse only once a request executed many times during a connection JDBC takes advantage of this with parameterized requests assiciated to instances of the PreparedStatement interface (that inherits from the Statement interface) 21

Example PreparedStatement pstmt = conn.preparestatement("update emp SET sal =?" + " WHERE name =?"); "?" indicates parameter locations This request will be executable with several parameter sets: (2500, DUPOND ), (3000, DURAND ), etc. Example with parameters PreparedStatement pstmt = conn.preparestatement( "UPDATE emp SET sal =? " + "WHERE nome =?"); for (int i=0; i<10; i++) { Starts at 1, not 0! pstmt.setdouble(1,employe[i].getsalaire()); pstmt.setstring(2, employe[i].getnom()); pstmt.executeupdate(); } 22

PreparedStatement and NULL value Use setnull(n, type) (type is one of the Types) Or pass null as a parameter if setxxx() has an Object as parameter (i.e. setstring, setdate, ) Advantages of PreparedStatement Faster if used several times, cacheable in case of using a connection pool, even after the connection has been closed on client side, Better portability as setxxx methods handle differences between DBRMs They protect agains SQL injection 23

Callable Statements Like PreparedStatements, CallableStatements are compiled but also grouped in the DBRM. They are used to call stored procedures, Less network access means better performance but as callable statements very often uses particularity of DBRMs, the portability is affected. Exemple of a stored procedure (Oracle) create or replace procedure augment (onedept in integer, percentage in number, begin cost out number) is select sum(sal) * percentage / 100 into cost from emp where dept = onedept; update emp set sal = sal * (1 + percentage / 100) 24

Creation of a callable statement CallableStatement inherits from PreparedStatement An instance of CallableStatement created by calling the preparecall method from Connection interface This method accepts as parameter a String that describes how to call the stored procedure and if it returns a value or not. Syntax for callable statements Not standardized, differs from one DBRM to another JDBC uses its own syntax to address this problem If stored procedure returns a value: {? = call procedure-name(?,?,...) } Returns no value: { call interface (?,?,...) } No parameter: { call interface } Driver will translate into DBRM syntax 25

Example CallableStatement cstmt = conn.preparecall("{? = call augment(?,?)}"); Execution of a callable statement 1. In and out parameters passed with setxxx Paramètres types for «out» and«in/out» set with registeroutparameter method 2. Execute with either executequery, executeupdate or execute, 3. «out», «in/out», and eventually the return value, collected with getxxx 26

Example CallableStatement csmt = conn.preparecall( "{ call augment(?,?,?) }"); // 2 digits after comma for third parameter csmt.registeroutparameter(3, Types.DECIMAL, 2); // Augment 2,5 % salaries from dept number 10 csmt.setint(1, 10); csmt.setdouble(2, 2.5); csmt.executequery(); // or execute() double cost = csmt.getdouble(3); System.out.println("Total R. Grin JDBC cost of augmentation: page 53 " + cost); MetaData JDBC allows getting informations about the data we retrieved with a SELECT (interface ResultSetMetaData), Also about the Database itself (interface DatabaseMetaData) Results differ from one DBRM to another 27

ResultSetMetaData ResultSet rs = stmt.executequery("select * FROM emp"); ResultSetMetaData rsmd = rs.getmetadata(); int nbcolumns = rsmd.getcolumncount(); for (int i = 1; i <= nbcolumns; i++) { String typecolumn = rsmd.getcolumntypename(i); String nomcolumn = rsmd.getcolumnname(i); System.out.println("Column " + i + " of name " + nomcolumn + " of type " + typecolumn); } DatabaseMetaData private DatabaseMetaData metadata; private java.awt.list listtables = new List(10);... metadata = conn.getmetadata(); String[] types = { "TABLE", "VIEW" }; ResultSet rs = metadata.gettables(null, null, "%", types); String tablesnames; while (rs.next()) { Joker for table and view names nomtable = rs.getstring(3); 28

Auto generated keys Execute (or prepare and execute) a statement specifying your will or the keys you want to retrieve: ps = c.preparestatement(" ", Statement.RETURN_GENERATED_KEYS); s.executeupdate( ); s.executeupdate(" ", {"Client_ID"}); Retrieve the keys: ResultSet r = s.getgeneratedkeys(); Batch updates To optimize performances, you may execute a set of updates at once You add your updates to the statement: s.addbatch("insert INTO t VALUES (1)"); For prepared and callable statements, you add a set of parameter values: ps.setint(1, 453); ps.addbatch(); The execution returns all the results: int[] tab = s.executebatch(); 29

More on ResultSet Interface providing methods for manipulating the result of executed queries ResultSet can have different functionality, based on their characteristics: Type Concurrency Holdability Type TYPE_FORWARD_ONLY: Forward only (only the next() method is available) Does not reflect changes in the database TYPE_SCROLL_INSENSITIVE: Scrollable Does not reflect changes in the database TYPE_SCROLL_SENSITIVE: Scrollable Reflects changes in the database 30

Concurrency CONCUR_READ ONLY: Default concurrency The ResultSet cannot be updated CONCUR_UPDATABLE: The ResultSet can be updated Holdability HOLD_CURSORS_OVER_COMMIT: The ResultSets are held open when a commit operation is performed on the connection CLOSE_CURSORS_AT_COMMIT: The ResultSets are closed when a commit operation is performed on the connection The default holdability is implementation defined 31

Retrieving information The ResultSet provides methods to move the cursor: first(), next(), previous(), last(), beforefirst(), afterlast() relative(int), absolute(int) The methods getxxx() retrieve the value in the column given by number or name to the Java type XXX The method wasnull indicates whether the latest retrieved value was null JDBC advanced: RowSet The RowSet extends the ResultSet It is the representation of the result of a SELECT query It holds all the connection information it needs (URL, user ) to be autonomous The RowSet is a Javabean, it generates events when a modification of its values occurs Good tutorial in french: http://java.developpez.com/faq/jdbc/?page= generalitesrowset#defrowset 32