0% found this document useful (0 votes)
25 views

Connecting To A Database From A Servlet: J M Githeko

1. Import the necessary JDBC and servlet packages. 2. Get a database connection using the DriverManager and specify the URL, username, and password. 3. Create statements to execute queries and updates. 4. Process the result set and release resources by closing the connection, statements, and result sets.

Uploaded by

Jason Githeko
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Connecting To A Database From A Servlet: J M Githeko

1. Import the necessary JDBC and servlet packages. 2. Get a database connection using the DriverManager and specify the URL, username, and password. 3. Create statements to execute queries and updates. 4. Process the result set and release resources by closing the connection, statements, and result sets.

Uploaded by

Jason Githeko
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Connecting to a Database from a

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

 If Application Server can not find


javax.http.* and javax.servlet.* then place it
in also in <java_home>\jre\lib\ext
Application-Specific Placement of
MySQL Connector/J
 If application cannot find MySQL
Connector, place MySQL Connector/J .jar
also in
$CATALINA_HOME
/webapp/yourapp/WEB-INF/lib/
 In my server:
C:\Program Files\Apache Software
Foundation\Tomcat
6.0\webapps\ex5\WEB-INF\lib
Using the MySQL Connector/J
 Register the Connector/J JDBC Driver for MySQL

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", "");

^database ^user ^p/word


Cont’d
1. Get a Statement Object
Statement stmt = conn.createStatement();

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

You might also like