0% found this document useful (0 votes)
5 views

Java

Uploaded by

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

Java

Uploaded by

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

Unit-2: Java Notes

BCA-502

Java Programming and Dynamic


Webpage Design

Code: BCA- 502, Semester: V


University: CCSU

Subject: Java Programming & Dynamic Webpage Design


Faculty: Prof. Manoj Kumar Gupta 1
Unit-2: Java Notes
BCA-502 Java Syllabus: Unit- III

Networking (datagram JDBC: socket and TCP/IP based server socket)


event handling,

Introduction,
Drivers,
Establishing Connection
Connection Pooling.

Subject: Java Programming & Dynamic Webpage Design


Faculty: Prof. Manoj Kumar Gupta 2
Unit-2: Java Notes
BCA-502 Datagrams in Java

TCP/IP-style networking provides a serialized, predictable, reliable


stream of packet data. This is not without its cost. However. TCP
includes algorithms for dealing with congestion control on crowded
networks.

Clients and servers that communicate via a reliable channel, such as


a TCP socket, have a dedicated point-to-point channel between
themselves. To communicate, they establish a connection, transmit
the data, and then close the connection. All data sent over the
channel is received in the same order in which it was sent. This is
guaranteed by the channel.
Subject: Java Programming & Dynamic Webpage Design
Faculty: Prof. Manoj Kumar Gupta 3
Unit-2: Java Notes
BCA-502 Datagrams in Java
A datagram is an independent, self-contained message sent over the network whose arrival,
arrival time, and content are not guaranteed.

1. Datagrams plays a vital role as an alternative.

2. Datagrams are bundles of information passed between machines. Once the datagram
has been released to its intended target, there is no assurance that it will arrive or even
that someone will be there to catch it.

3. Likewise, when the datagram is received, there is no assurance that it hasn’t been
damaged in transit or that whoever sent it is still there to receive a response and it is
crucial point to note.

Java implements datagrams on top of the UDP (User Datagram Protocol) protocol by using
two classes:
1. DatagramPacket object is the data container.
2. DatagramSocket is the mechanism used to send or receive the DatagramPackets.

Subject: Java Programming & Dynamic Webpage Design


Faculty: Prof. Manoj Kumar Gupta 4
Unit-2: Java Notes
BCA-502 Datagram Packet Class

DatagramPacket defines several constructors. Four are shown here:

1. DatagramPacket(byte data[ ], int size) : It specifies a buffer that will receive data and the size
of a packet. It is used for receiving data over a DatagramSocket.

2. DatagramPacket(byte data[ ], int offset, int size) : It allows you to specify an offset into the
buffer at which data will be stored.

3. DatagramPacket(byte data[ ], int size, InetAddress ipAddress, int port) : It specifies a target
address and port, which are used by a DatagramSocket to determine where the data in the
packet will be sent.

4. DatagramPacket(byte data[ ], int offset, int size, InetAddress ipAddress, int port) : It transmits
packets beginning at the specified offset into the data.

Subject: Java Programming & Dynamic Webpage Design


Faculty: Prof. Manoj Kumar Gupta 5
Unit-2: Java Notes
BCA-502 Datagram Socket Class

DatagramSocket defines four public constructors. They are shown here:

1. DatagramSocket( ) throws SocketException : It creates a DatagramSocket bound to any


unused port on the local computer.

2. DatagramSocket(int port) throws SocketException : It creates a DatagramSocket bound to the


port specified by port.

3. DatagramSocket(int port, InetAddress ipAddress) throws SocketException : It constructs a


DatagramSocket bound to the specified port and InetAddress.

4. DatagramSocket(SocketAddress address) throws SocketException : It constructs a


DatagramSocket bound to the specified SocketAddress.

Subject: Java Programming & Dynamic Webpage Design


Faculty: Prof. Manoj Kumar Gupta 6
Unit-2: Java Notes
BCA-502 Java Event Handling
Event and Listener (Java Event Handling): Changing the state of an object is known as an event. For
example, click on button, dragging mouse etc. The java.awt.event package provides many event classes
and Listener interfaces for event handling.

