0% found this document useful (0 votes)
39 views14 pages

JDBC

The document discusses Java Database Connectivity (JDBC), which defines interfaces and classes for connecting Java applications to databases and executing SQL statements. It outlines the basic steps for using JDBC, which include loading a JDBC driver, establishing a connection, preparing and executing SQL statements, processing result sets, and closing the connection. Key classes and interfaces like Statement, PreparedStatement, CallableStatement, ResultSet, and ResultSetMetaData are also introduced.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views14 pages

JDBC

The document discusses Java Database Connectivity (JDBC), which defines interfaces and classes for connecting Java applications to databases and executing SQL statements. It outlines the basic steps for using JDBC, which include loading a JDBC driver, establishing a connection, preparing and executing SQL statements, processing result sets, and closing the connection. Key classes and interfaces like Statement, PreparedStatement, CallableStatement, ResultSet, and ResultSetMetaData are also introduced.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

JDBC

A.MAHENDIRAN
ASST.PROFESSOR/MCA
JDBC Introduction

The JDBC ( Java Database Connectivity) API defines


interfaces and classes for writing database
applications in Java by making database connections
Using JDBC you can send SQL, PL/SQL statements
to almost any relational database.
JDBC is a Java API for executing SQL statements
and supports basic SQL functionality
can embed SQL inside Java code
JDBC Architecture
JDBC Basics - Java Database Connectivity Steps

Load JDBC driver


2. Establish connection to database
3. Prepare SQL statement
4. Execute the statement and process
results
5. Close connection
Sql package

Before you can create a java jdbc connection to the


database, you must first import the java.sql
package.
import java.sql.*;     The star ( * ) indicates that all
of the classes in the package java.sql are to be
imported.
1. Loading a database driver, 

load the driver class by calling Class.forName() with


the Driver class name as an argument
A client can connect to Database Server through
JDBC Driver
Since most of the Database servers support ODBC
driver therefore JDBC-ODBC Bridge driver is
commonly used
EXample

Try
{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //
Or any other driver}
catch(Exception x)
{
System.out.println( "Unable to load the driver class!" );
}
2. Establish Connection

The JDBC DriverManager class defines objects which can


connect Java applications to a JDBC driver
DriverManager class manages the JDBC drivers that are
installed on the system
Its getConnection() method is used to establish a
connection to a database.
An application can have one or more connections with a
single database, or it can have many connections with
different databases
Connection con =
DriverManager.getConnection(dsn, id, passwd);
3. Prepare SQL statement

Once a connection is obtained we can interact with


the database.
Connection interface defines methods for
interacting with the database via the established
connection.
To execute SQL statements, you need to instantiate
a Statement object from your connection object by
using the createStatement() method.
Statement statement =
dbConnection.createStatement();
Three kinds of Statements

Statement: Execute simple sql queries without parameters. 


    Statement createStatement()
        Creates an SQL Statement object.

Prepared Statement: Execute precompiled sql queries with


or without parameters.
    PreparedStatement prepareStatement(String sql)
        returns a new PreparedStatement object.
PreparedStatement objects are precompiled SQL statements.

Callable Statement: Execute a call to a database stored


procedure.
    CallableStatement prepareCall(String sql)
        returns a new CallableStatement object.  CallableStatement
objects are SQL stored procedure call statements.
4. Execute the statement and process results

 Statement interface defines methods that are used


to interact with database via the execution of SQL
statements.
The Statement class has three methods for executing
statements: executeQuery(), executeUpdate(),
and execute().
Three Methods

For a SELECT statement, the method to use is


executeQuery .
For statements that create or modify tables, the
method to use is executeUpdate.
Note: Statements that create a table, alter a table,
or drop a table are all examples of DDL statements
and are executed with the method executeUpdate.
execute() executes an SQL statement that is written
as String object.
ResultSet

ResultSet provides access to a table of data generated by


executing a Statement. The table rows are retrieved in
sequence. A ResultSet maintains a cursor pointing to its
current row of data. The next() method is used to
successively step through the rows of the tabular results.
    ResultSetMetaData Interface holds information on the
types and properties of the columns in a ResultSet. It is
constructed from the Connection object.
Connection con = DriverManager.getConnection(dsn,
id, passwd);
Statement stmt=con.createStatement();
String query="select * from products";
ResultSet rs=stmt.executeQuery(query);
5. Close Connection

Close the connection the connection when all


database operations are performed. ¨This
will also close all statement and result set
objects,
It is a good habit to close connection
explicitly for performance reasons.

You might also like