3Java_Database_programmin1
3Java_Database_programmin1
Data :
It is a meaningful information. It will be maintained by word document file. Word pad
is used to create the create the word document file. Word pad will maintain the
dictionary.
Eg:
MS-Word document File.
3 V's of Data
Volume:
How much data, Bytes, KB, MB, GB ( RDBMS Can ) , TB, PB, EB
Terabyte 1 TB = 1024 GB
Petabyte 1 PB = 1024 TB
Exabyte 1 EB = 1024 PB
Velocity:
Variety:
The various types of data, Structured data, Semi-Structured data and Un-Structured data
Unstructured data :
Eg:
Structured data :
Eg:
Relational data.
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
3|P ag e
Eg:
XML data
Data storage :
There are two types of storage
1) Temporary storage
2) Permanent storage
Temporary storage:
It is a data, which will be lost, once Application is shutdown ( stopped). It is maintained inside
the ram memory.
Eg:
String and StringBuffer
Collection Objects ( java.util package )
String and StringBuffer are used to store maintain unstructured small amount of data.
Collection Objects are used to store and maintain huge amount of structure , unstructed and
semi structured data.
Permanent storage
It is a Backend for the application, where storing and maintaining data permanently .
Eg:
Files and Database Servers
Files:
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
4|P ag e
Traditional file system and HDFS ( Hadoop Distributed File System ),are used to store the
data.
Database Concepts
Database :
It is a structured data. It will be maintained in the form of records. Database will be
maintained in the form of files.
Eg: Excel files,
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
5|P ag e
Data will be maintained in the files. This data is to be updated to meet the client ( eg :
company, bank, railways ) requirements, Some Mechanism is required to update the
data. Such mechanism is called as database management system. Some commands will
be maintained by the DBMS to update the data.
Eg :
INSERT, DELETE, UPDATE, SELECT
MS FOX-PRO is standalone database. It can not be shared by multiple clients and it fails to
maintain the huge amount of data base.
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
6|P ag e
Eg:
All Database servers are RDBMS
ORACLE , MS-SQL , MY-SQL, INFORMICS, DB2, SYBASE, postgres
Comparison of different Database servers
ORACLE:
It was developed by the Oracle Corporation.
It is a mostly used data base in the real time applications.
It was developed for all Operating Systems and Machines in the market
We can achieve 95 % of RDBMS with this data base.
MS-SQL Server :
It was developed by the Micro Soft.
It is a mostly used with Micro Soft front end products.
It was developed only for Window Operating Systems and Intel Machines.
We can not achieve complete RDBMS with this data base.
Dis-Adv There is no security for data base server as it is running on Windows OS.
MYSQL :
It was developed my Apache software foundation.
Later it was adopted by the Sun Soft.
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
7|P ag e
It was developed for all Operating Systems and Machines in the market
It is in-built with SOLARIS Operating system.
We can not achieve complete RDBMS with this data base.
It fails to support too many users to share the data.
Recently it was taken over by Oracle Corporation.
DB2 :
It was developed by the IBM
It is a mostly used data base with IBM mainframe applications.
We can achieve 99 % of RDBMS with this data base.
This is called universal database.
SYBASE :
It was developed by the Sybase Corporation.
It was developed for all Operating Systems and Machines in the market
We cannot achieve complete RDBMS with this data base.
INFORMICS :
It was developed by Informics Software company.
It was developed only for windows Operating Systems and Intel Machines.
It is mostly used with C++ Applications.
We cannot achieve complete RDBMS with this data base.
Postgres:
It was developed by the Postgres Inc.
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
8|P ag e
It was developed for all Operating Systems and Machines in the market.
It can be used with any front applications.
Advantages of RDBMS
Primary Advantages :
1) Data Security can be achieved
2) We can overcome Data redundancy
3) Data can be sharable
4) Data Normalization can be achieved
Other Advantages:
1) We can achieve the CODD’s Rules
2) Data representation is in the form of rows and columns.
3) Clients Operating system can be any one ( mostly clients are Windows O/S)
4) Data can stored in any format such Char, Number, Binary, Text
5) It provides support for ACID properties
6) Huge amount of data can be maintained
7) Data integrity can be achieved
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
9|P ag e
Every application will have its own drivers to connect to data base.
The common Applications are Java, .Net, ETL Tools, ERP Applications.
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
10 | P a g e
Java JDBC is a java API to connect and execute query with the database. JDBC API uses jdbc
drivers to connect with the database.
Before JDBC, ODBC API was the database API to connect and execute 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).
What is API
API (Application programming interface) is a document that contains description of all the
features of a product or software. It represents classes and interfaces that software programs
can follow to communicate with each other. An API can be created for applications, libraries,
operating systems, etc
JDBC Driver
JDBC Driver is a software component that enables java application to interact with the
database.
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
11 | P a g e
2) Native-API driver
4) Thin driver
The JDBC-ODBC bridge driver uses ODBC driver to connect to the database. The JDBC-
ODBC bridge driver converts JDBC method calls into the ODBC function calls. This is now
discouraged because of thin driver.
Advantages:
easy to use.
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
12 | P a g e
Dis-Advantages:
Performance degraded because JDBC method call is converted into the ODBC function
calls.
2) Native-API driver
The Native API driver uses the client-side libraries of the database. The driver converts JDBC
method calls into native calls of the database API. It is not written entirely in java.
Advantage:
Dis-Advantage:
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
13 | P a g e
Advantage:
No client side library is required because of application server that can perform many tasks
like auditing, load balancing, logging etc.
Dis-Advantages:
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
14 | P a g e
4) Thin driver
The thin driver converts JDBC calls directly into the vendor-specific database protocol. That is
why it is known as thin driver. It is fully written in Java language.
Advantage:
Dis-Advantage:
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
15 | P a g e
There are 7 steps to connect any java application with the database in java using JDBC.
3) Creating statement
6) Process ResultSet
7) Closing connection
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
16 | P a g e
To connect java application with the Oracle database ojdbc14.jar file is required to be loaded.
package com.nrit.mnrao.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
try
Class.forName("oracle.jdbc.driver.OracleDriver");
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
17 | P a g e
con = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:orcl",
"scott",
"tiger");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(sqlQuery);
while(rs.next())
System.out.println(
rs.getInt(1)+"\t"+
rs.getString(2)+"\t"+
rs.getString(3)+"\t"+
rs.getInt(4)+"\t"+
rs.getDate(5)+"\t"+
rs.getDouble(6)+"\t"+
rs.getDouble(7)+"\t"+
rs.getInt(8)
); }
con.close();
catch(ClassNotFoundException e)
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
18 | P a g e
e.printStackTrace();
catch(SQLException e)
e.printStackTrace();
finally
if(con!=null)
con.close();
The forName() method of Class class is used to register the driver class. This method is used to
dynamically load the driver class.
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
19 | P a g e
ORACLE oracle.jdbc.driver.OracleDriver
jdbc:oracle:thin:@hostname:portNumber:databaseName
DB2 COM.ibm.db2.jdbc.net.DB2Driver
jdbc:db2:hostname:portNumber/databaseName
Sybase com.sybase.jdbc.SybDriver
jdbc:sybase:Tds:hostname:portNumber/ databaseName
The getConnection() method of DriverManager class is used to establish connection with the
database.
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
20 | P a g e
throws SQLException
Connection con=DriverManager.getConnection(
Connection con=DriverManager.getConnection(
The createStatement() method of Connection interface is used to create statement. The object
of statement is responsible to execute queries with the database.
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
21 | P a g e
Statement stmt=con.createStatement();
Types of statements
1) Simple statement
2) Prepared Statement
3) Callable Statement
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.
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
22 | P a g e
6) Processing ResultSet:
while(rs.next())
Character getString()
Date getDate()
Timestamp getTimestamp()
BIT getBoolean()
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
23 | P a g e
By closing connection object statement and ResultSet will be closed automatically. The close()
method of Connection interface is used to close the connection.
con.close();
Mysql:
Step1:
$mysql -u root -p
Enter password:*******
mysql>show databases;
nrit
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
24 | P a g e
mysql > create table emp(empid int, name varchar(20), dept varchar(20), salary double(10,2));
+-------+--------+-------+---------+
+-------+--------+-------+---------+
+-------+--------+-------+---------+
Step2;
To connect java application with the mysql database mysqlconnector.jar file is required to be
loaded.
Down load “mysql-connector-java-5.1.18-bin.jar” and add to eclipse project ( add external jars
)
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
25 | P a g e
package com.nrit.mnrao.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
try
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/nrit",
"root",
"hadoop"
);
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(sqlQuery);
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
26 | P a g e
while(rs.next())
System.out.println(
rs.getInt(1)+"\t"+
rs.getString(2)+"\t"+
rs.getString(3)+"\t"+
rs.getDouble(4)
);
con.close();
catch (ClassNotFoundException e)
e.printStackTrace();
catch (SQLException e)
e.printStackTrace();
finally
if(con!=null)
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
27 | P a g e
con.close();
Eg:
ResultSet rs = st.executeQuery(sqlQuery);
while(rs.next())
System.out.println(
rs.getString(1)+"\t"+
rs.getInt(2)+"\t"+
rs.getDouble(3)+"\t"+
rs.getString(4)
);
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
28 | P a g e
package com.nrit.mnrao.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
try
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/",
"root",
"hadoop"
);
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(sqlQuery);
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
29 | P a g e
while(rs.next())
System.out.println(rs.getString(1));
con.close();
catch (ClassNotFoundException e)
e.printStackTrace();
catch (SQLException e)
e.printStackTrace();
finally
if(con!=null)
con.close();
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
30 | P a g e
package com.nrit.mnrao.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
try
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/empdb",
"root",
"hadoop"
);
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(sqlQuery);
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
31 | P a g e
while(rs.next())
System.out.println(rs.getString(1));
con.close();
catch (ClassNotFoundException e)
e.printStackTrace();
catch (SQLException e)
e.printStackTrace();
finally
if(con!=null)
con.close();
}
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
32 | P a g e
executeQuery():
This is used to execute any SQL query,which is fetching data from database.
package com.nrit.mnrao.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
try
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/empdb",
"root",
"hadoop"
);
Statement st = con.createStatement();
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
33 | P a g e
ResultSet rs = st.executeQuery(sqlQuery);
while(rs.next())
System.out.println(rs.getString(1));
con.close();
catch (ClassNotFoundException e)
e.printStackTrace();
catch (SQLException e)
e.printStackTrace();
finally
if(con!=null)
con.close();
}
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
34 | P a g e
List of setter() and getter() methods in JDBC for different data types of SQL
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
35 | P a g e
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
36 | P a g e
Connection interface:
A Connection is the session between java application and database. The Connection interface
is a factory of Statement, PreparedStatement, and DatabaseMetaData i.e. object of Connection
can be used to get the object of Statement and DatabaseMetaData. The Connection interface
provide many methods for transaction management like commit(),rollback() etc.
1) public Statement createStatement(): creates a statement object that can be used to execute
SQL queries.
Creates a Statement object that will generate ResultSet objects with the given type and
concurrency.
3) public void setAutoCommit(boolean status): is used to set the commit status.By default it is
true.
4) public void commit(): saves the changes made since the previous commit/rollback
permanent.
5) public void rollback(): Drops all changes made since the previous commit/rollback.
6) public void close(): closes the connection and Releases a JDBC resources immediately.
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
37 | P a g e
Statement interface
The Statement interface provides methods to execute queries with the database. The statement
interface is a factory of ResultSet i.e. it provides factory method to get the object of ResultSet.
2) public int executeUpdate(String sql): is used to execute specified query, it may be insert,
update, delete etc.
3) public boolean execute(String sql): is used to execute queries that may return multiple
results, create and drop
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
38 | P a g e
package com.nrit.mnrao.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
try
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/nrit",
"root",
"hadoop"
);
Statement st = con.createStatement();
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
39 | P a g e
if(count>0)
System.out.println("Successfully inserted");
else
System.out.println("failed to insert");
con.close();
catch (ClassNotFoundException e)
e.printStackTrace();
catch (SQLException e)
e.printStackTrace();
finally
if(con!=null)
{
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
40 | P a g e
con.close();
package com.nrit.mnrao.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
try
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/nrit",
"root",
"hadoop"
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
41 | P a g e
);
Statement st = con.createStatement();
if(count>=0)
con.close();
catch (ClassNotFoundException e)
e.printStackTrace();
catch (SQLException e)
e.printStackTrace();
finally
if(con!=null)
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
42 | P a g e
con.close();
package com.nrit.mnrao.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
try
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/nrit",
"root",
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
43 | P a g e
"hadoop"
);
Statement st = con.createStatement();
if(count>=0)
con.close();
catch (ClassNotFoundException e)
e.printStackTrace();
catch (SQLException e)
e.printStackTrace();
finally
if(con!=null)
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
44 | P a g e
con.close();
dbProduct=oracle
dbDriver=oracle.jdbc.driver.OracleDriver
dbHost=localhost
dbPort=1521
dbUid=scott
dbPasswd=tiger
#oracle
dbSid=orcl
#mysql
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
45 | P a g e
dbName=nrit
package com.nrit.mnrao.test;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
try
dataBaseProperties.load(freader);
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
46 | P a g e
String dbProduct=dataBaseProperties.getProperty("dbProduct");
if(dbProduct==null || dbProduct.isEmpty())
System.exit(0);
if(dbDriver==null ||dbDriver.isEmpty())
System.exit(0);
if(dbHost==null ||dbHost.isEmpty())
System.exit(0);
String dbPort=dataBaseProperties.getProperty("dbPort");
if(dbPort==null ||dbPort.isEmpty())
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
47 | P a g e
System.exit(0);
if(dbUid==null ||dbUid.isEmpty())
System.exit(0);
String dbPasswd=dataBaseProperties.getProperty("dbPasswd");
if(dbPasswd==null ||dbPasswd.isEmpty())
System.exit(0);
if(dbSid==null ||dbSid.isEmpty())
{
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
48 | P a g e
System.exit(0);
if(dbName==null ||dbName.isEmpty())
System.exit(0);
Class.forName(dbDriver);
String dbUrl=null;
if(dbProduct.equalsIgnoreCase("ORACLE"))
dbUrl="jdbc:oracle:thin:@"+dbHost+":"+dbPort+":"+dbSid;
else if(dbProduct.equalsIgnoreCase("MYSQL"))
dbUrl="jdbc:mysql://"+dbHost+":"+dbPort+"/"+dbName;
con = DriverManager.getConnection(dbUrl,dbUid,dbPasswd);
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
49 | P a g e
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(sqlQuery);
while(rs.next())
System.out.println(
rs.getInt(1)+"\t"+
rs.getString(2)+"\t"+
rs.getString(3)+"\t"+
rs.getInt(4)+"\t"+
rs.getDate(5)+"\t"+
rs.getDouble(6)+"\t"+
rs.getDouble(7)+"\t"+
rs.getInt(8)
);
rs.close();
con.close();
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
50 | P a g e
catch(ClassNotFoundException e)
e.printStackTrace();
catch(IOException e)
e.printStackTrace();
finally
if(con!=null)
con.close();
ResultSet interface:
The object of Scrollable ResultSet is to maintains a cursor pointing to a particular row of data.
Initially, cursor points to before the first row.
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
51 | P a g e
By default, ResultSet object can be moved forward only and it is not scrollable and updatable.
ResultSet.TYPE_FORWARD_ONLY.
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:
TYPE_SCROLL_INSENSITIVE
The constant indicating the type for a ResultSet object that is scrollable but generally not
sensitive to changes to the data that underlies the ResultSet.
TYPE_SCROLL_SENSITIVE
The constant indicating the type for a ResultSet object that is scrollable and generally sensitive
to changes to the data that underlies the ResultSet.
ResultSet.CONCUR_UPDATABLE);
Suppose you have EMP table in a database. You create insensitive statement:
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
52 | P a g e
ResultSet.CONCUR_READ_ONLY);
now you leave the result set open and you scroll through it using next(), previous() and
absolute(int)) methods.
At 10:03 you are still scrolling the result set but because you have INSENSITIVE result set,
you see old data.
Now comes the difference. If you had created the statement with SENSITIVE then you would
see all the changes that were being done at 10:02.
1) public boolean next(): is used to move the cursor to the one row next from the current
position.
2) public boolean previous(): is used to move the cursor to the one row previous from the
current position.
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
53 | P a g e
3) public boolean first(): is used to move the cursor to the first row in result set object.
4) public boolean last(): is used to move the cursor to the last row in result set object.
5) public boolean absolute(int row): is used to move the cursor to the specified row number in
the ResultSet object.
6) public boolean relative(int row): is used to move the cursor to the relative row number in
the ResultSet object, it may be positive ( forward) or negative
(backward).
7) public int getInt(int columnIndex):is used to return the data of specified column index of the
current row as int.
8) public int getInt(String columnName): is used to return the data of specified column
name of the current row as int.
9) public String getString(int columnIndex): is used to return the data of specified column
index of the current row as String.
10) public String getString(String columnName): is used to return the data of specified
column name of the current row as String.
Let’s see the simple example of ResultSet interface to retrieve the data of 3rd row.
package com.nrit.mnrao.test;
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
54 | P a g e
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.Properties;
try
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl",
"scott",
"tiger");
Statement st =
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
55 | P a g e
con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,Result
Set.CONCUR_UPDATABLE );
ResultSet rs = st.executeQuery(sqlQuery);
if(rs.first())
if(rs.absolute(5))
if(rs.relative(-2))
if(rs.relative(4))
if(rs.last())
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
56 | P a g e
con.close();
catch(ClassNotFoundException e)
e.printStackTrace();
finally
if(con!=null)
con.close();
Pre-compiled statement at the database server. For the First time of request query compiles and
stored at database server.
For next time of request, just calling pre-compiled query by passing values. That improves the
performance of database server.
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
57 | P a g e
Java PreparedStatement :
Eg:
As you can see, we are passing parameter (?) for the values. Its value will be set by calling the
setter methods of PreparedStatement.
Improves performance: The performance of the application will be faster if you use
PreparedStatement interface because query is compiled only once.
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
58 | P a g e
All the above setter methods depends on data table column type.
executes the query. It is used for create, drop, insert, update, delete etc.
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
59 | P a g e
create table emp1( id number(10), name varchar2(50), salary number(10,2), dept varchar2(20)
);
Order of setter methds using with prepared statements depends on sql query not on the table
columns.
package com.nrit.mnrao.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
60 | P a g e
try
Class.forName("oracle.jdbc.driver.OracleDriver");
con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl",
"scott",
"tiger");
pst = con.prepareStatement(sqlQuery);
pst.setInt(1, 1001);
pst.setString(2, "nrit1");
pst.setFloat(3,5000);
pst.setString(4,"dev");
if(pst.executeUpdate()!=0)
System.out.println("successfully inserted");
else
System.out.println("Failed to insert");
con.close();
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
61 | P a g e
catch(ClassNotFoundException e)
e.printStackTrace();
catch(SQLException e)
e.printStackTrace();
finally
if(con!=null)
con.close();
package com.nrit.mnrao.test;
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
62 | P a g e
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
try
Class.forName("oracle.jdbc.driver.OracleDriver");
con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl",
"scott",
"tiger");
pst = con.prepareStatement(sqlQuery);
pst.setFloat(1,2000);
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
63 | P a g e
pst.setString(2, "testing");
if(pst.executeUpdate()!=0)
System.out.println("successfully updated");
else
System.out.println("Failed to insert");
con.close();
catch(ClassNotFoundException e)
e.printStackTrace();
catch(SQLException e)
e.printStackTrace();
finally
if(con!=null)
{
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
64 | P a g e
con.close();
package com.nrit.mnrao.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
try
Class.forName("oracle.jdbc.driver.OracleDriver");
con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl",
"scott",
"tiger");
pst = con.prepareStatement(sqlQuery);
pst.setString(1, "dev");
ResultSet rs = pst.executeQuery();
while(rs.next())
System.out.println(rs.getInt(1)+"\t"+
rs.getString(2)+"\t"+
rs.getDouble(3)+"\t"+
rs.getString(4));
con.close();
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
66 | P a g e
catch(ClassNotFoundException e)
e.printStackTrace();
catch(SQLException e)
e.printStackTrace();
finally
if(con!=null)
con.close();
package com.nrit.mnrao.test;
import java.sql.Connection;
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
67 | P a g e
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
try
Class.forName("oracle.jdbc.driver.OracleDriver");
con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl",
"scott",
"tiger");
pst = con.prepareStatement(sqlQuery);
pst.setString(1, "dev");
pst.setDouble(2, 7000);
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
68 | P a g e
con.close();
catch(ClassNotFoundException e)
e.printStackTrace();
catch(SQLException e)
e.printStackTrace();
finally
if(con!=null)
con.close();
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
69 | P a g e
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "system", "oracle");
do {
int id = Integer.parseInt(br.readLine());
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
70 | P a g e
ps.setInt(1, id);
ps.setString(2, name);
ps.setFloat(3, salary);
ps.setString(4, dept)
int i = ps.executeUpdate();
String s = br.readLine();
if (s.startsWith("n")) {
break;
} while (true);
con.close();
ResultSetMetaData Interface
The metadata means data about data i.e. we can get further information from the data.
If you have to get metadata of a table like total number of column, column name, column type
etc. , ResultSetMetaData interface is useful because it provides methods to get metadata from
the ResultSet object.
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
71 | P a g e
package com.nrit.mnrao.test;
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
72 | P a g e
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
try
Class.forName("oracle.jdbc.driver.OracleDriver");
con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl",
"scott",
"tiger");
pst = con.prepareStatement(sqlQuery);
ResultSet rs = pst.executeQuery();
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
73 | P a g e
System.out.println(metaData.getColumnName(i)+"\t"+
metaData.getColumnTypeName(i)+"\t"+
metaData.getColumnType(i));
con.close();
catch(ClassNotFoundException e)
e.printStackTrace();
catch(SQLException e)
e.printStackTrace();
finally
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
74 | P a g e
if(con!=null)
con.close();
DatabaseMetaData interface:
DatabaseMetaData interface provides methods to get meta data of a database such as database
product name, database product version, driver name, name of total number of tables, name of
total number of views etc.
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
75 | P a g e
it returns the description of the tables of the specified catalog. The table type can be TABLE,
VIEW, ALIAS, SYSTEM TABLE, SYNONYM etc.
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
76 | P a g e
try
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl",
"scott",
"tiger");
DatabaseMetaData dbmd=con.getMetaData();
con.close();
}
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
77 | P a g e
catch(Exception e)
System.out.println (e);
package com.nrit.mnrao.test;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
try
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
78 | P a g e
Class.forName("oracle.jdbc.driver.OracleDriver");
con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl",
"scott",
"tiger");
while (rs.next())
System.out.println (rs.getString(3));
con.close();
catch(ClassNotFoundException e)
e.printStackTrace();
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
79 | P a g e
catch(SQLException e)
e.printStackTrace();
finally
if(con!=null)
con.close();
User tables.
package com.nrit.mnrao.test;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
80 | P a g e
import java.sql.SQLException;
import java.sql.Statement;
try
Class.forName("oracle.jdbc.driver.OracleDriver");
con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl",
"scott",
"tiger");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(query);
while (rs.next())
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
81 | P a g e
System.out.println (rs.getString(1));
con.close();
catch(ClassNotFoundException e)
e.printStackTrace();
catch(SQLException e)
e.printStackTrace();
finally
if(con!=null)
con.close();
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "system", "oracle");
while (rs.next()) {
System.out.println (rs.getString(3));
con.close();
} catch (Exception e) {
System.out.println (e);
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
83 | P a g e
package com.nrit.mnrao.test;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
try
Class.forName("oracle.jdbc.driver.OracleDriver");
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
84 | P a g e
con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl",
"scott",
"tiger");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(query);
while (rs.next())
System.out.println (rs.getString(1));
con.close();
catch(ClassNotFoundException e)
e.printStackTrace();
catch(SQLException e)
e.printStackTrace();
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
85 | P a g e
finally
if(con!=null)
con.close();
You can store images in the database in java by the help of PreparedStatement interface.
The setBinaryStream() method of PreparedStatement is used to set Binary information into the
parameterIndex.
throws SQLException
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
86 | P a g e
throws SQLException
For storing image into the database, BLOB (Binary Large Object) datatype is used in the table.
For example:
( NAME VARCHAR2(40),
PHOTO BLOB
Let's write the jdbc code to store the image in the database. Here we are using d:\\d.jpg for the
location of image. You can change it according to the image location.
package com.nrit.mnrao.test;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
87 | P a g e
try
Class.forName("oracle.jdbc.driver.OracleDriver");
con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl",
"scott",
"tiger");
pst.setString(1, "abc");
int i = pst.executeUpdate();
con.close();
catch(ClassNotFoundException e)
e.printStackTrace();
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
88 | P a g e
catch(SQLException e)
e.printStackTrace();
catch(Exception e)
e.printStackTrace();
finally
if(con!=null)
con.close();
By the help of PreparedStatement we can retrieve and store the image in the database.
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
89 | P a g e
The getBlob() method of PreparedStatement is used to get Binary information, it returns the
instance of Blob. After calling the getBytes() method on the blob object, we can get the array
of binary information that can be written into the image file.
( NAME VARCHAR2(40),
PHOTO BLOB
) /
package com.nrit.mnrao.test;
import java.io.FileOutputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
90 | P a g e
import java.sql.ResultSet;
import java.sql.SQLException;
try
Class.forName("oracle.jdbc.driver.OracleDriver");
con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl",
"scott",
"tiger");
ResultSet rs = pst.executeQuery();
// image
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
91 | P a g e
fout.write(barr);
fout.close();
} // end of if
System.out.println ("ok");
con.close();
catch(ClassNotFoundException e)
e.printStackTrace();
catch(SQLException e)
e.printStackTrace();
catch(Exception e)
e.printStackTrace();
finally
if(con!=null)
con.close();
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
92 | P a g e
Syntax:
For storing file into the database, CLOB (Character Large Object) datatype is used in the table.
For example:
( ID NUMBER,
NAME CLOB
package com.nrit.mnrao.test;
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
93 | P a g e
import java.io.File;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
try
Class.forName("oracle.jdbc.driver.OracleDriver");
con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl",
"scott",
"tiger");
ps.setInt(1, 101);
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
94 | P a g e
int i = ps.executeUpdate();
con.close();
catch(ClassNotFoundException e)
e.printStackTrace();
catch(SQLException e)
e.printStackTrace();
catch(Exception e)
e.printStackTrace();
finally
if(con!=null)
con.close();
}
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
95 | P a g e
The getClob() method of PreparedStatement is used to get file information from the database.
Let's see the table structure of this example to retrieve the file.
( ID NUMBER,
NAME CLOB
The example to retrieve the file from the Oracle database is given below.
package com.nrit.mnrao.test;
import java.io.FileWriter;
import java.io.Reader;
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
96 | P a g e
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
try
Class.forName("oracle.jdbc.driver.OracleDriver");
con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl",
"scott",
"tiger");
ResultSet rs = ps.executeQuery();
Clob c = rs.getClob(2);
Reader r = c.getCharacterStream();
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
97 | P a g e
int i;
fw.write((char) i);
fw.flush();
fw.close();
con.close();
System.out.println("OK");
catch(ClassNotFoundException e)
e.printStackTrace();
catch(SQLException e)
e.printStackTrace();
catch(Exception e)
e.printStackTrace();
finally
{
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
98 | P a g e
if(con!=null)
con.close();
CallableStatement Interface
We can have business logic on the database by the use of stored procedures and functions that
will make the performance better because these are precompiled.
Suppose you need the get the age of the employee based on the date of birth, you may create a
function that receives date as the input and returns age of the employee as the output.
The differences between stored procedures and functions are given below:
must not have the return type. must have the return type.
A procedure, can make a call to function Function, can not call to procedure.
Procedure supports input, output and in-out Function supports only input parameter.
parameters.
Exception handling using try/catch block can be Exception handling using try/catch can't be used in
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
99 | P a g e
To call the stored procedure, you need to create it in the database. Here, we are assuming that
stored procedure looks like this.
eid IN NUMBER,
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
100 | P a g e
ename IN VARCHAR2,
salary number,
dept VARCHAR2 )
is
begin
end;
package com.nrit.mnrao.test;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
try
{
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
101 | P a g e
Class.forName("oracle.jdbc.driver.OracleDriver");
con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl",
"scott",
"tiger");
cst.setInt(1, 2002);
cst.setString(2, "abc1");
cst.setFloat(3, 5000);
cst.setString(4, "admin1");
if( !cst.execute() )
System.out.println("successfully completed");
else
System.out.println("failed");
catch(Exception e)
e.printStackTrace();
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
102 | P a g e
finally
if(con!=null)
con.close();
is
temp NUMBER;
begin
temp:=x+y;
result:= temp;
end;
java program :
default parameters are IN params. Hence these are not required to register.
package com.nrit.mnrao.test;
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
103 | P a g e
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Types;
try
Class.forName("oracle.jdbc.driver.OracleDriver");
con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl",
"scott",
"tiger");
cst.setInt(1, 10);
cst.setInt(2, 20);
cst.registerOutParameter(3, Types.INTEGER);
cst.execute();
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
104 | P a g e
System.out.println(res);
catch(Exception e)
e.printStackTrace();
finally
if(con!=null)
con.close();
In this example, we are calling the sum4 function that receives two input and returns the sum
of the given number. Here, we have used the registerOutParameter method of
CallableStatement interface, that registers the output parameter with its corresponding type. It
provides information to the CallableStatement about the type of result being displayed.
The Types class defines many constants such as INTEGER, VARCHAR, FLOAT, DOUBLE,
BLOB, CLOB etc.
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
105 | P a g e
return number
is
temp number(8);
begin
temp :=n1+n2;
return temp;
end;
package com.nrit.mnrao.test;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Types;
try
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
106 | P a g e
Class.forName("oracle.jdbc.driver.OracleDriver");
con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl",
"scott",
"tiger");
cst.registerOutParameter(1,Types.INTEGER);
cst.setInt(2, 10);
cst.setInt(3, 20);
cst.execute();
System.out.println(res);
catch(Exception e)
e.printStackTrace();
finally
if(con!=null)
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
107 | P a g e
con.close();
The ACID properties describes the transaction management well. ACID stands for Atomicity,
Consistency, isolation and durability.
Consistency ensures bringing the database from one consistent state to another consistent state.
Durability means once a transaction has been committed, it will remain so, even in the event of
errors, power loss etc.
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
108 | P a g e
fast performance It makes the performance fast because database is hit at the time of commit.
2) void commit()
3) void rollback()
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
109 | P a g e
create table emp1( id number(10), name varchar2(50), salary number(10,2), dept varchar2(20)
);
package com.nrit.mnrao.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
try
Class.forName("oracle.jdbc.driver.OracleDriver");
con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl",
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
110 | P a g e
"scott",
"tiger");
con.setAutoCommit(false);
con.commit();
con.close();
System.out.println("Succesfully inserted");
catch(Exception e)
e.printStackTrace();
con.rollback();
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
111 | P a g e
finally
if(con!=null)
con.close();
If you see the table emp, you will see that 2 records have been added.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
try {
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
112 | P a g e
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "system", "oracle");
con.setAutoCommit(false);
while (true) {
String s1 = br.readLine();
int id = Integer.parseInt(s1);
String s3 = br.readLine();
ps.setInt(1, id);
ps.setString(2, name);
ps.setInt(3, salary);
ps.executeUpdate();
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
113 | P a g e
System.out.println ("commit/rollback");
if (answer.equals("commit")) {
con.commit();
if (answer.equals("rollback")) {
con.rollback();
if (ans.equals("n")) {
break;
con.commit();
} catch (Exception e) {
System.out.println (e);
}
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
114 | P a g e
Instead of executing a single query, we can execute a batch (group) of queries. It makes the
performance fast.
Fast Performance
Let's see the simple example of batch processing in jdbc. It follows following steps
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
115 | P a g e
Create Connection
Create Statement
Execute Batch
Close Connection
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "system", "oracle");
con.setAutoCommit(false);
con.commit();
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
116 | P a g e
con.close();
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "system", "oracle");
while (true) {
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
117 | P a g e
String s1 = br.readLine();
int id = Integer.parseInt(s1);
String s3 = br.readLine();
ps.setInt(1, id);
ps.setString(2, name);
ps.setInt(3, salary);
ps.addBatch();
if (ans.equals("n")) {
break;
ps.executeBatch();
con.close();
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
118 | P a g e
} catch (Exception e) {
System.out.println (e);
It will add the queries into the batch until user press n. Finally it executes the batch. Thus all
the added queries will be fired.
Using Savepoints
The new JDBC 3.0 Savepoint interface gives you the additional transactional control. Most
modern DBMS, support savepoints within their environments such as Oracle's PL/SQL.
When you set a savepoint you define a logical rollback point within a transaction. If an error
occurs past a savepoint, you can use the rollback method to undo either all the changes or only
the changes made after the savepoint.
The Connection object has two new methods that help you manage savepoints −
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
119 | P a g e
There is one rollback (String savepointName) method, which rolls back work to the specified
savepoint.
package com.nrit.mnrao.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Savepoint;
import java.sql.Statement;
Savepoint savepoint1=null;
Savepoint savepoint2=null;
try
Class.forName("oracle.jdbc.driver.OracleDriver");
con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl",
"scott",
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
120 | P a g e
"tiger");
con.setAutoCommit(false);
// set a Savepoint
savepoint1 = con.setSavepoint("sp1");
stmt.executeUpdate(SQL);
stmt.executeUpdate(SQL);
savepoint2 = con.setSavepoint("sp2");
stmt.executeUpdate(SQL);
stmt.executeUpdate(SQL);
con.commit();
con.close();
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
121 | P a g e
catch(Exception e)
e.printStackTrace();
con.rollback(savepoint2);
System.out.println("rollback sp2");
finally
if(con!=null)
con.close();
JDBC RowSet
The instance of RowSet is the java bean component because it has properties and java bean
notification mechanism.
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
122 | P a g e
It is the wrapper of ResultSet. It holds tabular data like ResultSet but it is easy and flexible to
use.
JdbcRowSet
CachedRowSet
WebRowSet
JoinRowSet
FilteredRowSet
rowSet.setUrl("jdbc:oracle:thin:@localhost:1521:xe");
rowSet.setUsername("system");
rowSet.setPassword("oracle");
rowSet.execute();
Advantage of RowSet
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
123 | P a g e
Let's see the simple example of JdbcRowSet without event handling code.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.sql.RowSetEvent;
import javax.sql.RowSetListener;
import javax.sql.rowset.JdbcRowSet;
import javax.sql.rowset.RowSetProvider;
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
124 | P a g e
Class.forName("oracle.jdbc.driver.OracleDriver");
rowSet.setUrl("jdbc:oracle:thin:@localhost:1521:xe");
rowSet.setUsername("system");
rowSet.setPassword("oracle");
rowSet.execute();
while (rowSet.next()) {
Interview Questions:
1) What is JDBC?
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
125 | P a g e
JDBC is a Java API that is used to connect and execute query to the database. JDBC API uses
jdbc drivers to connects to the database.
JDBC Driver is a software component that enables java application to interact with the
database.There are 4 types of JDBC drivers:
Creating connection
Creating statement
Executing queries
Closing connection
The java.sql package contains interfaces and classes for JDBC API.
Interfaces:
Connection
Statement
PreparedStatement
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
126 | P a g e
ResultSet
ResultSetMetaData
DatabaseMetaData
CallableStatement etc.
Classes:
DriverManager
Blob
Clob
Types
SQLException etc.
Statement
PreparedStatement
CallableStatement
In case of Statement, query is complied each time whereas in case of PreparedStatement, query
is complied only once. So performance of PreparedStatement is better than Statement.
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
127 | P a g e
The DriverManager class manages the registered drivers. It can be used to register and
unregister drivers. It provides factory method that returns the instance of Connection.
The Connection interface maintains a session with the database. It can be used for transaction
management. It provides factory methods that returns the instance of Statement,
PreparedStatement, CallableStatement and DatabaseMetaData.
The ResultSet object represents a row of a table. It can be used to change the cursor pointer and
get the information from the database.
The ResultSetMetaData interface returns the information of table such as total number of
columns, column name, column type etc.
The DatabaseMetaData interface returns the information of the database such as username,
driver name, driver version, number of tables, number of views etc.
The Connection interface provides methods for transaction management such as commit(),
rollback() etc.
14) What is batch processing and how to perform batch processing in JDBC?
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com
128 | P a g e
By using batch processing technique in JDBC, we can execute multiple queries. It makes the
performance fast.
15) How can we store and retrieve images from the database?
Durga Soft
nd
#202, 2 Floor, HUDA Maithrivanam, Ameerpet, Hyderabad.
040-6451 2786, 8096 9696 96, 9246 2121 43 | www.durgasoft.com