Java JDBC Tutorials

jdbc logo

The Java Database Connectivity (JDBC) API enables Java application to interact with database.

1. Getting Started

2. Statement

This Statement has no cache, good for simple and static SQL statements like CREATE or DROP. In Statement, the way we construct the condition or parameters in SQL is prone to SQL injection, remember escape the quotes and special characters.

  • statement.execute(sql) – Normally for DDL like CREATE or DROP
  • statement.executeUpdate(sql) – Normally for DML like INSERT, UPDATE, DELETE
  • statement.executeQuery(sql) – Run SELECT query and return a ResultSet
  • statement.executeBatch() – Run SQL commands as a batch

Articles:

3. PreparedStatement

PreparedStatement extends Statement to provide better performance by precompiled and cached the SQL statement, good for SQL statement that need to execute multiple times. Furthermore, it provides many setXxx() to protect SQL injection by escaping the quotes and special characters.

  • preparedStatement.execute() – Normally for DDL like CREATE or DROP
  • preparedStatement.executeUpdate() – Normally for DML like INSERT, UPDATE, DELETE
  • preparedStatement.executeQuery() – Run SELECT query and return a ResultSet
  • preparedStatement.executeBatch() – Run SQL commands as a batch

Articles:

4. CallableStatement

CallableStatement extends PreparedStatement, for executing stored procedures or functions from the database.

  • conn.prepareCall(sql)

Oracle database

PostgreSQL

5. Transaction


conn.setAutoCommit(false); // default true
// start transaction block

// SQL statements

// end transaction block
conn.commit();
conn.setAutoCommit(true);

6. Spring JDBC Database Access

JdbcTemplate examples.

FAQs

References

Oracle

MySQL

PostgreSQL

mkyong

Founder of Mkyong.com, passionate Java and open-source technologies. If you enjoy my tutorials, consider making a donation to these charities.

28 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Ng?c Kh??ng
8 years ago

Thank you very much :)))

Dan de Man
9 years ago

You’re my favorite person in the world!

anonim
15 years ago

An article about deadlocks would be appreciated.

Bhawna
3 years ago

Thank you very much for this valuable information.

vinoth
10 years ago

sir how to compare two images in java

AboAbdullah
12 years ago

Thank You for very useful tutorial
I have question related to JDBC can you help me with it ?
I want to draw bezier cubic curves by retrieving data from MySQL
database and then display the shape on JFrame I have table has 3 columns
(ID, ObjectID, Points) Points column is coordinates of cubic curves the statement is

SELECT * FROM DB.TABNAME WHERE OBJECTID=1;

ObjectID=1 has 11 ID’s, each ID contains a shape if i combine all ID`s together will form composite shape
How can i retrieve data from MySQL database, that data are path to draw bezier cubic curves in java GUI?

vittal
12 years ago

hi mkyong, its really awesome, a very good site to learn……
thnq u….

vishwas
12 years ago

hi yong

Can you tell me how to get list of database in Oracle as well as MySql using java?
Like SQLYog IDE which lists all databases in MySql.
I want the same thing. I want to connect to any Database server and fetch list of database in it.

panji
12 years ago

thansk for u share sir,

nice post

venkat
13 years ago

i want subscription with jdbc code pls help

edo
13 years ago

Hi Mr mkyong, your post’s very awesome and really helpful

Ashabasa
13 years ago

Hello

I’m creating a web service on Java. And I need it to connect with many dbms (Oracle, MySql…). Is there a way to do that ?
PS :I’m working with hibernate.

venkat
13 years ago

i want all database data retrive in the xml file code

venkat
13 years ago

i want jdbc with xml programs…..

venkat
13 years ago

where is the search box in this website?

venkat
13 years ago

where is the search box inthis site?

srinivasa reddy
13 years ago

all the tutorials are very helpful for me and all others

can u upload tutorials for servlets ans jsp

thank you mr.yong

sexy back
13 years ago

Good article

Krishna Kumar
13 years ago

hi sir,could help me please,actually we are using prepared statement for speed up and much more,ok but i want to perform single operation not bulk,which one is better and fast general or prepared stament(and also in terms of sql injection).thanx a lot sir.

panji
13 years ago

thanks for u tutorial

i like this posting

Crossroad
13 years ago

Hi mkyong, very good tutorials! Thaks a lot for making java so simple =)

Can you make a tutorial about using trigger??

masood
13 years ago

hi sir,

can u explain me, how can we get database table address in java program., pls help me out sir, this question asked in interview for my friend.

saroj
13 years ago

thnx mr. yong this article of your’s abt JDBC helped me a lot

jainath Jaiswal
13 years ago
Reply to  saroj

thnx mr. yong this article of your’s abt JDBC helped me a lot

Saraswati
13 years ago

Hi Yong,
can you please suggest a good JSP tutorial?
thanks.

Raj
14 years ago

nice article…….. keep it up,very helpful.

spaceinvader
15 years ago

an article in JFreeChart and JavaMail 🙂 plz

Raj
13 years ago
Reply to  spaceinvader

Very nice article