What Is JDBC
What Is JDBC
JDBC Driver
JDBC Driver is a software component that enables java application to interact with the database.
JDBC drivers:
1. JDBC-ODBC bridge driver
2. Native-API driver (partially java driver)
3. Network Protocol driver (fully java driver)
4. Thin driver (fully java driver)
The JDBC-ODBC bridge driver uses ODBC driver to connect to the database. The JDBC-ODBC bridge
method calls into the ODBC function calls. This is now discouraged because of thin driver.
In Java 8, the JDBC-ODBC Bridge has been removed.
Oracle does not support the JDBC-ODBC Bridge from Java 8. Oracle recommends that
you use JDBC drivers provided by the vendor of your database instead of the JDBC-
ODBC Bridge.
Advantages:
o easy to use.
o can be easily connected to any database.
Disadvantages:
o Performance degraded because JDBC method call is converted into the ODBC
function calls.
o The ODBC driver needs to be installed on the client machine.
2) Native-API driver
The Native API driver uses the client-side libraries of the database. The driver converts JDBC method
of the database API. It is not written entirely in java.
Advantage:
Disadvantage:
o No client side library is required because of application server that can perform
many tasks like auditing, load balancing, logging etc.
Disadvantages:
4) Thin driver
The thin driver converts JDBC calls directly into the vendor-specific database protocol. That is wh
driver. It is fully written in Java language.
Advantage:
Disadvantage:
Class.forName("com.mysql.jdbc.Driver");
DriverManager.registerDriver(): DriverManager is a Java inbuilt class
with a static member register. Here we call the constructor of the driver
class at compile time . The following example uses
DriverManager.registerDriver()to register the Oracle driver –
user – username from which your sql command prompt can be accessed.
password – password from which your sql command prompt can be
accessed.
con: is a reference to Connection interface.
url : Uniform Resource Locator.
3. Create a statement
Once a connection is established we can interact with the database. The
JDBCStatement, CallableStatement, and PreparedStatement interfaces
define the methods that enable us to send SQL commands and receive data
from our database.
Use of JDBC Statement is as follows:
Statement st = con.createStatement();
Here, con is a reference to Connection interface used in previous step .
4. Execute the query
Now comes the most important part i.e executing the query. Query here is an
SQL Query . Now we know we can have multiple types of queries. Some of
them are as follows:
Query for updating / inserting table in a database.
Query for retrieving data .
The executeQuery() method of Statement interface is used to execute
queries of retrieving values from the database. This method returns the
object of ResultSet that can be used to get all the records of a table.
The executeUpdate(sql query) method of Statement interface is used to
execute queries of updating/inserting .
int m = st.executeUpdate(sql);
if (m==1)
System.out.println("inserted successfully : "+sql);
else
System.out.println("insertion failed");
Here sql is sql query of the type String
5.Close the connections
By closing connection, objects of Statement and ResultSet will be closed
automatically. The close() method of Connection interface is used to close
the connection.
Example :
con.close();
Example
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/sonoo","root","root");
//here sonoo is database name, root is username and password
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);}
}
}
ResultSet interface
The object of ResultSet maintains a cursor pointing to a row of a table. Initially, cursor points
to before the first row.
By default, ResultSet object can be moved forward only and it is not updatable.
But we can make this object to move forward and backward direction by passing either
TYPE_SCROLL_INSENSITIVE or TYPE_SCROLL_SENSITIVE in createStatement(int,int) method
as well as we can make this object as updatable by: