JDBC

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Unit III Server side programming

Installing and Configuring Apache Tomcat Web Server;- DATABASE


CONNECTIVITY: JDBC perspectives, JDBC program example

INSTALLING AND CONFIGURING APACHE TOMCAT WEB SERVER



The Apache Jakarta Project “creates and maintains open source solutions on the Java
platform for distribution to the public at no charge”
• Apache Jakarta Tomcat--or just “Tomcat”--is one of those projects
• Tomcat is a container for servlets
• Tomcat can act as a simple standalone server for Web applications that use HTML, servlets,
and JSP
• Apache is an industrial-strength, highly optimized server that can be extended with Tomcat
Apache Tomcat7.0.XX new released
• The Apache Tomcat team has released version 7.0.40 of Apache.
• They removed several problems that stops Tomcat attempting to parse text, improved
handling and reporting if a Concurrent Modification Exception occurs while checking for
memory leaks, etc.
STEPS FOR INSTALLING TOMCAT 7
There are certain steps we must follow for configuring Apache Tomcat 7.
Step 1 : Download and Install Tomcat
1. Go to http://tomcat.apache.org/download-70.cgi then go to the Binary Distribution/Core/
and download the "zip" package (for example "apache-tomcat-7.0.40.zip", about 8 MB).
2. Now unzip the downloaded file into any directory(E:\)
Step 2: Check the installed directory to ensure it contains the following sub-directories:
• bin folder
• logs folder
• webapps folder
• work folder
• temp folder
• conf folder
• lib folder
Step 3
Now, create an Environment Variable "JAVA_HOME" and set it to the JDK installed directory.
1. To create the JAVA_HOME environment variable in Windows XP/Vista/7 go to "Start"
button then select "Control Panel" / "System" / "Advanced system settings". Then switch to the
"Advanced" tab and select "Environment Variables" / "System Variables" then select "New" (or
"Edit" for modification).
In "Variable Name", enter JAVA_HOME". In "Variable Value", enter your JDK
installed directory (e.g., "c:\Program Files\Java\jdk1.7.0_{xx}").
2. For ensuring that it is set correctly, start a command prompt (to refresh the environment) and
issue: set JAVA_HOME
JAVA_HOME=c:\Program Files\Java\jdk1.7.0_{xx} <== Check that this is OUR JDK installed
directory.
Step 4 Configure Tomcat Server
The configuration files of the Apache Tomcat Server are located in the "conf" sub-directory of
our Tomcat installed directory, for example "E:\myserver\tomcat7.0.40\conf". There are 4
configuration XML files:
1. context.xml file
2. tomcat-users.xml file
3. server.xml file
4. web.xml file
Before proceeding, make a BACKUP of the configuration files.

Step 5 :Now, start the tomcat server Executable programs and scripts are kept in the "bin" sub-directory
of the Tomcat installed directory, e.g., "E:\myserver\tomcat7.0.40\bin".
Step 5(a) Start Server Launch a command shell. Set the current directory to "\bin" like
E:\myserver\tomcat7.0.40\bin, and run "startup.bat" as follows:

After that a new Tomcat console window appears. Read the messages on the console. Look out for the
Tomcat's port number (double check that Tomcat is running on port 9999)....... We saw a figure like:
JDBC(JAVA DATABASE CONNECTIVITY):

• JDBC stands for Java Database Connectivity.


• JDBC is a standard Java API to connect and execute the query with the database.

Why use JDBC?


Before JDBC, ODBC API was the database API to connect and execute query with the database. But,
ODBC API uses ODBC driver which is written in C language (i.e. platform dependent and unsecured).
That is why Java has defined its own API (JDBC API) that uses JDBC drivers (written in Java
language).

What is API?
API (Application programming interface) is a document that contains description of all the features of
a product or software. It represents classes and interfaces that software programs can follow to
communicate with each other. An API can be created for applications, libraries, operating systems, etc.

