DATABASE CONNECTIVITY WITH SERVLETS
Now we shall study how we can connect servlet to database. This can be done with
the help of JDBC (Java Database Connectivity). Servlets, with their enduring life
cycle, and JDBC, a well-defined database-independent database connectivity API, are
an elegant and efficient combination and solution for webmasters who require to
connect their web sites to back-end databases
the help of JDBC (Java Database Connectivity). Servlets, with their enduring life
cycle, and JDBC, a well-defined database-independent database connectivity API, are
an elegant and efficient combination and solution for webmasters who require to
connect their web sites to back-end databases
The advantage of servlets over CGI and many other technologies is that JDBC is
database-independent. A servlet written to access a Sybase database can, with a twoline
modification or a change in a properties file, begin accessing an Oracle database.
database-independent. A servlet written to access a Sybase database can, with a twoline
modification or a change in a properties file, begin accessing an Oracle database.
One common place for servlets, especially servlets that access a database, is in what’s
called the middle tier. A middle tier is something that helps connect one endpoint to
another (a servlet or applet to a database, for example) and along the way adds a little
something of its own. The middle tier is used between a client and our ultimate data
source (commonly referred to as middleware) to include the business logic.
called the middle tier. A middle tier is something that helps connect one endpoint to
another (a servlet or applet to a database, for example) and along the way adds a little
something of its own. The middle tier is used between a client and our ultimate data
source (commonly referred to as middleware) to include the business logic.
Let us understand the database connectivity of servlet with table with the help of an
example. The following example shows a very simple servlet that uses the MSAccess
JDBC driver to perform a simple query, printing names and phone numbers
for all employees listed in a database table. We assume that the database contains a
table named CUSTOMER, with at least two fields, NAME and ADDRESS
example. The following example shows a very simple servlet that uses the MSAccess
JDBC driver to perform a simple query, printing names and phone numbers
for all employees listed in a database table. We assume that the database contains a
table named CUSTOMER, with at least two fields, NAME and ADDRESS
/* Example to demonstrate how JDBC is used with Servlet to connect to a customer
table and to display its records*/
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class DBPhoneLookup extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
table and to display its records*/
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class DBPhoneLookup extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
res.setContentType("text/html");
PrintWriter out = res.getWriter();
try {
// Load (and therefore register) the Oracle Driver
Class.forName(sun.jdbc.odbc.JdbcOdbcDriver);
// Get a Connection to the database
Connection con = DriverManager.getConnection (jdbc:odbc:Access);
// Create a Statement object
stmt = con.createStatement();
// Execute an SQL query, get a ResultSet
rs = stmt.executeQuery("SELECT NAME, ADDRESS FROM CUSTOMER");
// Display the result set as a list
out.println("<HTML><HEAD><TITLE>Phonebook</TITLE></HEAD>");
Statement stmt = null;
ResultSet rs = null;
res.setContentType("text/html");
PrintWriter out = res.getWriter();
try {
// Load (and therefore register) the Oracle Driver
Class.forName(sun.jdbc.odbc.JdbcOdbcDriver);
// Get a Connection to the database
Connection con = DriverManager.getConnection (jdbc:odbc:Access);
// Create a Statement object
stmt = con.createStatement();
// Execute an SQL query, get a ResultSet
rs = stmt.executeQuery("SELECT NAME, ADDRESS FROM CUSTOMER");
// Display the result set as a list
out.println("<HTML><HEAD><TITLE>Phonebook</TITLE></HEAD>");
out.println("<BODY>");
out.println("<UL>");
while(rs.next()) {
out.println("<LI>" + rs.getString("name") + " " + rs.getString("address"));
}
out.println("</UL>");
out.println("</BODY></HTML>");
}
catch(ClassNotFoundException e) {
out.println("Couldn't load database driver: " + e.getMessage());
}
catch(SQLException e) {
out.println("SQLException caught: " + e.getMessage());
}
finally {
// Always close the database connection.
try {
if (con != null) con.close();
}
out.println("<UL>");
while(rs.next()) {
out.println("<LI>" + rs.getString("name") + " " + rs.getString("address"));
}
out.println("</UL>");
out.println("</BODY></HTML>");
}
catch(ClassNotFoundException e) {
out.println("Couldn't load database driver: " + e.getMessage());
}
catch(SQLException e) {
out.println("SQLException caught: " + e.getMessage());
}
finally {
// Always close the database connection.
try {
if (con != null) con.close();
}
catch (SQLException e1) { } Servlet Programming
}
}
}
In the above example a simple servlet program is written to connect to the database,
and which executes a query that retrieves the names and phone numbers of everyone
in the employees table, and display the list to the user.
}
}
}
In the above example a simple servlet program is written to connect to the database,
and which executes a query that retrieves the names and phone numbers of everyone
in the employees table, and display the list to the user.
No comments:
Post a Comment