Course: Java Programming
(CET2031B)
SCHOOL OF COMPUTER ENGINEERING AND TECHNOLOGY
Java Programming 1
Lab 5
Problem Statement
Develop a simple web application using the concept of Servlets, JSP, and database
connectivity
Java Programming 2
Objective of the Assignment
• To understand server-side scripting
• To understand the concept of Servlet and JSP
• To learn the working of Servlet and JSP
• To learn the database connectivity using MySQL
• To develop web application using Servlet, JSP and database
connectivity
Java Programming 3
JDBC
• JDBC stands for Java Database Connectivity. JDBC is a Java API
to connect and execute the query with the database. It is a
part of JavaSE (Java Standard Edition). JDBC API uses JDBC
drivers to connect with the database. There are four types of
JDBC drivers:
– JDBC-ODBC Bridge Driver,
– Native Driver,
– Network Protocol Driver, and
– Thin Driver
• We can use JDBC API to access tabular data stored in any
relational database. By the help of JDBC API, we can save,
update, delete and fetch data from the database. It is like
Open Database Connectivity (ODBC) provided by Microsoft.
Java Programming 4
JDBC
The current version of JDBC is 4.3.
The java.sql package contains classes and interfaces for JDBC API.
Java Programming 5
JDBC Interfaces and Classes
JDBC Interfaces JDBC Classes
• Driver interface • DriverManager class
• Connection interface • Blob class
• Statement interface • Clob class
• PreparedStatement interface • Types class
• CallableStatement interface
• ResultSet interface
• ResultSetMetaData interface
• DatabaseMetaData interface
• RowSet interface
Java Programming 6
JDBC Driver
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)
1) 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.
Java Programming 7
JDBC ODBC Driver
Java Programming 8
Java Database Connectivity with MySQL
• To connect Java application with the MySQL database, we need to
follow 5 following steps.
1. Driver class: The driver class for the mysql database
is com.mysql.jdbc.Driver.
2. Connection URL: The connection URL for the mysql database
is jdbc:mysql://localhost:3306/sonoo where jdbc is the API, mysql is
the database, localhost is the server name on which mysql is running,
we may also use IP address, 3306 is the port number and sonoo is the
database name. We may use any database, in such case, we need to
replace the sonoo with our database name.
3. Username: The default username for the mysql database is root.
4. Password: It is the password given by the user at the time of installing
the mysql database. In this example, we are going to use root as the
password.
Java Programming 9
Database Creation
First create a table in the mysql database, but before creating
table, we need to create database first.
create database sonoo;
use sonoo;
create table emp(id int(10),name varchar(40),age int(3));
Java Programming 10
Example to Connect Java Application with mysql database
sonoo is the database name, root is the username and
password both.
1. import java.sql.*;
2. class MysqlCon{
3. public static void main(String args[]){
4. try{
5. Class.forName("com.mysql.jdbc.Driver");
6. Connection con=DriverManager.getConnectio
n(
7. "jdbc:mysql://localhost:3306/
sonoo","root","root");
8. //
here sonoo is database name, root is usernam
e and password
9. Statement stmt=con.createStatement();
10.ResultSet rs=stmt.executeQuery("select * fro
m emp");
11.while(rs.next())
12.System.out.println(rs.getInt(1)+" "+rs.getStr
ing(2)+" "+rs.getString(3));
13.con.close();
The above example will fetch all the records
14.}catch(Exception e)
of emp table.
{ System.out.println(e);}
Java Programming 11
15.}
Ways to load jar file
• To connect java application with the mysql database, mysqlconnector.jar file is required to be
loaded.
• Two ways to load the jar file:
1. Paste the mysqlconnector.jar file in jre/lib/ext folder
2. Set classpath
1) Paste the mysqlconnector.jar file in JRE/lib/ext folder:
• Download the mysqlconnector.jar file. Go to jre/lib/ext folder and paste the jar file here.
2) Set classpath:
• There are two ways to set the classpath:
• Temporary
• Permanent
• How to set the temporary classpath
open command prompt and write:
C:>set classpath=c:\folder\mysql-connector-java-5.0.8-bin.jar;.;
• How to set the permanent classpath
Go to environment variable then click on new tab. In variable name write classpath
and in variable value paste the path to the mysqlconnector.jar file by appending
mysqlconnector.jar;.; as C:\folder\mysql-connector-java-5.0.8-bin.jar;.;
Java Programming 12
Thank You!!
Java Programming 13