Professional Documents
Culture Documents
Module Objectives
At the end of this module, participants will be
able to:
Use JDBC technology and describe its features.
Use Database drivers and describe their
features.
Establish a connection to a database using the
Connection object.
JDBC Overview
JDBC (Java Database Connectivity) is the Java industry
standard for database-independent connectivity between the
Java language and other databases.
JDBC provides a comprehensive API that provides the
application the ability to connect to databases, send SQL
statements, and process results.
Database Drivers
Database Drivers or JDBC
Drivers are required to connect
to different databases. The
JDBC requires different drivers
for each database.
JDBC drivers provide the
connection to the database and
implement the protocol
necessary for sending queries
and retrieving results.
Application
JDBC API
JDBC Driver
Database
Class.forName(JDBCDriver_Name);
Example :
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Sample :
10
Sample :
11
12
Example:
SELECT patients.name, meds.name FROM patients, meds WHERE patients.med_id
= meds.med_id;
Example:
SELECT a.name, b.name FROM patients a, meds b WHERE a.med_id = b.med_id;
13
14
<table>
<column> = <value>
[, <column> = <value>, ]
<condition>]
15
FROM <table>
<condition>] ;
16
17
Transactions
Transaction management is vital for operations that modify or
update the records in the database.
Transaction management allows the application to confirm the
execution of a group of SQL statements by committing the
changes.
Should an exception occur, the transaction can be rolled-back
to undo the changes that were made.
18
Transactions (cont.)
Setting the Connection.autocommit() field of the Connection
object to false will prevent the statements from being committed
until an explicit Connection.commit() is executed by the
application.
The Connection.rollback() method can be executed should an
event occur requiring the changes made so far to be undone.
19
Using PreparedStatement
PreparedStatement is a Java object, which represents a
precompiled SQL statement.
PreparedStatement is usually used for SQL statements that
take parameters, although it can also execute SQL statements
that have no parameters.
Before a Statement object can be used, the
java.sql.PreparedStatement class has to be imported
first.
20
21
setString()
setInt()
setDouble()
setDate()
22
23
24
2.
3.
4.
Once installed, open Program Files -> MySQL -> MySQL Administrator.
This will launch the MySQL Authentication UI as shown below.
25
Type in the root password entered during installation as the password then
click OK. Once verified, the MySQL Administrator UI will be displayed.
In the left pane of the UI, click the Restore option. This will display the
Restore page.
Click the Open Backup File button found at the lower right portion of the UI.
26
In the open file window, browse to the directory where the Eclipse SEF
Workspace archive is extracted.
9. Select the sql file to restore:
<SEF Workspace>\SEF\src\sef\module13\activity\SEF Activity.sql
10. Click Open. This will close the open file window.
11. Once in the Restore page, click Start Restore at the lower right portion of
the UI.
12. A dialog box displaying the progress bar will appear. Click Close once done.
27
28
29
Activity
1)
2)
3)
30
Appendix
MySQL
Version
License
: 5.0
: http://www.opensource.org/licenses/gpl-license.php