Spring JDBC Dima Ionut Daniel. Contents What is Spring JDBC? Overview Spring JDBC Core SQL Exceptions Database Connection Batch Operations Handling BLOB/CLOB.

Slides:



Advertisements
Similar presentations
Connecting to Databases. connecting to DB DB server typically a standalone application Server runs on localhost for smaller sites –i.e. Same machine as.
Advertisements

Connecting to Databases. relational databases tables and relations accessed using SQL database -specific functionality –transaction processing commit.
Java Database Connectivity (JDBC). 2/24 JDBC (Java DataBase Connectivity) - provides access to relational database systems JDBC is a vendor independent.
Basic JDBC Celsina Bignoli What is JDBC Industry standard for database- connectivity between the Java language and a wide range of.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Introduction to SQL Programming Techniques.
Distributed Application Development B. Ramamurthy.
1 C. Shahabi Application Programming for Relational Databases Cyrus Shahabi Computer Science Department University of Southern California
Java Database Connectivity By: Abe Marji CS616. Agenda 1.Quick Review of Databases 2.What is SQL? 3.What is JDBC? 4.Advanced Functions of JDBC 5.Summary.
1 Lecture 29 More on JDBC Overview  Objectives of this lecture  JDBC and its Drivers  Connecting to Databases (Java’s Connection class)  Querying a.
JDBC. In This Class We Will Cover: What SQL is What ODBC is What JDBC is JDBC basics Introduction to advanced JDBC topics.
Introducing JavaBeans Lesson 2A / Slide 1 of 30 JDBC and JavaBeans Pre-assessment Questions 1.Which of the given symbols is used as a placeholder for PreparedStatement.
JDBC / ODBC JDBC is the java API that facilitate interaction of a java application with the DBMS. FIRST APPROACH:
Rice KRAD Data Layer JPA Design Eric Westfall July 2013.
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 Java Database Connection (JDBC) There are many industrial-strength DBMS's commercially available in the market. Oracle, DB2, and Sybase are just a few.
Think Possibility Integrating Web Applications With Databases.
UPortal Developers MIT August 2004 Persistence Strategy for uPortal 3 Mike DeSimone the r-smart group
Database Design for DNN Developers Sebastian Leupold.
CSCI 6962: Server-side Design and Programming JDBC Database Programming.
Beginning Databases with JDBC Mike Bradley Adapted from and notes by Kevin Parker, Ph.D.
Sayed Ahmed Computer Engineering, BUET, Bangladesh MSC, Computer Science, U of Manitoba, Canada
Hive Facebook 2009.
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,
Spring Data Access By, Srinivas Reddy.S
Connecting to Oracle using Java November 4, 2009 David Goldschmidt, Ph.D. David Goldschmidt, Ph.D.
Stored procedures1 Stored procedures and functions Procedures and functions stored in the database.
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.
MIS 3023 Business Programming II Professor: Akhilesh Bajaj Introduction to JDBC © Akhilesh Bajaj, All Rights Reserved.
JDBC Java and Databases. RHS – SOC 2 JDBC JDBC – Java DataBase Connectivity An API (i.e. a set of classes and methods), for working with databases in.
Andrew S. Budarevsky Adaptive Application Data Management Overview.
Spring Database Access Ojitha Kumanayaka Copyright © Virtusa, 2007.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Copyright  Oracle Corporation, All rights reserved. 7 Accessing a Database Using SQLJ.
Efficient RDF Storage and Retrieval in Jena2 Written by: Kevin Wilkinson, Craig Sayers, Harumi Kuno, Dave Reynolds Presented by: Umer Fareed 파리드.
Copyright © 2002 ProsoftTraining. All rights reserved. Building Database Client Applications Using JDBC 2.0.
JPA / HIBERNATE CSCI 6370 Nilayan Bhattacharya Sanket Sable.
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.
1 CS 430 Database Theory Winter 2005 Lecture 14: Additional SQL Topics.
12/6/2015B.Ramamurthy1 Java Database Connectivity B.Ramamurthy.
Li Tak Sing COMPS311F. Database programming JDBC (Java Database Connectivity) Java version of ODBC (Open Database Connectivity) ODBC provides a standard.
UNIT III - JDBC JDBC Overview – JDBC implementation – Connection class – Statements - Catching Database Results, handling database Queries. Networking–
DATABASE CONNECTIVITY TO MYSQL. Introduction =>A real life application needs to manipulate data stored in a Database. =>A database is a collection of.
Topic : Hibernate 1 Kaster Nurmukan. An ORM tool The problem fixed by ORM Advantage Hibernate Hibernate Basic –Hibernate sessionFactory –Hibernate Session.
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.
JDBC CS 260 Database Systems. Overview  Introduction  JDBC driver types  Eclipse project setup  Programming with JDBC  Prepared statements  SQL.
Access Databases from Java Programs via JDBC Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Introduction to Data Access with Spring.
Basics of JDBC Session 14.
Spring LDAP Dima Ionut Daniel.
Text 16 de mayo de 2009 Spring Framework Part III. Portable Service Abstractions Buenos Aires, June 2009.
Topic : Hibernate 1 Kaster Nurmukan. An ORM tool Used in data layer of applications Implements JPA.
Advanced Java Session 5 New York University School of Continuing and Professional Studies.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Introduction to Spring JDBC Simplifying.
Introduction to JDBC Instructor: Mohamed Eltabakh 1.
Intro to JDBC Joseph Sant Applied Computing and Engineering Sciences Sheridan ITAL.
Google Code Libraries Dima Ionut Daniel. Contents What is Google Code? LDAPBeans Object-ldap-mapping Ldap-ODM Bug4j jOOR Rapa jongo Conclusion Bibliography.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe.
JDBC.
Database: JDBC Overview
DEPTT. OF COMP. SC & APPLICATIONS
1st Semester, 2017 Sanghyun Park
Lec - 14.
ODBC, OCCI and JDBC overview
JDBC Database Management Database connectivity
Database JDBC Overview CS Programming Languages for Web Applications
Chapter 5: Advanced SQL Database System concepts,6th Ed.
Prof: Dr. Shu-Ching Chen TA: Sheng Guan
Java Database Connectivity
Data Access Layer (Con’t) (Overview)
Presentation transcript:

