Professional Documents
Culture Documents
Advanced Pogramming (Java) Course: Topics
Advanced Pogramming (Java) Course: Topics
Advanced Pogramming (Java) Course: Topics
(JAVA) COURSE
Topics:
Java Database Connectivity (JDBC)
Graphics Programming with Java 2D and
Java 3D
Security
Servlets
Java Server Pages
Java-Based Wireless Applications
Part I
Java Database
Connectivity
(JDBC)
Java Database Connectivity (JDBC)
A common programming interface writing
programs
that access information stored in
databases,
spreadsheets, and
other data sources
either directly or through
middleware
JDBC cont’d
By using the JDBC interface,
Java programmers can request a connection with a
database,
Send query statements using SQL
Receive the results for processing.
industry.java.sun.com/products/jdbc/drivers
http://servlet.java.sun.com/products/jdbc/drivers
java.sun.com/products/jdbc/faq.html
http://java.sun.com/products/jdbc/articles/package2.html
http://java.sun.com/developer/Books/JDBCTutorial/chapter5.html
www.jguru.com/jguru/faq/faqpage.jsp?name=JDBC
What is the JDBC API?
A set of classes and interfaces written in the Java
a standard API for tool/database developers &
makes possible to write database applications using an Java
API.
Obtaining a variety of different data sources is
possible for Java applications
Makes possible to publish a web page containing
an applet that uses information obtained from a
remote data source. For example:
To connect all employees (even if they are using Windows,
Macintosh, and UNIX machines) is possible as one or more
internal databases via an intranet
The JDBC 3.0 API
Includes two packages:
java.sql package
javax.sql package (adds server-side capabilities)
.
The java.sql Package
The java.sql Package
Contains the entire JDBC API that sends SQL statements to
relational databases and retrieves the results of executing
those SQL statements.
The Driver interface represents a specific JDBC implementation
for a particular database system.
Connection represents a connection to a database.
The Statement, PreparedStatement, and CallableStatement
interfaces support the execution of various kinds of SQL
statements.
ResultSet is a set of results returned by the database in
response to a SQL query.
The ResultSetMetaData interface provides metadata about a
result set
DatabaseMetaData provides metadata about the database as a
whole.
Array
Provides an interface to SQL ARRAY objects.
getArray() method returns a standard Java array of
objects of the type returned by getBaseType().
The contents of the array can also be returned as a
ResultSet using the various getResultSet() methods.
public abstract interface Array {
// Public Instance Methods
public abstract Object getArray () throws SQLException;
public abstract int getBaseType () throws SQLException
public abstract String getBaseTypeName () throws
SQLException;
public abstract ResultSet getResultSet () throws
SQLException; }
import java.sql. Connection;
import java.sql. DriverManager;
import java.sql. SQLException;
public class JdbcExam {
public static void main (String args[]) {
Connection con = null;
try {
Class.forName ("com.mysql.jdbc.Driver"). newInstance();
con =DriverManager. getConnection ("jdbc:mysql:///test", "root",
"secret");
if (!con.isClosed())
System.out.println ("Successfully connected to MySQL server
using TCP/IP...");
} catch(Exception e) {
System.err.println ("Exception: " + e.getMessage());
} finally {
try {
if(con != null)
con.close();
} catch (SQLException e) {}
}} }
import java.sql. Connection;
import java.sql. DriverManager;
import java.sql. SQLException;
we imported 3 classes from java.sql package.
Connection represents a connection to a
database
DriverManager manages JDBC drivers
used to create connections to databases
http://www.mysql.com/doc/en/Windows_installatio
n.html
is the website which you can install the mysql
javax.sql package
The javax.sql package
Contains the JDBC 2.0 Standard Extension API.
The classes and interfaces in this package provide new
functionality,
connection pooling, that do not fall under the scope of the original JDBC API and
can therefore be safely packaged separately.
The DataSource interface serves as a factory for Connection
objects;
DataSource objects can be registered with a JNDI ( Java
Naming and Directory Interface )server, making it possible to
get the name of a database from a name service.
PooledConnection supports connection pooling, which allows
an application to handle multiple database connections in a
fairly transparent manner.
RowSet extends the ResultSet interface to a JavaBeans
component that can be manipulated at design time and used
with non-SQL data sources
What Is the JDBC-ODBC Bridge?
A JDBC driver
implements JDBC operations which translate them into
ODBC operations.
ODBC acts as a normal application program.
The bridge implements JDBC for any database for
which an ODBC driver is available.
contains a native library used to access ODBC.
The Bridge is installed automatically with the Java 2
SDK, Standard Edition, as package sun.jdbc.odbc
.
A Base for Other APIs
Java APIs are implemented as Java classes.
The following are just a few of the available APIs except
JDBC
Java Real Time
Java Foundation Classes (JFC)
Swing—user interface
Remote Method Invocation (RMI)
Java for XML (JAXP)
Java for XML Messaging (JAXM)
Java Sound
Java Media Framework
Java 2D and 3D graphics
Java Naming and Directory Interface (JNDI)
http://www.alumni.caltech.edu/~croft/research/java/snapshot/
What Does the JDBC API Do?
Establish a connection with a data source
Send queries and update statements to the data source
Process the results
Connection con = DriverManager.getConnection(
"jdbc:myDriver:wombat", "myLogin", "myPassword");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery ("SELECT a, b, c FROM
Table1");
while (rs. next()) {
int x = rs.getInt ("a")
String s = rs.getString ("b");
float f = rs.getFloat ("c");
}
Why not Use ODBC from Java?
Before the development of the JDBC API
The most widely used programming interface Microsoft's ODBC
(Open DataBase Connectivity) API
To access relational databases
ODBC can be used with Java, but
The best application is JDBC API
Applied as the form of the JDBC-ODBC Bridge
ODBC is not appropriate for direct use from the Java
programming language
Uses C interface.
Occurs a number of drawbacks in the security, implementation,
robustness, automatic portability
during the calls from Java to native C code
QUESTION:
What are other important differences between JDBC API and ODBC API?
Once ODBC (as native code) is called, Java programming language can't
guarantee that a security violation won't occur (untrusted).
The Bridge’s usage
A JDBC connection is opened
using a URL with the odbc subprotocol.
Loading is done as follows:
Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
Before any connection, the bridge driver class,
sun.jdbc.odbc.JdbcOdbcDriver
added to the java.lang.System property named jdbc.drivers
OR
explicitly loaded using the Java class loader.
After loaded, the ODBC driver creates an
instance of itself and registers this with the
JDBC driver manager.
What Is the JDBC URL Supported by
the Bridge?
jdbc:odbc:sybase
jdbc:odbc:mydb;UID=me; PWD=secret
jdbc:odbc:ora123; Cachesize =300
JDBC Drivers
Provides JDBC access via one or more Open Database
Connectivity (ODBC) drivers.
Translates JDBC calls into the middleware vendor's protocol, which is then
converted to a database-specific protocol by the middleware server
software.
Better performance than Types 1 and 2. Can be used when a company has
multiple databases and wants to use a single JDBC driver to connect to all
of them.
.
Type 4 Java Driver cont’d
Not optimized for server operating system, so the
driver can't take advantage of operating system
features.
The driver is optimized for the database and can take advantage
of the database vendor's functionality.
User needs a different driver for each different database
http://msdn.microsoft.com/library/default.asp?url=/downl
oads/list/dataaccess.asp
Remote Data Service (RDS)
The part of Microsoft's ActiveX Data Objects (ADO)
programming model designed for remote data access in
internet client/server applications.
http://www.deitel.com/books/advjHTP1/advjHTP1_CloudscapeInst
allation.html
Cloudscape con’t
Some standard Java tools and utilities that
make it easier to use Cloudscape and to
develop Cloudscape applications
ij is Cloudscape's interactive JDBC scripting tool. It is
a simple utility for running scripts against a
Cloudscape database. You can also use it
interactively to run ad hoc queries. ij provides several
commands for ease in accessing a variety of JDBC
features.
ij can be used in an embedded or a client/server
environment.
Cloudview is a graphical user interface for creating
and managing Cloudscape databases. It can be used
in an embedded or a client/server environment.
Cloudscape Database
Consists of platform-independent files stored in
a directory that has the same name as the
database.
Most of the Cloudscape tools are JDBC
applications.
A JDBC application is one that uses the classes
in the java.sql package to interact with a DBMS.
When we work with JDBC applications, we need
to know about the following two JDBC concepts
Drivers
Database connection URLs
Drivers
Before a JDBC application interacts with a database, it must
cause the appropriate JDBC driver to be loaded in the Java
session. Cloudscape provides three JDBC drivers for use with
the Cloudscape database engine. When you use the
Cloudscape tools that are JDBC applications, you will need to
know which driver to load.
COM.cloudscape.core.JDBCDriver For embedded
environments, when Cloudscape runs in the same JVM as the
application
COM.cloudscape.core.WebLogicDriver For client/server
environments in which Cloudscape runs in the Cloudconnector
framework and applications connect via the network
COM.cloudscape.core.RmiJdbcDriver For client/server
environments in which Cloudscape runs in the RmiJdbc Server
framework and applications connect via the network
Run as two different frameworks
• Embedded The simplest Cloudscape environment.
Enables Cloudscape as part of a Java application
Database accession as only one application at a time
No network access
Starting of an Cloudscape instance within the current JVM
and shutting down before completing