JDBC stands for Java Database Connectivity. It is an API for the Java programming language. It allows the client to access the database and also establishes how the client may do so. It can work on a number of operating systems or platforms, like Windows, Mac, etc. It is basically a connection between the database and the application.
If you want to interact with database using c & c++, you need to use database specific libraries in your application directly. Later, if you want to migrate the database, you need to rewrite the entire application using real database specific libraries. This increases the maintenance of the application.
To avoid this, Microsoft has introduced ODBC Driver ODC (open database connectivity). Given below are some points related to the ODBC Driver.
With the ODBC Driver, you don’t need to use the application directly, because ODBC itself contain various database vendor specific libraries.
Your application now contacts the ODBC Driver instead of using database specific libraries directly. This reduces maintenance issues.
But ODBC has a limitation, i.e., the ODBC setup is available only on the Windows OS and neither has it shown good performance.
To avoid these limitations and to provide a uniform method to interact with any database, Sun has provided us with the JDBC API and JDBC Drivers.
Here are the steps to write a JDBC program:
package com.javabykiran.JDBC;
import java.sql.*;
public class DBUtil {
public static Connection getOracleConnection() {
Connection con = null;
try {
// 1.Load the Driver class
Class.forName ("oracle.jdbc.driver.oracle.Driver");
// 2.Establish the connection
con = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521.XE","system", "jbk");
} catch (Exception e) { e.printStackTrace();
}
return con;
}
public static Connection getMySQLConnection() {
Connection con = null;
try {
// 1. Load the Driver class
Class.forName("com.mysql.jdbc.Driver");
// 2. Establish the connection
con = DriverManager.getConnection(
"jdbc:mySQL://localhost:3306/jbkdb", "root", "root");
} catch (Exception e) { e.printStackTrace();
}
return con;
}
public static void cleanup(Connection con, Statement st, ResultSet rs) {
try {
// 7.Release the Resources
if (rs != null)
rs.close();
if (st != null)
st.close();
if (con != null)
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void cleanup(Connection con, Statement st) {
try {
// 7.Release the Resources
if (st != null)
st.close();
if (con != null)
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.javabykiran.JDBC;
import java.sql.*;
class JDBCLab2 {
public static void main(String as[]) {
int sid = Integer.parseInt(as[0]);
String sn = as[1];
String em = as[2];
long ph = Long.parseLong(as[3]);
String ci = as[4];
double fee = Double.parseDouble(as[5]);
Connection con = null;
Statement st = null;
ResultSet rs = null;
try {
con= DBUtil.getOracleConnection();
// 3.create the Required JDBC statement
st = con.createStatement();
// 4.prepare the Required SQL statement
String sql = null;
System.out.println(sql);
// 5.submit the sql statement to DB
int x = st.executeUpdate(sql);
// 6.process the Results
if (x == 1) {
System.out.println("Record is inserted");
} else {
System.out.println("sorry,Record is not inserted");
}
// 4.prepare the Required SQL statement
String sql="select * from b27 students";
// 5 submit the SQL statement to DB.
rs = st.executeQuery(sql);
// 6.process the Results.
while (rs.next()) {
sid = rs.getInt(1);
sn = rs.getString(2);
em = rs.getString(3);
ph = rs.getLong(4);
ci = rs.getString(5);
fee = rs.getDouble(6);
System.out.println("");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.cleanup(con, st, rs);
}
}
}
There are three types of JDBC statements. They are:
The executeQuery()method can be used to submit the selected SQL statement to the SQL Engine.
This method returns the Resultset object which contains the number of records returned by the given selected SQL statement.
The executeUpdate() method can be used to submit insert, update, and delete SQL statement to SQL Engine.
This method returns the integer number which represents the number of record affected by the given SQL statement.
=5ms+5ms+5ms+5ms
=20ms
1000 Queries =1000*20
=20,000
PreparedStatement is an interface available in java.sql package and it extends the Statement interface.
The PreparedStatement object can be created using one of the following methods of connection interface:
Once the preparedStatement object is created, you can call one of the following methods of preparedStatement interface:
Using one preparedStatement object, you can submit only one type of SQL statement.
When you submit the SQL statement to SQL Engine using preparedStatement object, the SQL statement will be compiled only once the first time, and will be executed every time without compilation.
For first query, time taken for executing prepared statement query will be:
1Query = 1+2+3+4
=5ms+5ms+5ms+5ms
=20ms
For the second query onwards, time taken for executing prepared statement query will be:
1Query = 1+3+4
5+5+5 = 15ms
1000-15000 = 5000ms (saving time)
package com.javabykiran.JDBC;
import java.sql.*;
class JDBCLab {
public static void main(String[] as) {
int sid = Integer.parseInt(as[0]);
String sn = as[1];
String em = as[2];
long ph = Long.parseLong(as[3]);
String ci = as[4];
double fee = Double.parseDouble(as[5]);
Connection con = null;
PreparedStatement ps1 = null;
PreparedStatement ps2 = null;
ResultSet rs = null;
try {
con = DBUtil.getMySQLConnection();
ps1 = con.prepareStatement("insert into b27students
values(?,?,?,?,?,?)");
ps1.setInt(1, sid);
ps1.setString(2, sn);
ps1.setString(3, em);
ps1.setLong(4, ph);
ps1.setString(5, ci);
ps1.setDouble(6, fee);
int x = ps1.executeUpdate();
if (x == 1) {
System.out.println("Record is inserted");
} else {
System.out.println("sorry, Record is not inserted");
}
ps2 =con.prepareStatement("select * from jbkStudentns");
rs = ps2.executeQuery();
while (rs.next()) {
sid = rs.getInt(1);
sn = rs.getString(2);
em = rs.getString(3);
ph = rs.getLong(4);
ci = rs.getString(5);
fee = rs.getDouble(6);
System.out.println(" " + sid + "!t" + sn + "!t" + em
+ "!t" + ph + "!t" + ci + "!t" + fee);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.cleanup(con, ps1, rs);
DBUtil.cleanup(con, ps2, rs);
}
}
}
The CallableStatement is an interface available in java.sql package and is an extension of the preparedStatement interface.
The CallableStatement object can be created using one of the following methods of connection interface:
Once callableStatement object is created, you can call one of the following methods of callableStatement interface:
CallableStatement is mainly used to execute stored procedures running in the database.
Using one CallableStatement object. You can submit only one call one stored procedure.
Stored procedure is a set of pre-compiled procedures i.e, when you create the procedure, it will be compiled and stored in the database memory. When you call the procedure it will be executed directly.
Example that invokes stored procedure with IN parameters:
/*
Create table hello (
a number(4),
b number(4),
tot number(8)
);
Create or replace procedure p1 (a in number, b in number)
as
tot number(8);
begin
tot = a+b:
insert into hello values (a,b,tot);
end;
*/
package com.javabykiran.JDBC;
import java.sql.CallableStatement;
import java.sql.Connection;
public class JDBCLab5 {
public static void main(String as[]) {
int a = Integer.parseInt(as[0]);
int b = Integer.parseInt(as[1]);
Connection con = null;
CallableStatement cs = null;
try {
con = DBUtil.getOracleConnection();
cs = con.prepareCall("{callp1(?,?)}");
cs.setInt(1, a);
cs.setInt(2, b);
cs.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.cleanup(con, cs);
}
}
}
Example that invokes stored procedure with IN and OUT parameters:
*/
create or replace procedure p2(a IN number, b IN number,
tot OUT number, dif OUT number, mul OUT number, div OUT number)
as
begin
tot:=a=b;
dif:=a-b;
mul:=a*b;
div:=a/b;
end;
*/
package com.javabykiran.JDBC;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.Types;
public class JDBCLab6 {
public static void main(String[] args) {
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
Connection con = null;
CallableStatement cs = null;
try {
con = DBUtil.getOracleConnection();
cs = con.prepareCall("{call p2(?,?,?,?,?,?(}");
cs.setInt(1, a);
cs.setInt(2, b);
cs.registerOutParameter(3, Types.INTEGER);
cs.registerOutParameter(4, 4);
cs.registerOutParameter(5, Types.INTEGER);
cs.registerOutParameter(6, 4);
cs.executeUpdate();
int c = cs.getInt(3);
int d = cs.getInt(4);
int e = cs.getInt(5);
int f = cs.getInt(6);
System.out.println(c);
System.out.println(d);
System.out.println(e);
System.out.println(f);
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.cleanup(con, cs);
}
}
}
It is an interface available in java.sql package, which provides various useful methods to get information about your database, which you are linked to.
You can create database metadata object as follows:
DatabaseMetaData dbmd=con.getMetaData();
By using MetaData we can get any information about Database.
ResultsetMetaData is an interface available in java.sql package, which gives the information about Resultset object, like number of columns available in Resultset, names of the columns name of the table from where column is fetched etc.
You can create the ResultsetMetaData object as follows:-
ResultsetMetaData rsmd=rs.getMetaData();
Oracle has provided subclasses for these interface in oracle.jdbc.driver package called oracleDatabaseMetaData and oracleResultsetMetaData.
MySQL has provided sub classes for these interfaces in com.mysql.jdbc package called DatabaseMetaData and ResultsetMetaData.
Question.
Answer.
class DriverManager {
static Connection getconnection(url,un,pw)
{
}
}
It takes the url and checks whether driver class is loaded for this url.
If not loaded then it gives this error:NosuitableDriver error
If loaded then
It creates the object of subclass of connection interfaces related vendor oracleConnection/MySQLconnection class.
Given below are vendor implementations:
Class OracleConnection implements java.sql.Connection {
Statement createStatement ()
{
return new oracleStatement ();
}
}
Class MySQLConnection implements java.sql.connection {
Statement createStatement ()
{
return new MySQLstatement();
}
}
Question.
Answer. Depending on the url you are passing as the parameters to getConnection() method, the corresponding database connection will be established.
Sample code:
class.forName("oracle.jdbc.driver.OracleDriver");
class.forName("com.MySQL.jdbc.Driver");
connection con=DM.getConnection(URL);
Resultset is a package which is in package java.sql package.
The Resultset object can be used to store multiple records returned by select statement.
When Resultset record is created initially result set cursor points to before to the first record.
Depending on the ResultSet cursor movement, you can divide the ResultSet into 2 types:
When Resultset is forward, only then you can move the Resultset cursor, that too only in the forward direction.
You can invoke the following methods on forward only Resultsets:
Checks whether next Record is available or not.
By default, ResultSets are forward only, you can specify the Resultsets as forward only explicitly as follows:
Statement stmt = con.createStatement();
Statement stmt = con.createStatement(ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_READ_ONLY);
ResultSet rs = stmt.executeQuery(sql);
Now, ResultSet is forward only and read only.
When Resultset is scrollable, you can move the Resultset cursor both in the forward direction and in the reverse direction a number of many times.
You can invoke the following methods on scrollable ResultSets.
next() | isAfterLast() | isBeforeFirst() | isLast() | isFirst() |
previous() | absolute() | relative() | afterLast() | beforeFirst() |
getxx() | close() | first() | last() |
You can specify the ResultSets as scrollable explicitly as follows:
Statement stmt = con.createStatement();
Statement stmt = con.createStatement (ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
ResultSet rs = stmt.executeQuery(sql);
Now ResultSet is scrollable and read only.
Statement stmt = con.createStatement();
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery(sql);
Now, ResultSet is scrollable and updatable.
Below given is a process to execute statements in JDBC:
load SQL
compile SQL
process SQL
load Data
If you want to submit multiple queries to the database one by one, a lot of time will get wasted on the requesting response.
Instead of submitting SQL statements one by one, we can submit multiple statements all at once to the database as a batch using Batch update concepts.
Using Batch updates, we can submit multiple insert, update and delete statements.