Spring JDBC Dima Ionut Daniel

Contents What is Spring JDBC? Overview Spring JDBC Core SQL Exceptions Database Connection Batch Operations Handling BLOB/CLOB Embedded Database Conclusions Bibliography

What is Spring JDBC? Spring JDBC is an abstraction framework for JDBC that provides easier access to datasources without all the exception handling and parsing of SQL fetch results.

Overview Usefull classes: JdbcTemplate, SimpleJdbcInsert, SimpleJdbcCall that requires a JDBC 2.0 compliant driver and some advanced features require a JDBC 3.0 driver. JdbcTemplate is the classic Spring JDBC approach and the most popular. SimpleJdbcInsert and SimpleJdbcCall optimize database metadata to limit the amount of necessary configuration. NamedParameterJdbcTemplate wraps a JdbcTemplate to provide named parameters instead of the traditional JDBC "?" placeholders. RDBMS Objects including MappingSqlQuery, SqlUpdate and StoredProcedure requires you to create reusable and thread-safe objects during initialization of your data access layer.

Overview (cont.) The Spring Framework’s JDBC abstraction framework consists of four different packages: core, datasource, object, and support. The org.springframework.jdbc.core package contains the JdbcTemplate class and its various callback interfaces, plus a variety of related classes. The subpackage named org.springframework.jdbc.core.simple contains the SimpleJdbcInsert and SimpleJdbcCall classes. The subpackage named org.springframework.jdbc.core.namedparam contains the NamedParameterJdbcTemplate class and the related support classes. The subpackage named org.springfamework.jdbc.datasource.embedded provides support for creating in- memory database instances using Java database engines such as HSQL and H2. The org.springframework.jdbc.support package provides SQLException translation functionality and some utility classes.

