About the Tutorial
[Pages:31]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
d, t, ts Keywords .....................................................................................................................................92 escape Keyword......................................................................................................................................93 fn Keyword .............................................................................................................................................93 call Keyword ...........................................................................................................................................94 oj Keyword..............................................................................................................................................94
14. STREAMING DATA...............................................................................................................95 15. CREATE DATABASE............................................................................................................100
Required Steps......................................................................................................................................100 Sample Code .........................................................................................................................................100
16. SELECT DATABASE.............................................................................................................103
Required Steps......................................................................................................................................103 Sample Code .........................................................................................................................................103
17. DROP DATABASE...............................................................................................................106
Required Steps......................................................................................................................................106 Sample Code .........................................................................................................................................106
18. CREATE TABLES ................................................................................................................. 109
Required Steps......................................................................................................................................109 Sample Code .........................................................................................................................................109
19. DROP TABLES .................................................................................................................... 112
Required Steps......................................................................................................................................112 Sample Code .........................................................................................................................................112
20. INSERT RECORDS...............................................................................................................115
Required Steps......................................................................................................................................115 Sample Code .........................................................................................................................................115
v
21. SELECT RECORDS .............................................................................................................. 118
Required Steps......................................................................................................................................118 Sample Code .........................................................................................................................................118
22. UPDATE RECORDS.............................................................................................................121
Required Steps......................................................................................................................................121 Sample Code .........................................................................................................................................121
23. DELETE RECORDS .............................................................................................................. 125
Required Steps......................................................................................................................................125 Sample Code .........................................................................................................................................125
24. WHERE CLAUSE.................................................................................................................129
Required Steps......................................................................................................................................129 Sample Code .........................................................................................................................................129
25. LIKE CLAUSE ...................................................................................................................... 133
Required Steps......................................................................................................................................133 Sample Code .........................................................................................................................................133
26. SORTING DATA..................................................................................................................137
Required Steps......................................................................................................................................137 Sample Code .........................................................................................................................................137
vi
1. JDBC INTRODUCTION
What is JDBC?
JDBC stands for Java Database Connectivity, which is a standard Java API for databaseindependent connectivity between the Java programming language, and a wide range of databases. The JDBC library includes APIs for each of the tasks mentioned below that are commonly associated with database usage.
Making a connection to a database. Creating SQL or MySQL statements. Executing SQL or MySQL queries in the database. Viewing & Modifying the resulting records. Fundamentally, JDBC is a specification that provides a complete set of interfaces that allows for portable access to an underlying database. Java can be used to write different types of executables, such as: Java Applications Java Applets Java Servlets Java ServerPages (JSPs) Enterprise JavaBeans (EJBs). All of these different executables are able to use a JDBC driver to access a database, and take advantage of the stored data. JDBC provides the same capabilities as ODBC, allowing Java programs to contain databaseindependent code.
Pre-Requisite
Before moving further, you need to have a good understanding of the following two subjects: Core JAVA Programming SQL or MySQL Database
7
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- java programming examples
- a step by step guide to using jdbc with eclipse step 1
- about the tutorial
- learning computer programming using java with 101 examples
- jdbc rip tutorial
- simple jdbc with spring 2 o reilly media
- simple tutorial for using jdbc
- application programming guide and reference for java
- jjddbbcc ddaattaabbaassee
Related searches
- learning about the brain for kids
- all about the winter solstice
- facts about the equinox
- quotes about the meaning of life
- bad things about the internet
- essay about the importance of learning english
- information about the roman empire
- interesting facts about the workplace
- interesting facts about the uae
- fun facts about the uae
- sermon about the family
- essay about the importance of learning en