Documentos de Académico
Documentos de Profesional
Documentos de Cultura
1. INTRODUCTION
2. JDBC DRIVERS
4. PROPERTIES OF RESULSET
9.
JDBC is a set of classes and interfaces written in Java to allow other Java programs
to send SQL statements to a relational database management system.
A JDBC driver is a middleware layer that translates the JDBC calls to the vendor-
specific APIs. A driver is nothing but an implementation of various interfaces
specified in java.sql and javax.sql packages.
JDBC API provides developers a way to connect to relational data from within java
code. Using the JDBC API developers can create a client (which can be anything from
an applet to an EJB) that can connect to a database, execute SQL statements and
processes that result of this statement.
Version 2.0 of the JDBC API had two parts: the JDBC 2.1 core API and the JDBC 2.0
optional package API and although these TWO APIs have been combined into one in
version 3.0
JDBC DRVERS
The JDBC API also includes facilities to obtain metadata about the database,
parameters to statements, and results:
1. DatabaseMetadata
DatabaseMetadata: Example
• Driver
• Connection
• Statement
• PreparedStatement
• CallableStatement
• ResultSet
• ResultSetMetaData
• DatabaseMetaData
• DataTruncation
• SQLException
• SQLWarning
V. Execute a query
import java.sql.*;
a. Using Class.forName()
b. Using DriverManager.registerDriver()
c. Using System.setProperty();
a. Using Class.forName()
When we load the driver class (using the Class.forName ()), the registration
happens automatically
b. Using DriverManager.registerDriver()
c. Using System.setProperty()
Note: you can specify multiple drivers as a list separated by colons (:)
try
Class.forName (driver);
catch(ClassNotFoundException cnfe)
Answer -------
JDBC URL
protocol:<sub-protocol>:<subName>
Ex:
String mysqlURL =
“protocol:sub-protocol://machine-name:port/dBName?user=;password=”;
or “Jdbc:mysql://localhost:3306/myDataBase?user=;password=”;
String oracleURL =
“protocol:sub-protocol:driver-type:@machine-name:port:dBName”;
There are three types of statement according to the type of SQL query to be fired
a. Statement
b. PreparedStatement
c. CallableStatement
a. Statement
Statement statement = connection.createStatement();
Statement statement = connection.createStatement(int ResultSetType, int
resultSetConcurrency);
The first argument specifies the required ResultSet type. We will see it later.
b. PreparedStatement
Every time, we fire a query to Oracle database, there are three steps taken
by Oracle database server:
Execution: The compiled query is now executed & the result is returned.
c. CallableStatement
a. executeQuery()
ResultSet rs = statement.executeQuery(query);
b. executeUpdate()
c. Execute()
con.execute();
Depending on the type of statement (or query) there are three types of result:
a. ResultSet
Using the next() method on the ResultSet object we can get a pointer
pointing to each record. Now calling getString() or getInt() method on
ResultSet object, we can get the date from a specific column.
b. int
c. *****
A ResultSet contains the results of the SQL query, represented by a table with rows
and columns.
ResultSet is a cursor (pointer) which points to table returned by query fired to dB.
Initially the cursor (returned by query) points to Before First Record. We now need
to point it to first record to get data from that record.
When next () method is called first time on the ResultSet object, it points to first
record. The next () method returns Boolean value, true if there is a record, false
otherwise.
2. getMetaDataObject()
• Returns a ResultSetMetaData object
containing information about the columns in
the ResultSet
4. getWarnings()
• Returns the first SQLWarning or null, if no
warnings occurred
5. findColumn()
• Corresponding to the specified column name
7. getInt()
8. getString()
9. getDouble()
10. getLong()
11. getTime()
12.
13.
14.
15.
Note: In cases where the column index is subjected to change due to changes in
SQL or the database schema, it is preferable to use column names in these
methods.
Properties of ResultSet:
ResultSet
Scrollability Concurrency
Insensitive Scrolling
Sensitive Scrolling
1. Scrollability:
1.2 Scrollable:
2. Concurrency:
2.1 Read-Only:
o Default.
2.2 Updatable:
3 ResultSetMetaData Interface
1. getCatalogName()
3. getTableName()
4. getSchemaName()
5. getColumnCount()
9. getColumnTypeName()
10. getColumnClassName()
13. getScale()
14. getPrecision()
16. isCurrency()
18. isCaseSensitive()
19. isSigned()
20. isAutoIncrement()
ResultSetMetaData Example:
Statement st = con.createStatement();
String query = “select * from tableName”;
ResultSet rs = st.executeQuery(query);
ResultSetMetaData rsmetadata = rs.getMetaData();
Int noColumns = rsmetadata.getColumnCount();
// Column numbers start from 1
for(int i=1; i<noColumns +1; i++)
{
SOP(rsmetadata.getColumnName(i) + “--” + rsmetadata.getColumnType(i));
}
• JDBC Thin Driver (No local Net8 installation required/ handy for applets)
• JDBC OCI for writing stand-alone Java applications
• JDBC KPRB driver (default connection) for Java Stored Procedures and
Database JSP's.
Oracle's JDBC Thin driver uses Java sockets to connect directly to Oracle. It provides
its own TCP/IP version of Oracle's Net8 (SQL*Net) protocol. Because it is 100% Java,
this driver is platform independent and can also run from a Web Browser (applets).
Oracle's JDBC OCI drivers use Oracle OCI (Oracle Call Interface) to interact with an
Oracle database. You must use a JDBC OCI driver appropriate to your Oracle client
installation. The OCI driver works through either SQL*Net or Net8.
The JDBC OCI drivers allow you to call the OCI directly from Java, thereby providing
a high degree of compatibility with a specific Version of Oracle. Because they use
native methods, they are platform specific.
Oracle's JDBC KBPR driver is mainly used for writing Java stored procedures, triggers
and database JSPs. It uses the default/ current database session and thus requires
no additional database username, password or URL.
All three drivers support the same syntax and API's. Oracle needs three drivers to
support different deployment options. Looking at source code, they will only differ in
The the JDBC thin driver provides the only way to access Oracle from the Web
(applets). It is smaller and faster than the OCI drivers, and doesn't require a
pre-installed version of the JDBC drivers.
import java.sql.*;
class dbAccess {
while (rset.next())
stmt.close();
import java.sql.*;
class dbAccess {
try {
Class.forName ("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
while (rset.next())
stmt.close();
• network topology,
In general, all things being equal, you can assume that the more your request
and response change hands, the slower it will be. This means that Type 1 and
Type 3 drivers will be slower than Type 2 drivers (the database calls are make
at least three translations versus two), and Type 4 drivers are the fastest
(only one translation).
Each Database has it’s own stored procedure language, usually a variant of C
with a SQL preproceesor. Newer versions of db’s support writing stored
procedures in Java and Perl too.
Before the advent of 3-tier/n-tier architecture it was pretty common for stored
procs to implement the business logic( A lot of systems still do it). The biggest
advantage is of course speed. Also certain kind of data manipulations are not
achieved in SQL. Stored procs provide a mechanism to do these manipulations.
Ø
Ø D
Ø Dd