Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
8 views

3.9 Database and Java Servlets.pptx

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

3.9 Database and Java Servlets.pptx

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

SUBJECT CODE

TYPE THE SUBJECT NAME HERE

UNIT NO 3

SERVER SIDE SCRIPTING


Topic: Database and Java Servlets

III V

20ITPC502
WEB TECHNOLOGY
IT8501
WEB TECHNOLOGY

Database and Java Servlets

● Servlet Database Access


● The term JDBC stands for Java Database Connectivity. It is a standard
Java API for database and used for connecting the wide range database and
the Java programming language.
● With HTML, Servlets, JDBC, and DBMS, we can easily build Web-based
three-tier systems -- client, Web server, and database server
IT8501
WEB TECHNOLOGY

JDBC Architecture

● JDBC is an API specification developed by Sun Microsystems that defines a


uniform interface for accessing various relational databases.
● The primary function of the JDBC API is to provide a means for the developer
to issue SQL statements and process the results in a consistent,
database-independent manner.
● The JDBC API uses a driver manager and database-specific drivers to
provide transparent connectivity to heterogeneous databases.
● The JDBC driver manager ensures that the correct driver is used to access
each data source. The driver manager is capable of supporting multiple
concurrent drivers connected to multiple heterogeneous databases.
IT8501
WEB TECHNOLOGY

Layers of JDBC architecture


IT8501
WEB TECHNOLOGY

Accessing a Database

The process of retrieving information from a database via JDBC involves these
five basic steps:

1. Register the JDBC driver with the driver manager.


2. Establish a database connection.
3. Execute an SQL statement.
4. Process the results.
5. Close the database connection.
IT8501
WEB TECHNOLOGY

Register the JDBC Driver

● Before a JDBC driver can be used to establish a database connection, it must


first be registered with the driver manager.
● The driver manager's job is to maintain a reference to all driver objects that
are available to JDBC clients.
● A JDBC driver automatically registers itself with the driver manager when it is
loaded.

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
IT8501
WEB TECHNOLOGY

Establish a Database Connection

• Once the driver is loaded, we can use it to establish a connection to the


database.
• A JDBC connection is identified by a database URL that tells the driver
manager which driver and data source to use ,with the following format

• jdbc:SUBPROTOCOL:SUBNAME

• The first part of the URL indicates that JDBC is being used to establish the
connection. The SUBPROTOCOL is the name of a valid JDBC driver or other
database connectivity solution. The SUBNAME is typically a logical name, or
alias, that maps to a physical database
• To establish a database connection, use the DriverManager object's
getConnection() method.
IT8501
WEB TECHNOLOGY

Establish a Database Connection

Syntax for getConnection Method

● Connection dbConn = DriverManager.getConnection(


"jdbc:mysql://localhost/phonebook");
● Databases that require authentication, the connection can be established like
this:

● String username = "Erin", password = "secret";


Connection dbConn = DriverManager.getConnection(
"jdbc:mysql://localhost/phonebook", username, password);
IT8501
WEB TECHNOLOGY

Execute an SQL Statement


● Once established, the database connection can be used to submit SQL statements to
the database.
● An SQL statement performs some operation on the database such as retrieving,
inserting, updating, or deleting rows.
● To execute an SQL command, a Statement object must be created using the
Connection object's createStatement() method. A Statement object can be created as
shown here:
● Statement stmt = dbConn.createStatement();
● Using the Statement object's executeQuery() method, information can be retrieved from
the database.
● The executeQuery() method accepts an SQL SELECT statement and returns a
ResultSet object containing the database rows extracted by the query.
● For inserts, updates, or deletes, use the executeUpdate() method. The ResultSet object
can be created like this:
● ResultSet rs = stmt.executeQuery("select * from employee");
IT8501
WEB TECHNOLOGY

Process the Results


● To process the results, you can traverse the rows of the result set using the
ResultSet object's next() and previous() methods
● The following sample code creates a Statement object, executes a query, and
iterates through the result set.
● The ResultSet object's getString() method is used to extract the value of
specific fields.
Statement stmt = dbConn.createStatement();
ResultSet rs = stmt.executeQuery("select * from employee");
while (rs.next())
{
System.out.println(rs.getString("FIRST_NAME") +" " +
rs.getString("LAST_NAME"));
}
IT8501
WEB TECHNOLOGY

Close the Database Connection

● Since database connections


are a valuable and limited
resource, you should close the
connection when processing is
complete.
● The Connection object
provides a simple close()
method for this purpose.
● In addition to closing the
database connection, you
should also explicitly close all
Statement and ResultSet
objects using their close()
methods.
IT8501
WEB TECHNOLOGY

MySql-Sample Database Creation

● CREATE TABLE DEPARTMENT (


DEPARTMENT_ID int NOT NULL AUTO_INCREMENT
PRIMARY KEY,
NAME varchar(20) NULL );

● CREATE TABLE EMPLOYEE (


EMPLOYEE_ID int NOT NULL AUTO_INCREMENT PRIMARY KEY,
DEPARTMENT_ID int NULL,
NAME varchar(20) NULL,
PHONE varchar(20) NULL,
EMAIL varchar(50) NULL,
HIRE_ DATE date NULL );
IT8501
WEB TECHNOLOGY

Simple Database Java Servlet – Interface with MySql Database


IT8501
WEB TECHNOLOGY

Step 1: Creation of Database and Table in MySQL


mysql> create database demoprj;
Query OK, 1 row affected (4.10 sec)

mysql> use demoprj


Database changed

mysql> create table demo(id int(10), string varchar(20));


Query OK, 0 rows affected (1.93 sec)

mysql> desc demo;


+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id | int(10) | YES | | NULL | |
| string | varchar(20) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
2 rows in set (0.40 sec)
IT8501
WEB TECHNOLOGY

Step 2: Implementation of required Web-pages

● Create a form in HTML file, where take all the inputs required to insert data
into the database. Specify the servlet name in it.
IT8501
WEB TECHNOLOGY

Step 3: Creation of Java Servlet program with JDBC Connection

To create a JDBC Connection following steps are implemented

1. Import all the packages

2. Register the JDBC Driver

3. Open a connection

4. Execute the query, and retrieve the result

5. Clean up the JDBC Environment


IT8501
WEB TECHNOLOGY

Step 3: Creation of Java Servlet program with JDBC Connection


Create a separate class to create a connection of database, following java program returns a Connection
object.
IT8501
WEB TECHNOLOGY

Step 4: To use this class method, create an object in Java Servlet


program
.
Java program given below shows Servlet Class which create a connection and
insert the data in the demo table,
IT8501
WEB TECHNOLOGY

Step 4: create an object in Java Servlet program (Contd…)


.
IT8501
WEB TECHNOLOGY

Step 5: Get the data from the HTML file


● To get the data from the HTML file, the request object is used which
calls getParameter() Method to fetch the data from the channel. After successful
insertion, the writer object is created to display a success message.
● After insertion operation from Servlet, data will be reflected in MySQL Database
Output:
IT8501
WEB TECHNOLOGY

Video Links
1. Introduction to JDBC :

https://www.c-sharpcorner.com/article/introduction-to-jdbc/

2. Java Servlet with JDBC :

https://youtu.be/-x4qIXzl6RY

3. Application Development

https://www.youtube.com/watch?v=jiGFhqD5elk

You might also like