J2EE
J2EE
1. J2EE stands for Java 2 Enterprise Edition where ‘2’ stands for version.
ii) Servlet
Abstraction
1. Interface
2. Abstract class
Abstract class
It is a component in Java which is used to achieve 100% abstraction and multiple inheritance.
It is a medium which is used to connect 2 applications (It is a medium of communication
between service provider and service user)
Note:
Q. Define a method which can accept a no. and check whether it is Even or odd.
Import java.util.Scanner;
class EOO
int n=sc.nextInt();
boolean rs=isBoolean(n);
if(rs==true)
System.out.println(n+“ is Even”);
else
System.out.println(n+“ is Odd”);
int n;
if(n%2==0)
return true;
else
return false;
}}
Q. Define a method which can accept the reference of book and print its attributes.
class EBook
String title=”Binge”;
String author=”Stephen”;
double price=540.0;
this.title=title;
this.author=author;
this.price=price;
}}
-class printDetails
printDetails(a);
printDetails(b);
System.out.println(“Title: ”+a.title);
System.out.println(“Author: ”+a.author);
System.out.println(“Price: ”+a.price);
}}
18/01/2023
Coupling
Tight Coupling
Loose Coupling
Change in the implementation does not affect the user then it is Loose Coupling.
Method
Note:
Q.
class Laptop
String brand;
double price;
static void printDetails(int ram, int storage, String brand, double price)
this.ram=ram;
this.storage=storage;
this.brand=brand;
this.price=price;
}}
-class TestLaptop
printDetails(l1);
printDetails(l2);
printDetails(l3);
System.out.println(“Ram : ”+laptop.ram);
System.out.println(“Storage : ”+laptop.storage);
System.out.println(“Brand : ”+laptop.brand);
System.out.println(“Price : ”+laptop.price);
System.out.println(“-----------------------------------“);
}}
19/01/2023
Note:
Ex.
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
class Demo
iterator<Integer> I = nums.iterator();
-In the above example iterator() is a method which is implemented in ArrayList and it will create the
implementation object of java.util.Iterator and returns the reference.
-We cannot create the object of an interface so if we want to call the methods of an interface then
we must create the implementation object of an interface.
Ex.
interface Vehicle
@Override
void start();
-class testVehicle
v.start();
20/01/23
Design Pattern
Note: In factory design pattern we will have a Hesper method or factory method. Which will take
string as argument and produces the object and return the reference.
Parameter
Parameter is used to represent the type of argument or it is used to hold the argument.
Argument
Factory
Bike Car
1. Implementation Logic
2. Object Creation Logic
3. Utilisation Logic
Object Creation Logic: It is used to create the object for implemented classes.
void Start();
-package org.jsp;
@override
{
@override
if(type.equals(“Bike”))
return newBike();
else if(type.equals(“Car”))
else
return null;
-package org.jsp;
import java.util.Scanner;
Vehicle v = factory.getVehicle(type);
If(v!=null)
v.start();
}}
21/01/23
class VehicleFactory
return null;
void Start()
@override
It is a compressed file format which is similar to zip file format. It is used to compress different types
of files together.
Contents of a JAR
.xml
.properties (To store the config data in the form of key=value pairs. Where key should be unique.)
Config (It is used to store the configuration files).
Used to config the resources with the help of customised tags or User defined tags.
3. Now Select the project which you want to compress and click on browse.
4. Browse the directory where you want to export the JAR file.
To import the properties of a resource we need a JAR file. We can add JAR file into Java build path in
two ways
3. Select the class Path and click on add external JAR and browse the directly where JAR file is
present.
1. Right click on your project and create a folder with the name lib.
3.3 Click on AddJARS and browse the lib folder where the JAR file is present.
JDBC
It is a specification provided in the form of abstraction API which is used to achieve loose
coupling between java application and the database server.
24/01/23
The process of loading a .class file into JVM memory. We can load a class into the JVM memory in
two ways.
-Test.java
class test
static {
-Demo.java
class Demo
static
{
System.out.println(“Demo class is loaded into JVM memory”);
System.out.println(“Main Starts”);
test.name = “ABC”;
System.out.println(“Main Ends”);
-package.org.btm;
class Student
static {
-package.org.jsp;
class ClassRoom
static {
try {
Class.forName(“org.btm.Student”);
catch(ClassNotFoundException e) {
System.out.println(“Invalid Class Name”);
e.printStackTrace();
}}
25/01/23
1. Protocol
2. Domain Name / Host + Port Number
3. Resource
Protocol
Host
1) Local Host
2) Remote Host
Local Host
Remote Host
Port Number
The port number to connect with different database servers are as follows:
MySQL - 3306
Oracle - 1521
JDBC
It is a specification which has been given in the form of abstraction API which is used to achieve loose
coupling between java application and the database server.
Specifications of JDBC
1. Every JDBC Driver should have a Driver class which is implementing java.sql.Driver interface.
26/01/23
Costly resource
Any resource which uses the system properties in the form of stream.
All the costly resources must be closed once the task is completed.
If we don’t close the costly resource then the efficiency of the application will decrease.
27/01/23
Driver Interface
It is an interface belong to JDBC API.
It belongs to java.sql package.
Driver Class
JDBC driver
1. Manually
2. Using forName
import java.sql.DriverManager;
import java.sql.SQLException;
import com.mysql.cj.jdbc.Driver;
class LoadAndRegisterDemo
try{
DriverManager.registerDriver(d);
} catch (SQLException e) {
e.printStackTrace();
Output:
31/01/23
Note:
The above way of loading and registering the driver will result in tight coupling.
Step 1: Loading and registering the driver with the help of forName method.
Ex.
class TestLoadAndRegister
try{
Class.forName(“com.mysql.cj.jdbc.Driver”);
}catch (ClassNotFoundException e) {
e.printStackTrace();
Output:
Step 2: Establish the connection between Java Application and database server.
01/02/23
java.sql.DriverManager
getConnection
This method is overloaded. Follo are the different types of getConnection() method.
1. getConnection(String url)
It is an interface present in JDBC API the implementation for connection is provided by vendors as a
part of JDBC Driver.
Note: All the costly resources must be closed in finally block by using if condition to avoid
NullPointerException.
Ex.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
class TestEstablishConnection
Try{
Class.forName(“com.sql.cj.jdbc.Driver”);
con =DriverManager.getConnection(“jdbc:mysql://localhost:3306”,”root”,”password”);
catch(ClassNotFoundException| SQLException e)
e.printStackTrace();
} finally
if(con!=null)
{
try {
con.close();
System.out.println(“Connection is closed”);
catch (SQLException e)
e.printStackTrace();
02/02/2023
CreateStatement()
This method will create the implement object of java.sql.Statement and returns the reference.
Ex.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
{
public static void main(String[] args)
Statement st = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306","root",
"ankush.rana11");
st = con.createStatement();
System.out.println("Platform Created");
catch(ClassNotFoundException | SQLException e)
e.printStackTrace();
finally
if(con!=null)
try
con.close();
System.out.println("Connection is closed");
catch (SQLException e)
{
e.printStackTrace();
if(st!=null)
try
st.close();
System.out.println("Statement is closed");
catch (SQLException e)
e.printStackTrace();
3/02/23
execute():
This method is used to execute any type of SQL query (Generic for all types of queries like
DDL, DML, DQL).
The returntype of this method is boolean.
It returns true if we execute DQL queries and returns false for if we execute other types of
query.
Method Declaration: public boolean execute(String query)
executeUpdate():
executeQuery():
Code to create the user table with the help of Statement interface
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
Statement st = null;
String query="create table user (id int not null, name varchar(45) null,
phone bigint(20) null, primary key(id))";
try {
Class.forName("com.mysql.cj.jdbc.Driver");
st = con.createStatement();
System.out.println("Platform Created");
boolean res=st.execute(query);
catch(ClassNotFoundException | SQLException e)
e.printStackTrace();
finally
if(con!=null)
try
con.close();
System.out.println("Connection is closed");
catch (SQLException e)
e.printStackTrace();
if(st!=null)
try
{
st.close();
System.out.println("Statement is closed");
catch (SQLException e)
e.printStackTrace();
04/02/23
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
Statement st = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc_demo","root",
"ankush.rana11");
st = con.createStatement();
System.out.println("Platform Created");
int rs = st.executeUpdate(query);
catch(ClassNotFoundException | SQLException e)
e.printStackTrace();
finally
if(con!=null)
try
con.close();
System.out.println("Connection is closed");
catch (SQLException e)
e.printStackTrace();
if(st!=null)
{
try
st.close();
System.out.println("Statement is closed");
catch (SQLException e)
e.printStackTrace();
06/02/23
Query to update
Ex.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
Statement st = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc_demo","root",
"ankush.rana11");
st = con.createStatement();
System.out.println("Platform Created");
int rs = st.executeUpdate(query);
catch(ClassNotFoundException | SQLException e)
e.printStackTrace();
finally
if(con!=null)
try
con.close();
System.out.println("Connection is closed");
}
catch (SQLException e)
e.printStackTrace();
if(st!=null)
try
st.close();
System.out.println("Statement is closed");
catch (SQLException e)
e.printStackTrace();
Code to delete a code from the user table using statement interface:
Ex.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
Statement st = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc_demo","root",
"ankush.rana11");
st = con.createStatement();
System.out.println("Platform Created");
int rs = st.executeUpdate(query);
catch(ClassNotFoundException | SQLException e)
e.printStackTrace();
finally
if(con!=null)
try
con.close();
System.out.println("Connection is closed");
catch (SQLException e)
e.printStackTrace();
if(st!=null)
try
st.close();
System.out.println("Statement is closed");
catch (SQLException e)
e.printStackTrace();
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
Statement st = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc_demo","root", "ankush.rana11");
st = con.createStatement();
System.out.println("Platform Created");
int rs = st.executeUpdate(query);
catch(ClassNotFoundException | SQLException e)
e.printStackTrace();
finally
if(con!=null)
try
con.close();
System.out.println("Connection is closed");
catch (SQLException e)
e.printStackTrace();
if(st!=null)
try
st.close();
System.out.println("Statement is closed");
catch (SQLException e)
e.printStackTrace();
07/02/23
If we execute a DQL query we might get a result which is referred as Resultant data (or) processed
data, which is present in Cursor memory (or) Buffer memory.
We can fetch the Resultant data with the help of java.sql.ResultSet interface.
We can create the implementation object of ResultSet with the help of 2 methods:
1. getResultSet():
It is a helper method present in statement interface.
It will create the implementation object of java.sql.ResultSet and returns the reference.
So the returntype of this method is ResultSet.
2. executeQuery():
This method is specific to execute only DQL queries.
The returntype of this method is ResultSet.
This method returns the reference of resultSet.
If we execute any query other than DQL it throws SQLException.
java.sql.ResultSet:
get***(int column_Index)
get***(String column_label)
next():
This method is used to check whether a record is present in the cursor memory or not.
Note:
08/02/23
1. getLong (int)
2. getLong (String) -> returntype = long
1. getString (int)
2. getString (String) -> returntype = String
If datatype is double
1. getDouble (int)
2. getString (String) -> returntype = String
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
Statement st = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc_demo","root",
"ankush.rana11");
st = con.createStatement();
System.out.println("Platform Created");
while(rs.next())
catch(ClassNotFoundException | SQLException e)
e.printStackTrace();
finally
if(con!=null)
{
try
con.close();
System.out.println("Connection is closed");
catch (SQLException e)
e.printStackTrace();
if(st!=null)
try
st.close();
System.out.println("Statement is closed");
catch (SQLException e)
e.printStackTrace();
if(rs!=null)
try
rs.close();
System.out.println("ResultSet Closed");
}
catch(SQLException e)
e.printStackTrace();
09/02/23
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
Statement st = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
st = con.createStatement();
System.out.println("Platform Created");
st.executeUpdate(qry1);
st.executeUpdate(qry2);
st.executeUpdate(qry3);
catch(ClassNotFoundException | SQLException e)
e.printStackTrace();
finally
if(con!=null)
try
con.close();
System.out.println("Connection is closed");
catch (SQLException e)
e.printStackTrace();
if(st!=null)
{
try
st.close();
System.out.println("Statement is closed");
catch (SQLException e)
e.printStackTrace();
Note: When we insert multiple records using statement interface the query will be compiled multiple
times. This will decrease the efficiency.
Using preparedStatement we can write dynamic queries which can be compiled only once and can be
executed multiple times.
PlaceHolder
It is a parameter which is used to hold dynamic values during from the user during execution.
09/02/23
1. We should assign the values for the PlaceHolder before the execution of the query.
2. The number of values must be same as the number of PlaceHolders.
3. We can assign the value for PlaceHolder using a method set***(int index, ***value).
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.PreparedStatement;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc_demo",
"root", "ankush.rana11");
pst = con.prepareStatement(qry);
pst.setInt(1, 7);
pst.setString(2, "A");
pst.setLong(3, 99);
pst.executeUpdate();
pst.setInt(1, 8);
pst.setString(2, "B");
pst.setLong(3, 9999);
pst.executeUpdate();
pst.setInt(1, 9);
pst.setString(2, "c");
pst.setLong(3, 9999);
pst.executeUpdate();
e.printStackTrace();
} finally {
if (con != null) {
try {
con.close();
System.out.println("Connection is closed");
} catch (SQLException e) {
e.printStackTrace();
if (pst != null) {
try {
pst.close();
System.out.println("Statement is closed");
} catch (SQLException e) {
e.printStackTrace();
}
}
11/02/23
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.util.Scanner;
int id = sc.nextInt();
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc_demo",
"root", "ankush.rana11");
pst = con.prepareStatement(qry);
pst.setInt(1, id);
pst.setString(2, name);
pst.setLong(3, phone);
pst.executeUpdate();
e.printStackTrace();
} finally {
if (con != null) {
try {
con.close();
System.out.println("Connection is closed");
} catch (SQLException e) {
e.printStackTrace();
if (pst != null) {
try {
pst.close();
System.out.println("Statement is closed");
} catch (SQLException e) {
e.printStackTrace();
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.util.Scanner;
int id = sc.nextInt();
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc_demo",
"root", "ankush.rana11");
pst = con.prepareStatement(qry);
pst.setInt(3, id);
pst.setString(1, name);
pst.setLong(2, phone);
pst.executeUpdate();
} finally {
if (con != null) {
try {
con.close();
System.out.println("Connection is closed");
} catch (SQLException e) {
e.printStackTrace();
if (pst != null) {
try {
pst.close();
System.out.println("Statement is closed");
} catch (SQLException e) {
e.printStackTrace();
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.util.Scanner;
public class DeleteUserInputPreparedStatement {
int id = sc.nextInt();
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc_demo",
"root", "ankush.rana11");
pst = con.prepareStatement(qry);
pst.setInt(1, id);
pst.executeUpdate();
e.printStackTrace();
} finally {
if (con != null) {
try {
con.close();
System.out.println("Connection is closed");
} catch (SQLException e) {
e.printStackTrace();
}
if (pst != null) {
try {
pst.close();
System.out.println("Statement is closed");
} catch (SQLException e) {
e.printStackTrace();
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Scanner;
ResultSet rs = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc_demo",
"root", "ankush.rana11");
pst=con.prepareStatement(qry);
pst.setInt(1, id);
rs=pst.executeQuery();
while(rs.next())
e.printStackTrace();
} finally {
if (con != null) {
try {
con.close();
System.out.println("Connection is closed");
} catch (SQLException e) {
e.printStackTrace();
if (pst != null) {
try {
pst.close();
System.out.println("Statement is closed");
} catch (SQLException e) {
e.printStackTrace();
}
if (rs != null) {
try {
rs.close();
System.out.println("ResultSet is closed");
} catch (SQLException e) {
e.printStackTrace();
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Scanner;
ResultSet rs = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc_demo",
"root", "ankush.rana11");
pst=con.prepareStatement(qry);
pst.setString(1, name);
rs=pst.executeQuery();
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString("name")+"
"+rs.getLong(3));
e.printStackTrace();
} finally {
if (con != null) {
try {
con.close();
System.out.println("Connection is closed");
} catch (SQLException e) {
e.printStackTrace();
if (pst != null) {
try {
pst.close();
System.out.println("Statement is closed");
} catch (SQLException e) {
e.printStackTrace();
if (rs != null) {
try {
rs.close();
System.out.println("ResultSet is closed");
} catch (SQLException e) {
e.printStackTrace();
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Scanner;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc_demo",
"root", "ankush.rana11");
pst=con.prepareStatement(qry);
pst.setLong(1, phone);
rs=pst.executeQuery();
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2)+"
"+rs.getLong("phone"));
e.printStackTrace();
} finally {
if (con != null) {
try {
con.close();
System.out.println("Connection is closed");
} catch (SQLException e) {
e.printStackTrace();
if (pst != null) {
try {
pst.close();
System.out.println("Statement is closed");
} catch (SQLException e) {
e.printStackTrace();
if (rs != null) {
try {
rs.close();
System.out.println("ResultSet is closed");
} catch (SQLException e) {
e.printStackTrace();
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.util.Scanner;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc_demo",
"root", "ankush.rana11");
pst = con.prepareStatement(qry);
pst.setString(1, name);
pst.executeUpdate();
e.printStackTrace();
} finally {
if (con != null) {
try {
con.close();
System.out.println("Connection is closed");
} catch (SQLException e) {
e.printStackTrace();
if (pst != null) {
try {
pst.close();
System.out.println("Statement is closed");
} catch (SQLException e) {
e.printStackTrace();
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.util.Scanner;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc_demo",
"root", "ankush.rana11");
System.out.println("Connection has been established");
pst = con.prepareStatement(qry);
pst.setLong(1, phone);
pst.executeUpdate();
e.printStackTrace();
} finally {
if (con != null) {
try {
con.close();
System.out.println("Connection is closed");
} catch (SQLException e) {
e.printStackTrace();
if (pst != null) {
try {
pst.close();
System.out.println("Statement is closed");
} catch (SQLException e) {
e.printStackTrace();
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Scanner;
ResultSet rs = null;
int id = sc.nextInt();
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc_demo",
"root", "ankush.rana11");
pst=con.prepareStatement(qry);
pst.setInt(1, id);
pst.setLong(2, phone);
rs=pst.executeQuery();
while(rs.next())
System.out.println(rs.getInt("id")+" "+rs.getString(2)+"
"+rs.getLong("phone"));
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally {
if (con != null) {
try {
con.close();
System.out.println("Connection is closed");
} catch (SQLException e) {
e.printStackTrace();
if (pst != null) {
try {
pst.close();
System.out.println("Statement is closed");
} catch (SQLException e) {
e.printStackTrace();
if (rs != null) {
try {
rs.close();
System.out.println("ResultSet is closed");
} catch (SQLException e) {
e.printStackTrace();
}
}
14/02/23
Batch execution
Batch
Picture!!!!!!!
1. addBatch()
This method is used to add a DML query into the batch.
2. executeBatch()
It is used to execute all the DML queries present in the batch.
It return an integer array(int[]) where each element represent the number of rows affected
due to the specific query.
The number of elements present in int array(size) is same as the number of DML queries
present in Batch.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
Statement st = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc_demo",
"root", "ankush.rana11");
st=con.createStatement();
int[] r = st.executeBatch();
for(int i=0;i<r.length;i++)
e.printStackTrace();
} finally {
if (con != null) {
try {
con.close();
System.out.println("Connection is closed");
} catch (SQLException e) {
e.printStackTrace();
if (st != null) {
try {
st.close();
System.out.println("Statement is closed");
} catch (SQLException e) {
e.printStackTrace();
}
}
15/02/23
Note:
A batch with statement interface can contain all types of DML queries in it.
A batch with prepared statement can contain only one type of DML query.
So, a batch with statement is more efficient than batch with prepared Statement.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.PreparedStatement;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc_demo",
"root", "ankush.rana11");
pst.setInt(1, 12);
pst.setString(2, "Rolex");
pst.setLong(3, 888);
pst.addBatch();
pst.setInt(1, 13);
pst.setString(2, "Adheera");
pst.setLong(3, 777);
pst.addBatch();
pst.setInt(1, 14);
pst.setString(2, "Rocky");
pst.setLong(3, 888);
pst.addBatch();
int[] r = pst.executeBatch();
for(int i=0;i<r.length;i++)
e.printStackTrace();
} finally {
if (con != null) {
try {
con.close();
System.out.println("Connection is closed");
} catch (SQLException e) {
e.printStackTrace();
if (pst != null) {
try {
pst.close();
System.out.println("PreparedStatement is closed");
} catch (SQLException e) {
e.printStackTrace();
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
ResultSet rs = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc_demo",
"root", "ankush.rana11");
pst=con.prepareStatement(qry);
pst.setLong(1, phone);
pst.setString(2, password);
rs=pst.executeQuery();
if(rs.next())
System.out.println("Login Successful");
System.out.println("ID"+rs.getInt(1));
System.out.println("name"+rs.getString(2));
System.out.println("phone"+rs.getLong(4));
System.out.println("age"+rs.getInt(3));
else
e.printStackTrace();
} finally {
if (con != null) {
try {
con.close();
System.out.println("Connection is closed");
} catch (SQLException e) {
e.printStackTrace();
if (pst != null) {
try {
pst.close();
System.out.println("Statement is closed");
} catch (SQLException e) {
e.printStackTrace();
if (rs != null) {
try {
rs.close();
System.out.println("ResultSet is closed");
} catch (SQLException e) {
e.printStackTrace();
17/02/23
SERVLET
Server
Server is a Software which is used to manage all the resources along with which it will process the
client request and serve the client request (response).
Picture!!!
1. Database Server
2. Application Server
3. Web Server
Database Server
- A server which is used to deal with the “data” is called as Database Server.
Ex. MangoDB, MySQLDB, Oracle DB, etc.
Application Server
- A server which is used to execute both enterprise application as well as web application is
called as Application Server.
Web Server
Web Application
1. Presentation Logic
2. Business Logic
3. Persistent Logic
Presentation Logic
Technologies Used:
Persistent Logic
Technologies Used:
18/02/23
JEE API
JEE API is a specification, which is used for the development of both Enterprise application as well as
web application, using which we can achieve Loose coupling between Application and server.
Picture!!!
Servlet
It is a server side Java Program which is used to execute all three types of logic such as :
1. Presentation Logic
2. Business Logic
3. Persistence Logic
EJB
Invasive
A framework which allows the programmer to implement its interface and extend its classes
is called as Invasive Framework.
Ex. EJB
Non-Invasive
A framework which doesn’t allow the programmer to implement its interfaces and extends
its classes is called as Non-Invasive Framework.
Ex. Spring, Hibernate, etc…
JEE Container
It is an engine which is used to manage JEE components such as EJB, JSP, Servlet, etc…
Picture!!!
Picture!!!
1. Sett JAVA_HOME
Copy the path: c:/Program Files/Java/Jdk 19.0.1
Paste the Path: Right click on ‘This PC’/properties -> Advanced-system settings ->
Environment variable -> system variable -> new.
2. Set CATALINA-HOME
Copy the path: Upto Apache Tomcat
Paste the Path: Right click on ‘This PC’/properties -> Advanced-system settings ->
Environment variable -> system variable -> new.
3. Set path for Apache Tomcat
Copy the path: Apache Tomcat upto bin folder.
Paste the Path: Advanced System Settings -> Environment Variables -> system variable ->
path -> edit -> new -> Paste it.
21/02/23
bin:
It is used to store the executable files, like start up and shutdown instruction for a server.
Lib:
This folder is used to store the executable jar files which can be used for some additional
functionality.
Conf:
Work:
Logs:
Web-app:
Temp:
Deployment:
The process of making all the resources available to the server is called as Deployment.
1. Manual Deployment
The process of making the resources available to the server manually to the web apps folder
is called as Manual Deployment.
2. Automated Deployment
The process of making the resources available to the server automatically with the help of
automated tools, such as: “ANT (Another Need Tool), Maven, JENKINS” is called as
automated Deployment.
Note:
We can convert (compress) the project into a war file (Web Achieve) and we can deploy into
web apps folder.
We can directly deploy on application into a web apps folder.
Note:
We can deploy more than one application onto server, and if we are deploying more than
one application then the name of every application must be unique.
Every Application should have one web.xml (deployment description) without which we will
get “404 Error” stating resources not found.
The web.xml file will be parsed by JEE container so, if there is any error in web.xml we will
get parse Exception.
22/02/23
Web.xml
It is used to configure the file which has to be display whenever we start the application.
We need to configure welcome file in web.xml, using <welcome-file></welcome-file> which is
a sub tag of <welcome-file-list> </welcome-file-list>.
We can configure more than one <welcome-file> and if we have more than one <welcome-
file> all the files will be loaded in sequential order.
By default the <welcome-file> is index.html.
If we don’t have the welcome file in the project, then we will get 404 Error.
Note:
23/02/23
Servlet
Servlet is a server side java program, which is used to execute all the 3 types of logic, such as
presentation logic, Business Logic and Persistence Logic.
1. Servlet Name
2. Servlet class(Fully qualified Class name of Servlet)
3. URL Pattern.
Picture!!!
Servlet Hierarchy
Picture!!!
In javax.Servlet package
Interfaces
1. Servlet
2. ServletRequest
3. ServletResponse
4. RequestDispatcher
Class
1. Generic Servlet
In javax.Servlet.http
Interfaces
1. HttpServletRequest
2. HttpServletResponse
3. HttpSession
Class
1. HttpServlet
24/02/23
javax.servlet.Servlet
1. init (ServletConfig)
2. destroy ()
3. service (ServletRequest, ServletResponse)
4. getServletInfo() String
5. getServletConfig() ServletConfig
Servlet-Mapping
The process of mapping a servlet with URL is called as Servlet-Mapping.
1. By using web.xml
2. By using Annotation
Java Code:
import java.io.IOException;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
@Override
@Override
return null;
@Override
return null;
@Override
public void init(ServletConfig arg0) throws ServletException {
@Override
XML code:
<servlet>
<servlet-name>First</servlet-name>
<servlet-class>org.jsp.servletdemo.FirstServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>First</servlet-name>
<url-pattern>/fs</url-pattern>
</servlet-mapping>
HTML code:
<body>
</body>
25/02/23
import java.io.IOException;
import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
System.out.println("This is MyServlet...");
XML code:
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>org.jsp.servletdemo.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/ms</url-pattern>
</servlet-mapping>
Html code:
javax.servlet.GenericServlet
27/02/23
javax.servlet.http.HttpServlet
Note: HttpServlet doesn’t have any abstract method, all the inherited abstract methods are
implemented. Along with the inherited methods from the GenericServlet. HttpServlet also contains
do***(). Where * stands for type of request.
1. Post
2. Put
3. Delete
4. Get
5. Patch
6. Head
7. Options
8. Trace
Java code:
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
Xml code:
<servlet>
<servlet-name>Second</servlet-name>
<servlet-class>org.jsp.servletdemo.SecondServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Second</servlet-name>
<url-pattern>/ss</url-pattern>
</servlet-mapping>
Html code:
Java code:
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
XML code:
<servlet>
<servlet-name>Second</servlet-name>
<servlet-class>org.jsp.servletdemo.SecondServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Second</servlet-name>
<url-pattern>/ss</url-pattern>
</servlet-mapping>
Html code:
</form>
28/02/23
@WebServlet
It is the data that has been sent to the server along with the request in the form of key
equals value pairs.
We can fetch the form data with the help of getparameter (String) present in ServletRequest
Interface.
getparameter
Html code:
</form>
Java code:
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(urlPatterns = "/print")
@Override
String name=req.getParameter("nm");
long phone=Long.parseLong(req.getParameter("ph"));
String state=req.getParameter("st");
String country=req.getParameter("ct");
System.out.println("Name: "+name);
System.out.println("Phone: "+phone);
System.out.println("State: "+state);
System.out.println("Country: "+country);
01/03/23
Java.io.Printwriter
getWriter()
Form2.html code:
</form>
Java code:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(urlPatterns = "/ps")
@Override
String name=req.getParameter("nm");
long phone=Long.parseLong(req.getParameter("ph"));
String state=req.getParameter("st");
String country=req.getParameter("ct");
writer.write("<h2>Phone: "+phone+"</h2>");
writer.write("<h2>State: "+state+"</h2>");
writer.write("<h2>Country: "+country+"</h2></body></html>");
02/03/23
Enumeration
Methods of Enumeration:
hasMoreElements
This method returns a boolean value, depends upon the presence of element in the
Enumeration.
It returns true if any element present in the Enumeration otherwise it will return false.
nextElement
getParameterNames
Ex.
Demo.java
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(urlPatterns = "/demo")
@Override
while(keys.hasMoreElements()) {
Demo.html
</form>
03/03/23
Servlet Life Cycle depicts the different phases of a servlet from instantiation upto destruction.
Instantiation Phase
In this phase JEE container will create the object for the servlet by calling public No-argument
constructor.
Along with the servlet object servlet config object will be created which is used to initialise
the members of the Servlet.
The scope of servlet config object is restricted to a particular servlet (For every Servlet object
one servlet config object will be created.)
If this phase fails we will get Servlet Exception.
This phase fails if JEE container is not able to find public No-argument constructor.
Initialisation Phase
In this phase JEE container will call the init() of the servlet to initialise the members of the
servlet.
Servlet Config object is used to initialise the members of the Servlet.
If this phase fails we will get servlet Exception.
04/03/23
Service Phase
In this phase JEE container will call the service method to execute the logic.
JEE container will create the object for ServletRequest and ServletResponse to call the service
method.
By default service method is multiThreaded but we can make the servlet as single threaded in 2 ways
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(urlPatterns = "/lh")
public class LifeCycleDemo extends HttpServlet{
static {
public LifeCycleDemo(){
@Override
@Override
try {
Thread.sleep(5000);
catch(InterruptedException e){
e.printStackTrace();
Destruction Phase
In this phase JEE container will call the destroy method to close the resources.
Destroy method will be called only once but in 2 situations
Case 1: If we close the application destroy method will be called to close all the costly
resources.
Case 2: If we redeploy the application, JEE container will call the destroy method to close all
the previously used resources.
If this Phase fails, efficiency of the application will decrease.
Note:
1. The JEE container will create the servlet object only for the first client request.
2. Init method is executed only once for the first client request.
3. Service method will execute for every client request.
4. Destroy method will be called only once.
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(urlPatterns = "/lh")
static {
public LifeCycleDemo(){
@Override
@Override
@Override
Load on Startup
06/03/23
<load-on-startup> is a sub-tag of servlet tag.
We should config. Load on startup with a positive integer value.
The servlets will be loaded based on the positive integer value in ascending order of integer
value.
If we have more than one servlet config with same integer value then sequential execution
takes place.
If we config load on startup for a servlet with negative integer value then the servlet object
will be created after the first client request.
Note: If we use load on startup only service method will execute for the first client request.
Demo.java
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public demo(){
@Override
@Override
@Override
Test.html
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public test(){
@Override
@Override
@Override
Web.xml
<welcome-file-list>
<welcome-file>home.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>test</servlet-name>
<servlet-class>org.jsp.test</servlet-class>
<load-on-startup>11</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>
07/03/23
Servlet Chaining
The process of transferring the control from a servlet to another resource such as jsp or html or
another servlet is called as Servlet Chaining.
1. javax.servlet.RequestDispatcher
2. Send Redirect(String)
javax.servlet.RequestDispatcher
1. forward(ServletRequest, ServletResponse)
2. include(ServletRequest, ServletResponse)
getRequestDispatcher(String path)
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/login")
dispatcher = request.getRequestDispatcher("home.html");
dispatcher.forward(request, response);
}else
dispatcher = request.getRequestDispatcher("login.html");
writer.write("<html><body><h1>Invalid
Credentials<h1><body><html>");
dispatcher.include(request, response);
Html code:
</form>
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/reg")
int id = Integer.parseInt(request.getParameter("id"));
try {
Class.forName(UserUtility.DRIVER);
con =
DriverManager.getConnection(UserUtility.URL,UserUtility.USER,UserUtility.PA
SSWORD);
pst = con.prepareStatement(qry);
pst.setInt(1, id);
pst.setString(2, Name);
pst.setInt(3, Age);
pst.setLong(4, Phone);
pst.setString(5, Pass);
pst.executeUpdate();
}catch(SQLException | ClassNotFoundException e) {
e.printStackTrace();
}finally {
if(con!=null)
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
if(pst!=null)
try {
pst.close();
} catch (SQLException e) {
e.printStackTrace();
Html code:
<h1>Registration Form</h1>
</form>
09/03/23
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(value = "/fetch")
@Override
int id = Integer.parseInt(req.getParameter("id"));
ResultSet rs = null;
try {
Class.forName(UserUtility.DRIVER);
con =
DriverManager.getConnection(UserUtility.URL,UserUtility.USER,UserUtility.PASSWORD);
pst = con.prepareStatement(qry);
pst.setInt(1,id);
rs = pst.executeQuery();
if(rs.next()) {
writer.write("<html><body><h1>ID: "+rs.getInt(1)+"<br>");
writer.write("<h1>Name: "+rs.getString("name")+"</h1>");
writer.write("<h1>Age: "+rs.getInt("age")+"</h1>");
writer.write("<h1>Phone:
"+rs.getLong("phone")+"</h1></body></html>");
}else {
writer.write("<html><body><h1>Invalid ID</h1></body></html>");
} finally {
if (con != null) {
try {
con.close();
System.out.println("Connection is closed");
} catch (SQLException e) {
e.printStackTrace();
if (pst != null) {
try {
pst.close();
System.out.println("Statement is closed");
} catch (SQLException e) {
e.printStackTrace();
if (rs != null) {
try {
rs.close();
System.out.println("ResultSet is closed");
} catch (SQLException e) {
e.printStackTrace();
}}
Html code:
</form>
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(value = "/del")
@Override
int id = Integer.parseInt(req.getParameter("id"));
try {
Class.forName(UserUtility.DRIVER);
con =
DriverManager.getConnection(UserUtility.URL,UserUtility.USER,UserUtility.PASSWORD);
pst = con.prepareStatement(qry);
pst.setInt(1,id);
pst.executeUpdate();
}
e.printStackTrace();
} finally {
if (con != null) {
try {
con.close();
System.out.println("Connection is closed");
} catch (SQLException e) {
e.printStackTrace();
if (pst != null) {
try {
pst.close();
System.out.println("Statement is closed");
} catch (SQLException e) {
e.printStackTrace();
}}
Html code:
</form>
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(value = "/update")
@Override
int id = Integer.parseInt(req.getParameter("id"));
String qry = "update user set name=?, age=?, phone=?, pass=? where id=?";
writer.write("<html><body><h1>User Updated
Succesfully</h1></body></html>");
try {
Class.forName(UserUtility.DRIVER);
con =
DriverManager.getConnection(UserUtility.URL,UserUtility.USER,UserUtility.PA
SSWORD);
pst = con.prepareStatement(qry);
pst.setInt(5,id);
pst.setString(1, Name);
pst.setInt(2, Age);
pst.setLong(3, Phone);
pst.setString(4, Pass);
pst.executeUpdate();
e.printStackTrace();
} finally {
if (con != null) {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
if (pst != null) {
try {
pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
Html Code:
</form>
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(value = "/login")
@Override
ResultSet rs = null;
try {
Class.forName(UserUtility.DRIVER);
con =
DriverManager.getConnection(UserUtility.URL,UserUtility.USER,UserUtility.PA
SSWORD);
pst = con.prepareStatement(qry);
pst.setLong(1, Phone);
pst.setString(2, Pass);
rs = pst.executeQuery();
if(rs.next()) {
writer.write("<html><body><h1>ID: "+rs.getInt(1)+"<br>");
writer.write("<h1>Name: "+rs.getString("name")+"</h1>");
writer.write("<h1>Age: "+rs.getInt("age")+"</h1>");
writer.write("<h1>Phone: "+rs.getLong("phone")+"</h1></body></html>");
}else {
writer.write("<html><body><h1>Invalid Credentials</h1></body></html>");
e.printStackTrace();
} finally {
if (con != null) {
try {
con.close();
System.out.println("Connection is closed");
} catch (SQLException e) {
e.printStackTrace();
if (pst != null) {
try {
pst.close();
System.out.println("Statement is closed");
} catch (SQLException e) {
e.printStackTrace();
if (rs != null) {
try {
rs.close();
System.out.println("ResultSet is closed");
} catch (SQLException e) {
e.printStackTrace();
Html code:
</form>
Home.HTML:
<body>
</body>
10/03/23
Advantages of JSP
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(value = "/fetchJ")
@Override
int id = Integer.parseInt(req.getParameter("id"));
ResultSet rs = null;
try {
Class.forName(UserUtility.DRIVER);
con =
DriverManager.getConnection(UserUtility.URL,UserUtility.USER,UserUtility.PA
SSWORD);
pst = con.prepareStatement(qry);
pst.setInt(1,id);
rs = pst.executeQuery();
if(rs.next()) {
dispatcher = req.getRequestDispatcher("print.jsp");
req.setAttribute("id", rs.getInt("id"));
req.setAttribute("Name", rs.getString("Name"));
req.setAttribute("Age", rs.getInt("Age"));
req.setAttribute("Phone", rs.getLong("Phone"));
dispatcher.forward(req, resp);
}else {
writer.write("<html><body><h1>Invalid ID</h1></body></html>");
e.printStackTrace();
} finally {
if (con != null) {
try {
con.close();
System.out.println("Connection is closed");
} catch (SQLException e) {
e.printStackTrace();
if (pst != null) {
try {
pst.close();
System.out.println("Statement is closed");
} catch (SQLException e) {
e.printStackTrace();
if (rs != null) {
try {
rs.close();
System.out.println("ResultSet is closed");
} catch (SQLException e) {
e.printStackTrace();
}}
HTML code:
<input type="submit">
</form>
JSP code:
<body>
<h1>ID:<%=request.getAttribute("id") %></h1><br>
<h1>Name:<%=request.getAttribute("Name") %></h1><br>
<h1>Age:<%=request.getAttribute("Age") %></h1><br>
<h1>Phone:<%=request.getAttribute("Phone") %></h1><br>
</body>
11/03/23
Print.jsp
Translated Servlet
Class printJSP_Servlet
Return a+b;
Writer.write(sum);
Session Tracking
Session Tracking is a technique in which we track the session of a user. Generally the application
should not ask the user to login for every request. Every request should not be considered as a new
request until the session is over.
1. Cookies
2. HttpSession
3. HiddenFormFields
13/03/23
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
p.setProperty("jdbc.driver", "com.mysql.cj.jdbc.Driver");
p.setProperty("jdbc.user", "root");
p.setProperty("jdbc.url", "jdbc:mysql://localhost:3306/jdbc_demo");
p.setProperty("jdbc.password", "ankush.rana11");
}
}
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
p.load(fin);
System.out.println(p.getProperty
("jdbc.driver"));
System.out.println(p.getProperty("jdbc.user"));
System.out.println(p.getProperty("jdbc.url"));
System.out.println(p.getProperty("jdbc.password"));
#Code to Establish the connection between java application and database server using
getConnection(String URL, Properties info).
Store Procedure
Store procedure is a group of sql statements which contains a name and can be reused.
1. IN Parameter
2. Out Parameter
3. INOUT Parameter
Assignment Questions
BEGIN
/* SQL statements */
END
Java.sql.CallableStatement
prepareCall (String)
JDBC Driver
JDBC Driver is the implementation of JDBC provided by database vendor to connect with
database.
JDBC Driver is used to convert java calls into sql calls.
It should be installed in client machine to connect with a specific database.
Note: We are using Type 4 Driver OR Thin Driver for database Connectivity.