Documente Academic
Documente Profesional
Documente Cultură
SQL is a language used to create, manipulate, examine and manage relational databases. SQL
was standardized in 1992 so that a program could communicate with most database systems
without having to change the SQL commands. Unfortunately you must connect to a database
before sending SQL commands and each database vendor has a different interface as well as
different extensions of SQL
Microsoft ODBC API offers connectivity to almost all databases on almost all platforms and is
widely used programming interface for accessing relational databases. But ODBC cannot be
directly used with Java Programs due so various reasons and hence JDBC. JDBC does the
following things:
a) ODBC cannot be directly used with Java because it uses a C interface and calls from
java to native C code have a lot of drawbacks in security, implementation, and portability.
b) ODBC makes use of pointers, java does not
c) ODBC requires manual installation of the ODBC driver manager and driver on all client
machines. JDBC drivers are written in java and JDBC code is automatically installable,
secure and portable
In this model, the java applet / application interact directly with the database. A JDBC driver is
required to communicate with the particular database management system that is being
accessed. The SQL statements are sent to the database and the results are given to the user.
This type of model is referred to as the client / server configuration where user is the client and
the machine that has the database is called the server.
The reason for the middle tier is and it does the following
a) Collection of SQL statements from the client and handing over the same to the DB
b) Receiving results from the DB to the client
c) Maintaining control over accessing and updating of the data.
Middle tier until recently had been written in C and C++ which enable faster performance. With
the introduction of optimizing compilers that translate java byte codes into machine specific code,
it is not possible to implement the middle tier in java.
The JDBC Driver Manager is used to connect Java Applications to the correct JDBC Driver. There
are 4 types of JDBC Drivers and they are:
This driver converts JDBC calls into calls on the client API for Oracle, Sybase, Informix and other
DBMS. But some binary code has to be loaded on all clients like the bridge driver and again not
suitable for large applications.
This driver translates JDBC calls into DBMS independent net protocol. A server again translates
this protocol into a DBMS protocol. This net server middleware connects its pure java clients to
many different databases. The type of protocol in this middleware depends on the vendor.
These driers convert JDBC calls to network protocols used by the DBMS directory and the
requests from client machines are made directly to the DBMS server.
The ODBC Bridge is a thin layer over JDBC. All JDBC methods are mapped to ODBC calls and
thus interact with ODBC driver’s thereby enabling access to a wide range of databases.
DriverManager Class
The DriverManager class is the primary class that has the driver information. Whenever a driver
is loaded, it registers with the DriverManager class. This class enables selection of the same
whenever there is a request for connection to the Database. This class has the method
getConnection ().
Driver Interface
This is implemented by the JDBC drivers. Writing a JDBC driver consist of creating java class that
implements the driver interface.
Connection Interface
Close()
GetMetaData() – returns an object of DatabaseMetaData interface that can be used to obtain
detailed information about the structure and capabilities of the database.
CreateStatement() – creates a SQL PreparedStatement object using a SQL string.
PrepareCall() – creates a SQL CallableStatement object using a SQL string.
- Establishing Connection: For this you will have to a) Load the Driver and b) Make
Connection.
- For Loading the dirvers, we use the JDBC-ODBC Bridge Driver and the following
code with load it:
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
Class.forName(“oracle.jdbc.driver.OracleDriver”);
- You need for create an instance of the Driver and register it with the Driver
Manager because calling Class.forName() will do that for u automatically. If you
were to create ur own instance, you would be creating an unnecessary duplicate,
but with no harm and u do it the following way
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
When u have loaded a driver, it is available for making a connection with a DBMS.
The second step is establishing a connection is to have the appropriate driver connected to the
DBMS. The following code will do it
The JDBC URL provides a way for identifying the database so that the appropriate Driver will
recognize it and establish the connection. It would be jdbc:<subprotocol>:<subname>.
Once we have connected to the database, we can request information on the names of the tabes
and the names and contents of their columns and we can run SQL statements that either query
the database or add to or modify its contents. The objects that we can use to obtain information
from the database are:
b) ResultSet – Information about a table or a result of a query. We can access the data row
by row, but an access the columns in any order.
c) ResultSetMetaData – Inforamtion about the column names and types in a Result Set.
SQL Statements
The Statement Interface defines methods that are used to interact with databases via the
excution of the SQL statements. These methods also support the processing of query results
returned via ResultSet objects and provide control over the mechanics of query processing.
Execute()
ExecuteUpdate()
ExecuteQuery()
Once a connection is established, it is used to pass SQL statements to its underlying database.
JDBC do not put any restriction on the kinds of SQL statements that can b send. Therefore the
user must be responsible for making sure that the underlying database can process the SQL
statements being send.
The following are the quick way to determine which Connection method is appropriate for creating
different types of SQL statements:
PreparedStatement Interface
This interface defines methods that are used to work with precompiled SQL statements and this
provides efficient way of executing frequently used SQL statements.
Prepared Statement is useful when the same statement is to be repeated many number of times.
For Example: select ? from emp and the ? mark will then be replaced by the parameter passed.
The methods setInt, setString, setObject(), setDate(), setTime() etc are used to set the values to
the appropriate parameters.
Here the first parameter to setInt is the number of the argument i.e., only one parameter is passd.
The second argument refers to the value. Here the details of the item whose product code is 100
will be contained in the result set.
This interface is used to implement stored SQL procedure. CallableStatement objects are created
via prepareCall() method of the Connection class. A Stored SQL procedure is passed as an
argument to the prepareCall() method.
A stored procedure is used when different types of clients need the same functionality. Another
advantage of this is that it can be used to check for all types of constraints since the whole date is
available to stored procedures.
A Statement object is what sends your SQL statement to the DBMS. You simply create a
Statement object and then execute it, supplying the appropriate execute method with the SQL
statement you want to send.
We can make a string createTableBooks and assign this query to it and then use it in this
alternate form.
Stmt.executeUpdate(createTableBooks);
Executing Statements
We have used executeUpdate() because it is a DDL statement and all create, alter, drop are
examples of DDL. We most commonly use executeQuery().
An Example
import java.util.*;
import java.sql.*;
class Jdbc
{
public static void main(String[] args)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c =
DriverManager.getConnection("jdbc:odbc:oracle","scott","tiger");
DatabaseMetaData dm = c.getMetaData();
ResultSet rs = dm.getTables(null,null,null,null);
while(rs.next())
{
System.out.println(rs.getString(3));
}
c.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
ResultSet
The ResultSet is one of the most important object in JDBC. It is essentially an abstraction of a
table of the general width and unknown length. Nearly all methods and queries return data as a
ResultSet. It contains any number of named columns that u can ask for by name. Before u can
use ResultSet, we need to ask how many columns it contains. This information is stored in the
ResultSetMetaData.
ResultSetMetaData
We can obtains ResultSetMetaData from the ResultSet using the getMetaData() method. We can
use this object to discover the number and type of columns and names of each column
getColumnCount()
GetColumnName(int I) – returns the name of the column number I
getColumnType( int I) – returns the SQL database for this column.
ResultSetMetadate rsmd;
Rsmd = results.getMetaData();
noofcols = getColumnCount();
When we obtain a ResultSet, it points to just before the first row. We use the next() method to
obtain each additional row and method returns false when no more rows remain. Since fetching
data might results in an exception, we must always enclose our result set in try block.
try
{
rsmd = results.getMetaData();
noofcols = getColumnCount();
while(more)
{
for (I=1; I<nosofcols; Iee)
{
SOP (results.getString(I)+” “ );
SOP()
More = results.next();
}
results.close();
}
catch(Exception e)
{}
we can use the getXXX methods of the approprate type (Sting, int) to retrieve the value in each
column. The method for retrieving value of Varchar is String.
DatabaseMetaData
getTables – returns a descriptin of the table names for all tables matching table names and all
columns matching column names.
getColumns –
getURL() – Gets the name of the URL you are connected to.
Transaction Processing
A transaction consists of a group of related database operations. The SQL statements that make
up a transaction update the database at the same time. Even though statement are executed in
sequence, they do not permanently update the database until they are committed. If an error
happens, the SQL statements are be rolled back.
The setAutoCommit(), commit() and rollback() methods of the connection interface are used to
implement transaction processing.
If AutoCommit is turned on, then individual statements are organized into single transaction until a
commit or rollback () is invoked.
Example
import java.sql.*;
import java.util.*;
class Jdbc1
{
public static void main(String[] args)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c =
DriverManager.getConnection("jdbc:odbc:oracle","scott","tiger");
Statement st = c.createStatement();
System.out.println("Executing SQL");
ResultSet rs = st.executeQuery("Select * from item");
try
{
while(rs.next())
{
System.out.println(rs.getInt("pcode")+ " ; ");
System.out.println(rs.getString("pname"));
System.out.println(rs.getString("price"));
}
}
catch(Exception e)
{
System.out.println("Inside "+e);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
import java.sql.*;
import java.util.*;
import java.io.*;
class Jdbc2
{
public static void main(String[] args)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c =
DriverManager.getConnection("jdbc:odbc:oracle","scott","tiger");
Statement st = c.createStatement();
try
{
int x = st.executeUpdate("insert into item values("+y+",
"+st1+","+z+")");
System.out.println("Number of rows affected = "+x);
}
catch(Exception e)
{
System.out.println("Inside "+e);
}
}
catch (Exception e)
{
System.out.println(e);
}
}
}
import java.sql.*;
import java.util.*;
class Jdbc3
{
public static void main(String[] args)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c =
DriverManager.getConnection("jdbc:odbc:oracle","scott","tiger");
Statement st = c.createStatement();
try
{
}
catch(Exception e)
{
System.out.println("Inside "+e);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
import java.sql.*;
import java.util.*;
class Jdbc4
{
public static void main(String[] args)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c =
DriverManager.getConnection("jdbc:odbc:oracle","scott","tiger");
Statement st = c.createStatement();
try
{
}
catch(Exception e)
{
System.out.println("Inside "+e);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
import java.sql.*;
import java.util.*;
import java.io.*;
class Jdbc5
{
public static void main(String[] args) throws Exception
{
if( arg.length<3)
{
System.out.println("Pass pcode, pname and price as arguments");
}
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c =
DriverManager.getConnection("jdbc:odbc:oracle","scott","tiger");
st.setInt(1,Integer.parseInt(arg[0]);
st.setString(2,arg[1]);
st.setInt(3,Integer.parseInt(arg[2]);
try
{
int z = st.executeUpdate();
System.out.println("Number of rows affected = "+x);
}
catch(Exception e)
{
System.out.println("Inside "+e);
}
st.close();
c.clost()
}
catch(Exception e)
{
System.out.println(e);
}
}
}
import java.sql.*;
import java.util.*;
import java.io.*;
class Jdbc6
{
public static void main(String[] args) throws Exception
{
if( arg.length<2)
{
System.out.println("Pass pcode, pname and price as arguments");
}
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c =
DriverManager.getConnection("jdbc:odbc:oracle","scott","tiger");
st.setString(2,arg[1]);
st.setInt(2,Integer.parseInt(arg[0]);
try
{
int z = st.executeUpdate();
System.out.println("Number of rows affected = "+x);
}
catch(Exception e)
{
System.out.println("Inside "+e);
}
st.close();
c.clost()
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Make a Database in Access called as Book, with Bookname, Price and Bookcode. Insert some
records and then run this program.
import java.sql.*;
import java.util.*;
class JdbcAccess
{
public static void main(String[] args)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c = DriverManager.getConnection("jdbc:odbc:access");
Statement st = c.createStatement();
System.out.println("Executing SQL");
try
{
while(rs.next())
{
System.out.println(rs.getString("Bookname"));
System.out.println(rs.getInt("Price"));
}
}
catch(Exception e)
{
System.out.println("Inside "+e);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
The Application layer is where the developer uses the API to make calls to the database via SQL
and retrieve the results.
The Driver layer handles all communication with a specific driver implementation.
The driver layer is responsible for developing code that interfaces to the database and supports
the JDBC application level calls. There are 4 main interfaces that every driver layer must
implement and one class that bridges the Application and Driver layers. The 4 interfaces are
Driver, Connection, Statement and Resultset. The Driver interface implementation is where the
connection to the database is made. In most applications, the Driver is accessed through the
DriverManager class – providing one more layer of abstraction for the developers.
The Connection, Statement and Resultset interfaces are implemented by the driver vendor but
these interfaces specify the methods that the developer can use. They allow the developer to
create statements and retrieve results without having to think about where the objects are coming
from or worry about what specific driver the application will use.
Driver is an interface. Each vendor supplies a class that implements this interface. The other
important class is the DriverManager class, which sits above the Driver and Application layers.
The DriverManager class is responsible for loading and unloading drivers and making
connections through drivers. The DriverManager class also provides features for logging and
database login timeouts.
Imp: The driver does not need to connect directly to a database and can support a new protocol
for a multi tier database design.
Every JDBC application must have atleast one JDBC driver implementation. The Driver interface
allows the DriverManager and JDBC Application layers to exist independently of the particular
database used. A JDBC driver is an implementation of the Driver interface class.
Drivers use a string to locate and access database. The syntax is jdbc:drivername:password.
In this the database driver is oracle driver and the subname is a local database called products.
This driver is designed to known how to use the subname when making the connection to the
oracle database.
A network naming service may also be specified as the subprotocol rather than using a specific
database driver name. In this case, the subprotocol would define the naming service :
jdbc:localnaming:human-resource.
In this example, the subprotocol defines a local service that an resolve the subname human-
resource to a database driver. This approach can be very useful when the developer wants to
isolate the user from the actual location, name, database username and database password. This
URL specifies a driver named localnaming this could be java program that contains a simple flat
file lookup translates human-resource into hrddatabase1:eng:888/personnel and know to use the
username user and password xyz. The details of the connection are hidden from the user.
This checks the subprotocol name of the URL string passed for a match with this driver. If there is
a match the driver should then attempt to make a connection to the database using the
information passed in the remainder of the URL. A successful database connection will return an
instance of the driver’s implementation of a Connection interface. The SQLException will be
through only if the driver recognizes the URL subprotocol but cannot make the database
connection. A null is returned if the URL does not match a URL the driver expected. The
username and password are included in an instance of the Properties container class.
Explicitly asks the driver if the URL is valid. Note that typically the implementation of this method
checks only the subprotocol specified in the URL and not whether the connection can be made.
The Connection object is used to create Statement object that perform queries.
Since Connection is a java interface, so the object returned is actually an instance of the vendor’s
implementation of the Connection interface.
DriverManager Class
This is actually a utility class used to manage JDBC Drivers. The class provides methods to
obtain a connection through a driver, register and deregister drivers, set up logging and set login
timeouts for database access.
This attempts to return a reference to an object implemented from the Connection interface. The
method sweeps through a vector of stored Driver classes passing the URL string and Properties
object to each in turn. The first Driver class that returns a Connection is used and info is a
reference to a Properties container object of username/password.
Drivers are registered with the DriverManager class either at initialisation of the DriverManager
class or when an instance of the driver is created. When the DriverManager class is loaded, a
section of static code in the class is run and the class names of drivers listed in a java property
named jdbc.drivers are loaded, which contains a list of colon seperated driver class names.
Driver class implementation must explicitly register itself with the DriverManager by calling:
DriverManager.registerDriver (this).
Application Layer
The Application layer encompasses 3 interfaces that are implemented by the Driver layer but are
used by the developer. Once a Connection object returned, the developer may create a
Statement object to issue SQL against the database. If the SQL that was submitted was a select
query, then the result set is returned in a ResultSet Object.
Connection Interface:
The Connection interface represents a session with the database connection provided by the
Driver. Typical database connections include the ability to control changes made to the actual
data stored through transactions. On creation, the JDBC connections are in a auto commit mode
– there is no rollback possible. So after getting a Connection object from the driver, the developer
should consider setting auto commit to disable with a setAutoCommit (boolean b) method and
then the Connection will support Connection.commit() and Connection.rollback(). The methods
are
CallableStatement prepareCall (String sql): The Connection object implementation will return an
instance of a CallableStatement. CallableStatements are optimised for handling stored
procedures. The driver implementation may send the sql string immediately when prepareCall ()
is complete or may wait until an execute method occurs.
Connection c;
Statement s;
c = DriverManager.getConnection(url);
s = c.createStatement();
This statement may be used to send SQL statements that return a single result set in a ResultSet
object reference.
Statements that need to be called a number of times with a slight variations may be executed
more efficiently using a PreparedStatement. The Connection interface is also used to create a
CallableStatement whose purpose is to execute stored procedures.
Statement Interface:
A Statement is the vehicle for sending SQL queries to the database and retrieving a set of results.
ResultSet executeQuery (String url): This executes a single query and returns the results in an
object of type ResultSet.
int executeUpdate (String s): This executes a single SQL update that returns a count of rows
affected rather than a set of results.
ResultSet getResultSet (): This returns the result of a statement execution as a ResultSet object.
Note that if there are no results to be read or if the result is an update count, this method return
null. Also note that once read, the results are cleared.
int getUpdateCount(): This returns the status of an Update, an Insert or a Delete query, a stored
procedure or a DLL statement. The value returned is the number of rows affected.
boolean getMoreResults (): Moves to the next results in a set of multiple results / update counts.
This method returns true if the next result is a ResultSet object. This method will also close any
previous ResultSet read.
PreparedStatement Interface
The PreparedStatement interface extends the Statement interface. When there is a SQL
statement that requires repetition with minor variations, the PreparedStatement provides the
mechanism for passing a precompiled SQL statement that uses parameters.
The setType methods fill the value of parameters (marked by question marks) in a
PreparedStatement. These parameters are indexed from 1 to n.
Parameters hold their current values until either a new setType method is called or the method
clearParameters() is called for the PreparedStatement object.
CallableStatement interface
The CallableStatement interface is used to execute SQL stored produres. Callable Statement
inherits from the PreparedStatement interface, so all of the execute and setType methods are
avaliable. The syntax of stored Procedures varies among database vendors, so JDBC defines a
standard way to call stored procedures in all RDBMS.
The JDBC uses an escape syntax that allow parameters to be passed as in parameters and out
parameters. The syntax also allow a result to be returned. If this syntax is used, the parameter
must be registered as an out parameter.
The ResultSet interface defines methods for accessing tables of data generated as a result of
executing a Statement. ResultSet column values may be accessed in any order – they are
indexed and may be selected by either the name or the number (1 to n) of the column. ResultSet
maintains the position of the current row, starting with the first row of data returned. The next ()
moves to the next row of data.
ResultSetMetaData get MetaData(): Returns an object that contains a description of the current
result set including the number of columns the type of each column and properties of the results.
JDBC
Package java.sql contains classes and interfaces for manipulating relational databases in Java.
Interface Connection helps manage the connection between the Java program and the database.
It also provides support for executing SQL statements to manipulate the database and transaction
processing.
Connecting to a database requires the database URL (Uniform Resource Locator) that helps the
program to locate the database (possibly on a network or in the local file system of the computer)
and may require a username and password for logging in to the database.
The database URL specifies the protocol for communication, the subprotocol for communication
and the name of the database.
The subprotocol odbc indicates that the program will be using jdbc and JDBC-to-ODBC bridge
driver to connect to a Microsoft ODBC data source. ODBC is a technology developed by
Microsoft to allow generic access to disparate database systems on the windows platform.
The Java 2 SDK comes with a JDBC-to-ODBC bridge database driver to allow any java program
to access any ODBC data source. The driver is defined by class JdbcOdbcDriver in package
sun.jdbc.odbc.
Method forName of class Class is used to load the class definition for a database driver. Class
sun.jdbc.odbc.JdbcOdbcDriver represents the JDBC-to-ODBC bridge driver.
When a query is performed on a database, a ResultSet object is returned containing the results of
the query. The methods of interface ResultSet allow the programmer to manipulate the query
results.
Connection method createStatement obtains a Statement object that will be used to manipulate
the database.
Statement method executeQuery returns an object that implements interface ResultSet and
contains the result of a query.
ResultSet method next positions the cursor to the next record in the ResultSet. Initially the
ResultSet cursor is positioned before the first record, so this method must be called before you
can access the results. Method next returns a boolean value indicating if it was able to position to
the next record. If the method returns false, there is no more records to process.
ResultSet method getMetaData return the meta data for the ResultSet in a ResultSetMetaData
object. The meta data for the ResultSet describes the contents of a ResultSet. This information
can be used to obtain information about the names and types of the ResultSet columns and can
help the programmer process a ResultSet dynamically when detailed information about the
ResultSet is not known in advance of the query.
ResultSetMetaData method getColumnCount returns the number of columns in the ResultSet.
ResultSetMetaData method getColumnName returns the name of the specified column.
To connect to an ODBC data source the database must be registered with the system through the
ODBC Data Sources option in the windows control panel.
The basic format of an INSERT INTO SQL statement is “ INSERT INTO tablename (Column1,
Column2..) VALUES (‘value1’, ‘value2’..), where tablename is the table in which the data will be
inserted. Each column name to be updated is specified in a comma-separated list in parentheses.
The value for each column is specified after the SQL keywords VALUES in another comma
separated list in parentheses.
Statement method executeUpdate sends a SQL statement into the database that updates a
record or adds a new record. The method returns an int indicating the success or failure of the
update operation.
The basic Update SQL statement has the form: UPDATA tablename SET column1=’value1’,
column2=’value2’ WHERE criteria.
If the database supports transaction processing – changes made to the database can be undone.
Java provides transaction processing via several methods of interface Connection.
Method setAutoCommit determines if each individual SQL statement should be performed and
committed individually or in several SQL statements should be grouped as a transaction. If the
argument to setAutoCommit is false, the Statement used to executed the SQL statements must
be terminated with a call to Connection method commit or method rollback. Interface Connection
also provides method getAutoCommit that returns to determine the auto commit state.