Connecting To A Database From A Servlet: J M Githeko
Connecting To A Database From A Servlet: J M Githeko
Servlet
J M Githeko
Objectives
To be able to:
Query a database via a web interface
View or browse data
Modify data
View database metadata [characteristics of
database]
Introduction to JDBC
JDBC = Java Database Connector
JDBC is included in Java SDKs
JDBC is a driver that allows access to
databases
There are four types fo JDBCs:
Type 1 - JDBC-ODBC Bridge
Type 2 - Native API, partly Java
Type 3 - Pure Java to database middleware
Type 4 – Pure Java direct to database
Type 1: JDBC-ODBC Bridge
Relies on existence of ODBC drivers
Requires code library JdbcOdbc.dll
Limited to MS Windows
ODBC is not thread-safe - leads to poor
performance
ODBC datasource has to be configured
Limited by underlying ODBC capabilities
BUT – convenient where available – good for
learning JDDBC
Type 2 – Native API
Similar to ODBC
Rely on native (OS-specific) code libraries
usually in the form of DLLs
Client must be configured to use the native
drivers
Type 3 - Middleware
Communicate via a middleware server
Performance affected by “middleware”
architecture
Provide greatest flexibility
Type 4 – Pure Java Direct to
Database
Driver talks direct to database
Drivers provided by DBMS vendor
Provides fastest performance
Install MySQL JDBC Connector
Download and uncompress .zip file [use
Winzip, 7zip or similar program]
Locate .jar file
In my case named mysql-connector-java-
5.1.7-bin.jar
Copy .jar into the folder named
<java_home>\jre\lib\ext
In my case:
C:\Program Files\Java\jdk1.6.0_11\jre\lib\ext
Application-Specific Placement for
MySQL Connector/J
Class.forName("com.mysql.jdbc.Driver").newInstance();
E.g.: Class.forName("com.mysql.jdbc.Driver");
Make the Connection to the Database by Using the
DriverManager Class
jdbc:mysql://[host][,failoverhost..][:port]/[database][?
propertyName1][=propertyVal1][&propertyName2]
[=propertyVal2]..
Example:
Connection con = null;
con=DriverManager.getConnection("jdbc:mysql:
//localhost:3306/example5", "root", "");
Example:
Statement stmt = null;
stmt = con.createStatement();
2. Execute the SQL Statement
Example:
ResultSet rs = null;
rs = stmt.executeQuery("SELECT * FROM
class1");
To Update Records
Use executeUpdate() Statement method
int updateCount = 0;
updateCount = stmt.executeUpdate ("INSERT
INTO Class1 VALUES
(‘John’,’S13/2020/05’)");
Retrieve the Data From the
Result Set
Iterate through a result-set:
while (rs.next())
{
String Name = rs.getString(1);
String Reg_No = rs.getString(2);
System.out.print(Name);
System.out.println(Reg_No);
}
Method getString(1) gets first String item in the record
Method getString() gets second String item in the record
Release the Resources
It’s important to close the ResultSet,
Statement, and Connection() objects.
Once closed, gabbage collection can be
done to remove objects from memory:
rs.close();
stmt.close();
conn.close();
Complete Example
DBConnection.java
Exercise 5/5
Write a servlet to print AICM course list from a
database containing AICM course listing.
Create two versions of the servlet:
1. Print simple list with CODE and TITLE only
2. Print complete list with CODE, TITLE, CF and
DESCRIPTION
Hints:
Collect the data
Tabulate
Create MySQL database, import AICM data
Write servlet to query database