Register the component with the Listener. For registering the component with the Listener, many classes
provide the registration methods. Like:
1. Button
public void addActionListener(ActionListener a){}
2. MenuItem
public void addActionListener(ActionListener a){}
3. TextField
public void addActionListener(ActionListener a){}
public void addTextListener(TextListener a){}
4. TextArea
public void addTextListener(TextListener a){}
5. Checkbox
public void addItemListener(ItemListener a){}
6. Choice
public void addItemListener(ItemListener a){} etc….

Subject: Java Programming & Dynamic Webpage Design


Faculty: Prof. Manoj Kumar Gupta 7
Unit-2: Java Notes
BCA-502 Introduction: JDBC

Java Database Connectivity (JDBC) is an application programming


interface (API) that allows Java applications to access and interact
with databases.

OR

JDBC stands for Java Database Connectivity. JDBC is a Java API to


connect and execute the query with the database.

Subject: Java Programming & Dynamic Webpage Design


Faculty: Prof. Manoj Kumar Gupta 8
Unit-2: Java Notes
BCA-502 JDBC- Drivers

JDBC Driver API − This supports the JDBC Manager-to-Driver


Connection.

Driver: This is the interface which 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.

Subject: Java Programming & Dynamic Webpage Design


Faculty: Prof. Manoj Kumar Gupta 9
Unit-2: Java Notes
BCA-502 Common JDBC Components

1. 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 subprotocol
under JDBC will be used to establish a database Connection.

2. 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.

3. Connection − This is an 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.

Subject: Java Programming & Dynamic Webpage Design


Faculty: Prof. Manoj Kumar Gupta 10
Unit-2: Java Notes
BCA-502 Common JDBC Components

4. Statement − We can 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.

5. 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.

6. SQLException − This class handles any errors that occur in a


database application.

Subject: Java Programming & Dynamic Webpage Design


Faculty: Prof. Manoj Kumar Gupta 11
Unit-2: Java Notes
BCA-502 JDBC: Socket
Java Socket programming is used for communication between the
applications running on different JRE. Java Socket programming can be
connection-oriented or connection-less.

Socket and ServerSocket classes are used for connection-oriented


socket programming and DatagramSocket and DatagramPacket classes
are used for connection-less socket programming.

A socket is simply an endpoint for communications between the


machines. The Socket class can be used to create a socket.

The client in socket programming must know two information:


1. IP Address of Server,
2. Port number.
Subject: Java Programming & Dynamic Webpage Design
Faculty: Prof. Manoj Kumar Gupta 12
Unit-2: Java Notes
BCA-502 JDBC: Program To CREATE Table into DB
package jdbcPro;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class JdbcConnection {

public static void main(String[] args) throws Exception {

String url="jdbc:oracle:thin:@localhost:1521:xe";
String user="system";
String pass="oracle2024";

Class.forName("oracle.jdbc.driver.OracleDriver"); //step1 load the driver class


Connection conn = DriverManager.getConnection(url, user, pass); //step2 create the connection object
Statement stmt=conn.createStatement(); //step3 create the statement objec
//step4 execute query
String query = "Create table customer(cust_id NUMERIC(10) PRIMARY KEY, cust_name VARCHAR(80), cust_address VARCHAR(100))";

int result =stmt.executeUpdate(query);


System.out.println("Success");

conn.close(); //step5 close the connection object

}
}
Subject: Java Programming & Dynamic Webpage Design
Faculty: Prof. Manoj Kumar Gupta 13
Unit-2: Java Notes
BCA-502 JDBC: Program to INSERT Records into Table

