About the Tutorial

 About the Tutorial

JDBC API is a Java API that can access any kind of tabular data, especially data stored in a Relational Database. JDBC works with Java on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX.

Audience

This tutorial is designed for Java programmers who would like to understand the JDBC framework in detail along with its architecture and actual usage.

Prerequisites

Before proceeding with this tutorial, you should have a good understanding of Java programming language. As you are going to deal with RDBMS, you should have prior exposure to SQL and Database concepts.

Copyright & Disclaimer

Copyright 2015 by Tutorials Point (I) Pvt. Ltd. All the content and graphics published in this e-book are the property of Tutorials Point (I) Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish any contents or a part of contents of this e-book in any manner without written consent of the publisher. We strive to update the contents of our website and tutorials as timely and as precisely as possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt. Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our website or its contents including this tutorial. If you discover any errors on our website or in this tutorial, please notify us at contact@

i

Table of Contents

About the Tutorial .....................................................................................................................................i Audience .................................................................................................................................................... i Prerequisites .............................................................................................................................................. i Copyright & Disclaimer ..............................................................................................................................i Table of Contents......................................................................................................................................ii

1. INTRODUCTION.....................................................................................................................1

What is JDBC? ...........................................................................................................................................1 Pre-Requisite ............................................................................................................................................1 JDBC Architecture .....................................................................................................................................1 Common JDBC Components......................................................................................................................2 The JDBC 4.0 Packages ..............................................................................................................................3

2. SQL SYNTAX ..........................................................................................................................4

Create Database .......................................................................................................................................4 Drop Database ..........................................................................................................................................4 Create Table..............................................................................................................................................4 Drop Table ................................................................................................................................................5 INSERT Data ..............................................................................................................................................5 SELECT Data ..............................................................................................................................................5 UPDATE Data ............................................................................................................................................6 DELETE Data..............................................................................................................................................6

3. ENVIRONMENT .....................................................................................................................8

Install Java ................................................................................................................................................8 Install Database ........................................................................................................................................8 Install Database Drivers ............................................................................................................................9

ii

Set Database Credential............................................................................................................................9 Create Database .......................................................................................................................................9 Create Table............................................................................................................................................10 Create Data Records ...............................................................................................................................11

4. SAMPLE CODE.....................................................................................................................12

Creating JDBC Application.......................................................................................................................12 Sample Code ...........................................................................................................................................12

5. DRIVER TYPES......................................................................................................................16

What is JDBC Driver?...............................................................................................................................16 JDBC Drivers Types..................................................................................................................................16 Which Driver should be Used? ................................................................................................................19

6. CONNECTIONS .................................................................................................................... 20

Import JDBC Packages.............................................................................................................................20 Register JDBC Driver ...............................................................................................................................20 Database URL Formulation .....................................................................................................................22 Create Connection Object .......................................................................................................................22 Closing JDBC Connections .......................................................................................................................24

7. STATEMENTS ...................................................................................................................... 25

The Statement Objects ...........................................................................................................................25 The PreparedStatement Objects .............................................................................................................29 Prepare - Example Code ..........................................................................................................................31 The CallableStatement Objects ...............................................................................................................34

8. RESULT SETS ....................................................................................................................... 40

Type of ResultSet ....................................................................................................................................40 Concurrency of ResultSet ........................................................................................................................41

iii

Navigating a Result Set ...........................................................................................................................41 Navigate - Example Code ........................................................................................................................43 Viewing a Result Set ...............................................................................................................................46 Viewing - Example Code..........................................................................................................................47 Updating a Result Set..............................................................................................................................51 Updating - Example Code........................................................................................................................52

9. DATA TYPES ........................................................................................................................ 58

Date & Time Data Types..........................................................................................................................60 Handling NULL Values .............................................................................................................................62

10. TRANSACTIONS ...................................................................................................................63

Commit & Rollback .................................................................................................................................63 Commit - Example Code ..........................................................................................................................64 Using Savepoints.....................................................................................................................................68 Savepoints - Example Code .....................................................................................................................69

11. EXCEPTIONS ........................................................................................................................ 74

SQLException Methods ...........................................................................................................................74

12. BATCH PROCESSING............................................................................................................78

Batching with Statement Object .............................................................................................................78 Batching - Example Code.........................................................................................................................79 Batching with PrepareStatement Object.................................................................................................83 Batching - Example Code.........................................................................................................................85

13. STORED PROCEDURE ..........................................................................................................90

Creating CallableStatement Object .........................................................................................................90 Closing CallableStatement Object: ..........................................................................................................92 JDBC SQL Escape Syntax..........................................................................................................................92

iv

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download