Professional Documents
Culture Documents
Outline of Presentation
Introduction to JDBC
JDBC Driver Types
General Architecture
JDBC Components
Introduction to JDBC
JDBC is a front end tool which is used to connect
front end java application to back end database
JDBC is an alternative to ODBC and ADO that
provides database access to programs written in
Java.
JDBC drivers are available for most DBMS
products
A JDBC Driver
Is an interpreter that translates JDBC method calls to
JDBC calls
Driver
Database
standard
General Architecture
JDBC Components
Using JDBC
1. Load the driver:
Class.forName(sun.jdbc.odbc.JdbcOdbcDriver);
//Type1 Driver
2. Establish a connection to the database:
A connection URL string includes the literal jdbc:, followed by the
name of the driver and a URL to the database
String url = "jdbc:odbc:dsn;
Create a Connection object:
Connection con = DriverManager.getConnection(url,
scott,tiger);
3. Create a statement object
Statement stmt = conn.createStatement();
Associate SQL with the statement object
String queryString = select * from emp";
Using a PreparedStatement
// Once you have a connection, you can create a
// "prepared statement" object. A prepared statement is
// precompiled, and can be used repeatedly with new values
// for its parameters.
// Use question marks for parameter place-holders.
PreparedStatement prepStmt = con.prepareStatement(
"INSERT INTO Artist (ArtistID, Name, "
+ "Nationality, BirthDate, DeceasedDate)"
+ "VALUES (ArtistSeq.nextVal, ?, ?, ?, ? )" );
// Now supply values for the parameters
// Parameters are referenced in order starting with 1.
prepStmt.setString( 1, "Galvan" );
prepStmt.setString( 2, "French" );
prepStmt.setInt
( 3, 1910 );
prepStmt.setNull ( 4, Types.INTEGER );
// The PreparedStatement object methods:
// 1) executeUpdate -- statements that modify the database
// 2) executeQuery -- SELECT statements (reads)
prepStmt.executeUpdate();
System.out.println( "Prepared statement executed" );
// Now do it again
prepStmt.setString(
prepStmt.setString(
prepStmt.setInt
(
prepStmt.setInt
(
1,
2,
3,
4,
"Monet" );
"French" );
1840 );
1879 );
prepStmt.executeUpdate();
System.out.println( "Prepared statement executed again" );
Java.sql.* package
java.sql.Connection
Statement createStatement (int resultSetType, int resultSetConcurrency)
PreparedStatement prepareStatement (String sql, int resultSetType, int
resultSetConcurrency)
CallableStatement prepareCall (String sql, int resultSetType, int resultSetConcurrency)
resultSetType
resultSetConcurrency
ResultSet.TYPE_FORWARD_ONLY
ResultSet.CONCUR_READ_ONLY
ResultSet.TYPE_SCROLL_INSENSITIVE
ResultSet.CONCUR_UPDATABLE
ResultSet.TYPE_SCROLL_SENSITIVE
Java.sql.* package
java.sql.ResultS
et
void beforeFirst() throws SQLException
void afterLast() throws SQLException
boolean first() throws SQLException
Example : Backward
Statement stmt = conn.createStatement
(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
symbol id
company symbol
price
num_shares
Register w/ODBC
Create an ODBC data source.
Click on the Start button.
Choose Settings, Control Panel
Double-click on ODBC Data Sources
Choose the System DSN tab
Click Add
Click on the desired driver (Microsoft ODBC for oracle)
Click on the Finish button
Enter a Data Source Name (I called my database CoolStocks
and that name appears in the java code below)
<%
Connection con = null;
try {
// Load the Driver class file
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// Make a connection to the ODBC datasource Movie Catalog
con = DriverManager.getConnection("jdbc:odbc:CoolStocks,scott,tiger);
// Create the statement
Statement statement = con.createStatement();
// Use the created statement to SELECT the DATA
// FROM the customer Table.
ResultSet rs = statement.executeQuery("SELECT * " +
"FROM customer");
// Iterate over the ResultSet
%>
finally {
try {
if ( con != null ) {
// Close the connection no matter what
con.close();
}
}
catch (SQLException sqle) {
out.println(sqle.getMessage());
}
}
%>
</BODY>
</HTML>
It Works!(output)
You can use the JSP action tag, <jsp:useBean> to include a bean
reference in your JSP page.
The <jsp:useBean> action tag creates an instance of the JavaBean and
stores the bean reference in a variable.
The variable can then be used to access the JavaBean throughout the JSP
page.
The following code snippet shows the syntax of the <jsp:useBean> action
tag:
<jsp:useBean
id=Bean Name
scope=ScopeName
class=class name />
Attribute
Description
name
property
Attribute
Description
name
property
Represents the bean property for which you want to retrieve the
value.
Demonstration-Implementing
JavaBeans in JSP
Problem Statement
Demonstration-Implementing
JavaBeans in JSP (Contd.)
Solution
maintainable
Struts
helps structuring these components in a Java web app.
these components
unifies the interaction between them
This separation between presentation, business logic and control is
achieved
by implementing the Model-View-Controller (MVC)
Design Pattern
Model
holds application data and business logic
Controller
bridge between Model and View
controls the flow of the application
Benefits:
better maintainability and testability of applications
ability to easily develop different kinds of UIs (e.g. console, GUI, )
separation of different tasks in development
code reusability
Controller ActionServlet
The central component in a Struts application
manages the flow of the application
receives user requests and delegates
them
Controller Actions
perform logic depending on a users request
Actions
are Java classes that extend Struts Action
class org.apache.struts.action.Action
The Action's execute() method is called by
the ActionServlet
Tasks are usually performed by Actions:
depending on the type of action:
Controller ActionForms
represent the data stored in HTML forms
hold the state of a form in their properties
provide getter/setter methods to access them
may provide a method to validate form data
ActionForms
are Java classes that extend Struts ActionForm
class org.apache.struts.action.ActionForm
are filled with the form data by the ActionServlet
one ActionForm can be used for more than one HTML form
very useful when building wizards or similar types of forms
...
The Model
Holds the data of an application and provides
business logic methods
Not directly part of the Struts framework!
The Model is usually built of different kinds
of Business Objects:
JavaBeans
simple
Important Questions
1. Explain Different Types of JDBC Drivers
2. Explain the process of Accessing Database from a
JSP Page
3. Explain Struts Framework
4. Explain Deploying of Java Beans in a JSP Page
5. Explain Java.sql.* package
6. Explain Various types of ResultSets with example