package jdbcPro;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class JdbcConnection {

public static void main(String[] args) throws Exception {

String url="jdbc:oracle:thin:@localhost:1521:xe";
String user="system";
String pass="oracle2024";

Class.forName("oracle.jdbc.driver.OracleDriver"); //step1 load the driver class


Connection conn = DriverManager.getConnection(url, user, pass); //step2 create the connection object
Statement stmt=conn.createStatement(); //step3 create the statement objec

String query = "INSERT INTO customer values(11012, 'RKomal Rani', 'Delhi’)”; //step4 execute query

int result =stmt.executeUpdate(query);


System.out.println("Success");

conn.close(); //step5 close the connection object


}
}

Subject: Java Programming & Dynamic Webpage Design


Faculty: Prof. Manoj Kumar Gupta 14
Unit-2: Java Notes
BCA-502 JDBC: Program to DELETE Records from table
package jdbcPro;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class JdbcConnection {

public static void main(String[] args) throws Exception {

String url="jdbc:oracle:thin:@localhost:1521:xe";
String user="system";
String pass="oracle2024";

Class.forName("oracle.jdbc.driver.OracleDriver"); //step1 load the driver class


Connection conn = DriverManager.getConnection(url, user, pass); //step2 create the connection object
Statement stmt=conn.createStatement(); //step3 create the statement objec

String query = "DELETE from student where roll='111’”; //step4 execute query

int result =stmt.executeUpdate(query);


System.out.println("Success");

conn.close(); //step5 close the connection object


}
}

Subject: Java Programming & Dynamic Webpage Design


Faculty: Prof. Manoj Kumar Gupta 15
Unit-2: Java Notes
BCA-502 JDBC: Program to Fetch records from Table
package jdbcPro;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class JdbcConnection {

public static void main(String[] args) throws Exception {

String url="jdbc:oracle:thin:@localhost:1521:xe";
String user="system";
String pass="oracle2024";
try{
Class.forName("oracle.jdbc.driver.OracleDriver"); //step1 load the driver class
Connection conn = DriverManager.getConnection(url, user, pass); //step2 create the connection object
Statement stmt=conn.createStatement(); //step3 create the statement objec t
ResultSet rs=stmt.executeQuery("select * from student");//step4 execute query

while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2)+"+rs.getString(3));

con.close(); //step5 close the connection object


}
catch(Exception e){ System.out.println(e);}
}
}
Subject: Java Programming & Dynamic Webpage Design
Faculty: Prof. Manoj Kumar Gupta 16
Unit-2: Java Notes
BCA-502 Connection Pooling
In Java applications, interacting with the databases using JDBC (Java
Database Connectivity), and managing the database connections
efficiently is crucial for optimal performance and resource utilization.

Connection pooling is the technique employed to achieve the goal by


reusing the established database connections instead of creating new
ones for each request.

Connection pooling means a pool of Connection Objects. Connection


pooling is based on an object pool design pattern.

Subject: Java Programming & Dynamic Webpage Design


Faculty: Prof. Manoj Kumar Gupta 17
Unit-2: Java Notes
BCA-502 Connection Pooling
An application that uses a
connection pooling strategy has
already DB connection objects
which can be reused.

So, when there is a need to interact


with the database, the application
obtains connection instances from
Pool.

Connection pooling improves


application performance that
interacts with the database.

Subject: Java Programming & Dynamic Webpage Design


Faculty: Prof. Manoj Kumar Gupta 18
Unit-2: Java Notes
BCA-502 Connection Pooling
We can create our own implementations of Connection pooling. Any
connection pooling framework needs to do three tasks.

1. Creating Connection Objects


2. Manage usage of created Objects and validate them
3. Release/Destroy Objects

In Java, we have great set of libraries which are readily available. We


only need to configure few properties to use them. Some of the libraries
as are given below:
1. Apache Commons DBCP 2
2. HikariCP
3. C3P0

Subject: Java Programming & Dynamic Webpage Design


Faculty: Prof. Manoj Kumar Gupta 19
Unit-2: Java Notes
BCA-502

THANKS
Subject: Java Programming & Dynamic Webpage Design
Faculty: Prof. Manoj Kumar Gupta 20

You might also like