Spring JDBC Core The JdbcTemplate class is the central class in the JDBC core package and handles the creation and release of resources. The JdbcTemplate class executes SQL queries, update statements and stored procedure calls, performs iteration over ResultSets and extraction of returned parameter values. It also catches JDBC exceptions and translates them to the generic, more informative, exception hierarchy defined in te org.springframework.dao package. The PreparedStatementCreator or CallableStatementCreator callback interfaces creates a prepared/callback statement given a Connection provided. The RowCallbackHandler interface extracts values from each row of a ResultSet.

Spring JDBC Core (cont.) The NamedParamterJdbcTemplate needs a datasource. The RowMapper interface is used to map(iterate) a line from the ResultSet to domain bean.

Spring JDBC Core (cont.) The NamedParamterTemplate can be used for CRUD JDBC operations. KeyHolder/GeneratedKeyHolder can be used to retrieve auto generated values from the RDBMS.

Spring JDBC Core (cont.) The JdbcTemplate has an update() method that can perform insert, update, delete operations. Parameter values are usually provided as var args or alternatively as an object array.

Spring JDBC Core (cont.) The JdbcTemplate has the execute() method that can execute arbitrary SQL like DDL statements. It is heavily overloaded with variants taking callback interfaces, binding variable arrays Instances of the JdbcTemplate class are threadsafe once configured. This is important because it means that you can configure a single instance of a JdbcTemplate and then safely inject this shared reference into multiple DAOs (or repositories). The JdbcTemplate is stateful, in that it maintains a reference to a DataSource, but this state is not conversational state.

SQL Exceptions SQLExceptionTranslator is an interface to be implemented by classes that can translate between SQLExceptions and Spring’s own org.springframework.dao.DataAccessException Implementations can be generic (for example, using SQLState codes for JDBC) or proprietary (for example, using Oracle error codes) for greater precision. SQLErrorCodeSQLExceptionTranslator is the implementation of SQLExceptionTranslator that is used by default.

SQL Exceptions (cont.) The error code translations are based on codes held in a JavaBean type class called SQLErrorCodes. This class is created and populated by an SQLErrorCodesFactory which as the name suggests is a factory for creating SQLErrorCodes based on the contents of a configuration file named sql-error-codes.xml. This file is populated with vendor codes and based on the DatabaseProductName taken from the DatabaseMetaData.

Database Connection Spring obtains a connection to the database through a DataSource. Popular connection pool implementations are Apache Jakarta Commons DBCP and C3P0. Spring provides DriverManagerDataSource from org.springframework.jdbc.datasource package. BasicDataSource sample usage:

Database Connection (cont.) The SmartDataSource interface should be implemented by classes that can provide a connection to a relational database. The SingleConnectionDataSource class is an implementation of the SmartDataSource interface that wraps a single Connection that is not closed after each use. The DataSourceTransactionManager class is a PlatformTransactionManager implementation for single JDBC datasources. It binds a JDBC connection from the specified data source to the currently executing thread, potentially allowing for one thread connection per data source.

Batch Operations BatchPreparedStatementSetter can be used to pass parameters during a batch update method call. We use the getBatchSize method to provide the size of the current batch. We use the setValues method to set the values for the parameters of the prepared statement. This method will be called the number of times that you specified in the getBatchSize call.

Batch Operations (cont.) ParameterizedPreparedStatementSetter can also be used in the batch operations.

Handling BLOB/CLOB In Spring you can handle these large objects by using the JdbcTemplate directly. We can use also implementation of LobHandler for the actual management of the LOB data. The LobHandler provides access to a LobCreator class, through the getLobCreator method, used for creating new LOB objects to be inserted.

Embedded Database The org.springframework.jdbc.datasource.embedded package provides support for embedded Java database engines. Support for HSQL, H2, and Derby is provided natively. We can also use an extensible API to plug in new embedded database types and DataSource implementations. The EmbeddedDatabaseBuilder class provides a fluent API for constructing an embedded database programmatically.

Conclusions Spring JDBC is very useful to use when working with RDBMS. Spring JDBC is documented. Spring JDBC is easy to use.

Bibliography reference/html/jdbc.html