Oop L34
Oop L34
JDBC
JDBC
• JDBC stands for Java Database Connectivity.
• JDBC is a Java API to connect and execute the query with the database.
• It is a part of JavaSE (Java Standard Edition).
• JDBC API uses JDBC drivers to connect with the database. There are four types
of JDBC drivers:
• JDBC-ODBC Bridge Driver
• Native Driver (partially java driver)
• Network Protocol Driver (fully java driver)
• Thin Driver (fully java driver)
Before JDBC, ODBC API was the database API to connect and execute the query with the database. But, ODBC
API uses ODBC driver which is written in C language (i.e. platform dependent and unsecured). That is why
Java has defined its own API (JDBC API) that uses JDBC drivers (written in Java language).
• We can use JDBC API to access tabular data stored in any relational
database. By the help of JDBC API, we can save, update, delete and
fetch data from the database.
• The java.sql package contains classes and interfaces for JDBC API.
• A list of popular interfaces of JDBC API are : Driver interface,
Connection interface, Statement interface, PreparedStatement
interface, CallableStatement interface, ResultSet interface,
ResultSetMetaData interface, DatabaseMetaData interface, RowSet
interface.
• A list of popular classes of JDBC API are: DriverManager class, Blob
class, Clob class, Types class
Java Database Connectivity with 7 Steps
There are 7 steps to connect any java application with the database
using JDBC. These steps are as follows:
1. Import the package (import java. sql.*)
2. Load and register the Driver
3. Establish the connection
4. Create statement
5. Execute queries
6. Process Result
7. Close connection
Load and register the Driver
• For mysql the driver is, com.mysql.jdbc.Driver
• For loading we have to download a .jar file i.e. mysql-connector
• For registering the driver, we need a method forName() belongs to a
class Class. i.e.
Class.forName(com.mysql.jdbc.Driver)
Establish the connection
• The getConnection() method of DriverManager class is used to
establish connection with the database.
• Syntax of getConnection() method
public static Connection getConnection(String url)throws SQLException
public static Connection getConnection(String url,String name,String password)
throws SQLException
• Example to establish connection with the MySql database
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/myDB","root","root");
where jdbc is the API, mysql is the database, localhost is the server name on which
mysql is running, we may also use IP address, 3306 is the port number and myDB is
the database name. root is the username and root is the password.
Create statement
• The createStatement() method of Connection interface is used to
create statement. The object of statement is responsible to execute
queries with the database.
• Syntax of createStatement() method
public Statement createStatement()throws SQLException
• Example to create the statement object
Statement stmt=con.createStatement();
Execute queries and Process the result
• The executeQuery() method of Statement interface is used to execute queries to
the database.
• This method returns the object of ResultSet that can be used to get all the records
of a table.
• Syntax of executeQuery() method
public ResultSet executeQuery(String sql)throws SQLException
• Example to execute query
ResultSet rs=stmt.executeQuery("select * from emp");
while(rs.next()){
System.out.println(rs.getInt(1)+" "+rs.getString(2));
}
Close Connection
• By closing connection object, statement and ResultSet will be closed
automatically.
• The close() method of Connection interface is used to close the
connection.
• Syntax of close() method
public void close()throws SQLException
• Example to close connection
con.close();
Complete Program
import java.sql.*;
class MysqlCon{
public static void main(String args[]){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/myDB","root","root");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from emp");
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
con.close();
}catch(Exception e){ System.out.println(e);}
}
}