JDBC Tutorial

The Java Database Connectivity (JDBC) API enable Java application to interact with a database. In this tutorials, it provides many step by step examples on using JDBC Statement, PreparedStatement , CallableStatement , JDBC Transaction and how to integrate with other frameworks like Spring.
Happy learning JDBC :)
Quick Start
Some quick guides to show how JDBC interact with databases like MySQL, Oracle and PostgreSQL.
- Connect to MySQL with JDBC driver
Here’s an example to show you how to connect to MySQL database via JDBC driver. - Connect to PostgreSQL with JDBC driver
Here is an example to show you how to connect to PostgreSQL database with JDBC driver. - Connect to Oracle DB via JDBC driver
Here’s an example to show you how to connect to Oracle database via JDBC driver.
JDBC & Statement
The “Statement” interface is used to execute a simple SQL statement with no parameters. For create, insert, update or delete statement, uses “Statement.executeUpdate(sql)“; select query, uses “Statement.executeQuery(sql)“.
- JDBC Statement example – Create a table
Example to create a table in database. - JDBC Statement example – Insert a record
Example to insert a record into table. - JDBC Statement example – Update a record
Example to update a record in table. - JDBC Statement example – Delete a record
Example to delete a record from a table. - JDBC Statement example – Select list of the records
Example to select the entire records from a table, and iterate the records via a ResultSet object. - JDBC Statement example – Batch Update
Example to insert records in batch process, via JDBC Statement.
JDBC & PreparedStatement
The “PreparedStatement” interface is extended “Statement”, with extra feature to send a pre-compiled SQL statement with parameters. For create, insert, update or delete statement, uses “PreparedStatement.executeUpdate(sql)“; select query, uses “PreparedStatement.executeQuery(sql)“.
- JDBC PreparedStatement example – Create a table
Example to create a table in database. - JDBC PreparedStatement example – Insert a record
Example to insert a record into table. - JDBC PreparedStatement example – Update a record
Example to update a record in table. - JDBC PreparedStatement example – Delete a record
Example to delete a record from a table. - JDBC PreparedStatement example – Select list of the records
Example to select records from a table, and iterate the records via a ResultSet object. - JDBC PreparedStatement example – Batch Update
Example to insert records in batch process, via JDBC PreparedStatement.
JDBC & Stored Procedure
JDBC CallableStatement and Stored Procedure, IN, OUT, CURSOR examples.
- JDBC CallableStatement – Stored Procedure IN parameter example
Stored procedure IN parameter via JDBC CallableStatement. - JDBC CallableStatement – Stored Procedure OUT parameter example
Stored procedure OUT parameter via JDBC CallableStatement. - JDBC CallableStatement – Stored Procedure CURSOR example
Stored procedure CURSOR parameter via JDBC CallableStatement.
JDBC Transaction example
JDBC Trasaction how to :
- JDBC Transaction example
Example to show you how to use JDBC Transaction.
JDBC Integration example
Integrate JDBC with other frameworks.
- JSF 2.0 + JDBC integration example
Here’s a guide to show you how to integrate JSF 2.0 with database via JDBC. In this example, we are using MySQL database and Tomcat web container. - Spring + JDBC integration example
Many examples to show how to integarte Spring with JDBC.
FAQ
Reference
- http://en.wikipedia.org/wiki/JDBC_driver
- http://www.oracle.com/technetwork/java/javase/tech/index-jsp-136101.html
- http://download.oracle.com/javase/6/docs/technotes/guides/jdbc/
- http://java.sun.com/developer/onlineTraining/Programming/JDCBook/jdbc.html
- http://download.oracle.com/javase/6/docs/api/java/sql/CallableStatement.html
- http://docsrv.sco.com/JDK_guide/jdbc/getstart/callablestatement.doc.html
- http://onjava.com/pub/a/onjava/2003/08/13/stored_procedures.html
- http://www.oradev.com/ref_cursor.jsp

i want subscription with jdbc code pls help
Hi Mr mkyong, your post’s very awesome and really helpful
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.
i want all database data retrive in the xml file code
i want jdbc with xml programs…..
where is the search box in this website?
where is the search box inthis site?
all the tutorials are very helpful for me and all others
can u upload tutorials for servlets ans jsp
thank you mr.yong
Good article
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.
thanks for u tutorial
i like this posting
Hi mkyong, very good tutorials! Thaks a lot for making java so simple =)
Can you make a tutorial about using trigger??
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.
thnx mr. yong this article of your’s abt JDBC helped me a lot
thnx mr. yong this article of your’s abt JDBC helped me a lot
Hi Yong,
can you please suggest a good JSP tutorial?
thanks.
nice article…….. keep it up,very helpful.
an article in JFreeChart and JavaMail :) plz
Very nice article
An article about deadlocks would be appreciated.