JDBC – Architecture:
The JDBC Architecture consists of two layers: the JDBC API, which provides the application-to-
JDBC Manager connection, and the JDBC Driver API, which supports the JDBC Manager-to-Driver
Connection.
Common JDBC Components
The JDBC API provides the following interfaces and classes −
DriverManager: This class manages a list of database drivers. Matches connection requests from the
java application with the proper database driver using communication sub protocol. The first driver that
recognizes a certain sub protocol under JDBC will be used to establish a database Connection.
Driver: This interface handles the communications with the database server. You will interact directly
with Driver objects very rarely. Instead, you use DriverManager objects, which manages objects of this
type. It also abstracts the details associated with working with Driver objects.
Connection: This interface with all methods for contacting a database. The connection object
represents communication context, i.e., all communication with database is through connection object
only.
Statement: You use objects created from this interface to submit the SQL statements to the database.
Some derived interfaces accept parameters in addition to executing stored procedures.
There are 3 types of Statements, as given below:
• Statement: It can be used for general-purpose access to the database. It is useful when you are
using static SQL statements at runtime.
• PreparedStatement: It can be used when you plan to use the same SQL statement many times.
The PreparedStatement interface accepts input parameters at runtime.
• Callable Statement: Callable Statement can be used when you want to access database stored
procedures.
ResultSet: These objects hold data retrieved from a database after you execute an SQL query using
Statement objects. It acts as an iterator to allow you to move through its data.
SQLException: This class handles any errors that occur in a database application.
JDBC Driver is a software component that enables java application to interact with the database.
There are 4 types of JDBC drivers:
• JDBC-ODBC bridge driver
• Native-API driver (partially java driver)
• Network Protocol driver (fully java driver)
• Thin driver (fully java driver)

Type I:JDBC-ODBC bridge driver


• The JDBC-ODBC bridge driver uses ODBC driver to connect to the database. The JDBC
ODBC bridge driver converts JDBC method calls into the ODBC function calls. This is now
discouraged because of thin driver.

Type II:Native-API driver (partially java driver:


• The Native API driver uses the client-side libraries of the database. The driver converts JDBC
method calls into native calls of the database API. It is not written entirely in java.
Type III:Network Protocol driver.
• The Network Protocol driver uses middleware (application server) that converts JDBC calls
directly or indirectly into the vendor-specific database protocol. It is fully written in java.

Type IV:Thin driver


• The thin driver converts JDBC calls directly into the vendor-specific database protocol. That is
why it is known as thin driver. It is fully written in Java language.
STEPS IN JDBC STEPS:
1. import packages
2. Registering JDBC drivers
3. Opening a Connection to a database
4. Create a Statement object
5. Execute a query and Returning a result
6. Processing the ResultSet
7. Closing the ResultSet
8. Closing the connection

Step 1: import packages


Import the required packages for JDBC. import java.sql.*;
Step 2: Registering JDBC drivers
The forName() method of Class class is used to register the driver class. This method is used to
dynamically load the driver class.

Class.forName("oracle.jdbc.driver.OracleDriver");

Step 3: Opening a Connection to a database


The getConnection() method of DriverManager class is used to establish connection with the database.
Connection con=DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe","system","password");
Step 4: Create a Statement object

The createStatement() method of Connection interface is used to create statement. The object of
statement is responsible to execute queries with the database.
Statement stmt=con.createStatement();
Step 5: Execute a query and Returning a result
The executeQuery() method of Statement interface is used to execute queries to the database. This
method returns the object of ResultSet that can be used to get all the records of a table.

ResultSet rs=stmt.executeQuery("select * from emp");


Step 6: Processing the ResultSet

next():Used to move the cursor to the one row next from the current position.
getInt(column index):Used to return the data of specified column index of the current row as int.
while(rs.next())
{
int id = rs.getInt(1); // index 1 is the "id" column
String name = rs.getString(2); // index 2 is the "name" column
System.out.println(id);
System.out.println(name)
}

Step 7: Closing the connection

By closing connection object statement and ResultSet will be closed automatically. The close() method
of Connection interface is used to close the connection.

con.close();

Example program for JDBC:

import java.sql.*;
class MysqlCon
{
public static void main(String args[])
{
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306
/mark","root","root");
//here mark is the database name, root is the username and root is the password
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from emp");
while(rs.next())
{
int id = rs.getInt(1); // index 1 is the "id" column
String name = rs.getString(2); // index 2 is the "name" column
System.out.println(id);
System.out.println(name)
}
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}

You might also like