0% found this document useful (0 votes)
4 views54 pages

Java Networking Unit-4 - Complete

The document outlines the curriculum for an Advance Java Programming course, focusing on Networking and Java Database Programming. Key topics include basics of networking, socket programming, client/server computing, SQL, JDBC, and various types of database interactions. It also covers the advantages of wired and wireless networking, as well as the differences between TCP and UDP protocols.

Uploaded by

Ravi Shankar Jha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views54 pages

Java Networking Unit-4 - Complete

The document outlines the curriculum for an Advance Java Programming course, focusing on Networking and Java Database Programming. Key topics include basics of networking, socket programming, client/server computing, SQL, JDBC, and various types of database interactions. It also covers the advantages of wired and wireless networking, as well as the differences between TCP and UDP protocols.

Uploaded by

Ravi Shankar Jha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 54

Subject Title: Advance Java Programming,

Code: CSF206, Credit: 3:0:1:4


Class: B.Tech. 2st Year, 4th Sem

UNIT IV: Networking and Java database programming


Basics of Networking, Socket Programming, Client/Server Computing,
Relational Database Systems, SQL, JDBC, Prepared Statement, Callable
Statement.

DIT University, 17July2019


Basics of Networking
• Networking is a concept of connecting two or more computing devices
together so that we can share resources.
• Networking, also known as computer networking, is the practice of
transporting and exchanging data between nodes over a shared medium in
an information system.
• Networking comprises not only the design, construction and use of a
network, but also the management, maintenance and operation of the
network infrastructure, software and policies.
• Java socket programming provides facility to share data between different
computing devices.

Advantage of Java Networking


I. Sharing resources
II. Centralize software management
Types of networking
• There are two primary types of computer networking:
1. wired networking and,
2. wireless networking
Wired networking requires the use of a physical medium for transport
between nodes.
• Copper-based Ethernet cabling, popular due to its low cost and
durability, is commonly used for digital communications in businesses
and homes.
• Alternatively, optical fiber is used to transport data over greater
distances and at faster speeds, but it has several tradeoffs, including
higher costs and more fragile components.
• Wireless networking uses radio waves to transport data over the air,
enabling devices to be connected to a network without any cabling.
• Wireless LANs are the most well-known and widely deployed form of
wireless networking. Alternatives include microwave, satellite, cellular
and Bluetooth, among others.
Advantages:
• wired networking offers greater speed, reliability and security
compared to wireless networks.
• wireless networking tends to provide more flexibility, mobility and
scalability.
Components of networking
Computer networking requires the use of physical network
infrastructure including switches, routers and wireless access points
and the underlying firmware that operates such equipment.
Other components include the software necessary to
monitor, manage and secure the network.
• Networks rely on the use of standard protocols to uniformly perform
distinct functions or communicate with different types of data.
• UDP and TCP, two ways of sending traffic(Network Protocols)
Types of Area Network
Network Protocols
• The java.net package provides support for the two common network protocols −
• When a computer is sending out traffic it needs to send the data packets from its own IP address
(source address) to a destination IP address. So IP will handle the addresses of different devices on
a network.
• But different programs and services have very different requirements regarding how they prefer
that traffic should be sent over the network. To some programs, it is extremely important that not
a single data packet is lost, and that no packets are received in the wrong order. Other programs
might not care if some errors or packet losses occur. Instead, they might prefer that the traffic is
just simply being sent as quickly as possible.

• A protocol is a compilation of information regarding decisions on how something should be


performed.
• Networks and computers use lots of different protocols that govern how communication should be
handled, how data should be sent and so on.
• IP addresses and how network traffic should be addressed with IP addresses is governed by the IP
protocol. IP actually stands for Internet Protocol
• TCP and UDP are protocols governing how traffic should be sent.
TCP Vs. UDP
• Data Transfer Features
TCP enables the establishment of a strong connection between two hosts to
exchange data in streams. TCP guarantees to deliver data in the same ordered
manner as sent from server to user and vice versa. Thus, TCP is a connection-
oriented protocol. However, UDP is connectionless and non-dedicated protocol
does not check the readiness of the receiver host.
• Reliability
Reliability of TCP is comparatively higher as it ensures message acknowledgment
and retransmissions of data in case of loss of data parts in transit. Hence, the hosts
do not lose any missing data. On the other hand, UDP does not offer concepts of
message acknowledgments, time-out or retransmission feature. Therefore, there
is no communication of whether the packets have reached receiver or lost in
transit.
Client Server Model
• Client-Server Model – The client-server model describes the
communication between two computing entities over a network.
Clients are the ones requesting a resource or service and Servers are
the ones providing that resource or service.
Categories of Client-Server Computing
There are four main categories of the client-server model:
1) One-Tier architecture: consists of a simple program running on a
single computer without requiring access to the network. User requests
don’t manage any network protocols, therefore the code is simple and
the network is relieved of the extra traffic.
2) Two-Tier architecture: consists of the client, the server, and the
protocol that links the two tiers. The Graphical User Interface code
resides on the client host and the domain logic resides on the server
host. The client-server GUI is written in high-level languages such as C+
+ and Java.
3) Three-Tier architecture: consists of a presentation tier, which is the
User Interface layer, the application tier, which is the service layer that
performs detailed processing, and the data tier, which consists of a
database server that stores information.
Socket Programming
• Socket programming is a way of connecting two nodes on a network
to communicate with each other. Means, how application
communicates/transfers data with each other over internet.
• One socket(node or end point or machine) listens on a particular port
at an IP, while the other socket reaches out to the other to form a
connection.
• The server forms the listener socket while the client reaches out to
the server.
1) Establish a Socket
Connection
• To connect to another machine we need a socket connection. A socket
connection means the two machines have information about each other’s
network location (IP Address) and TCP port. The java.net.Socket class
represents a Socket.

• The first argument – IP address of Server. ( 127.0.0.1 is the IP address of


localhost, where code will run on the single stand-alone machine).
• The second argument – TCP Port. (Just a number representing which
application to run on a server. For example, HTTP runs on port 80. Port
number can be from 0 to 65535)
2) Communication
To communicate over a socket connection, streams are used to both
input and output the data.
3) Closing the connection
• The socket connection is closed explicitly once the message to the
server is sent.
• In the program, the Client keeps reading input from a user and sends
it to the server until “Over” is typed.
Socket and port number
Socket I/O
Basics of java I/o
• Java I/O (Input and Output) is used to process the input and produce the output.
• Java uses the concept of a stream to make I/O operation fast.
• The java.io package contains all the classes required for input and output
operations.
• A stream can be defined as a sequence of data. The InputStream is used to read
data from a source and the OutputStream is used for writing data to a
destination.
• In Java, 3 streams are created for us automatically. All these streams are
attached with the console.
• 1) System.out: standard output stream
• 2) System.in: standard input stream
• 3) System.err: standard error stream
Java PrintStream Class
• The PrintStream class of the java.io package can be used to write
output data in commonly readable form (text) instead of bytes.
Methods:
Java BufferedReader/InputStream Class

• Java BufferedReader class is used to read the text from a character-


based input stream. It can be used to read data line by line by
readLine() method. Methods:

• InputStreamReader: An InputStreamReader is a bridge from byte


streams to character streams: It reads bytes and decodes them into
characters using a specified charset.
• Method: getInputStream()- The getInputStream() method of Java
Socket class returns an input stream for the given socket.
Package java.net
ServerSocket
Socket
Creation of server:
Creation of Client:
• Write a simple socket program to send a message from server to
client.
Client Side
Server Side
• WAP to calculate a factorial of a number using soket programming
working on port number 9806.
Client Side
Server side
Relational Database Systems
• A database is a collection of organised or arranged data that can be
easily accessed, updated/ modified or controlled. Information within
the database can be easily placed into rows and columns, or tables.
Meanwhile, database management enables the user to store, manage
and access data.
• A relational database management system (RDBMS) is a collection of
programs and capabilities that enable IT teams and others to create,
update, administer and otherwise interact with a relational database.
RDBMSes store data in the form of tables, with most commercial
relational database management systems using Structured Query
Language (SQL) to access the database.
SQL
SQL is a standard language for accessing and manipulating databases.
SQL stands for Structured Query Language
SQL lets you access and manipulate databases
SQL became a standard of the American National Standards Institute
(ANSI) in 1986, and of the International Organization for
Standardization (ISO) in 1987
Major Commands:
SELECT, UPDATE, DELETE, INSERT, WHERE
JDBC(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.
• 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.
JDBC Drivers
• Purpose of Jdbc: To convert java call into database specific call and
vice-versa, translator is required i.e. driver software.
• Based on functionality and architecture all JDBC drivers are
categorised into 4 types:
1) Type-I (JDBC-ODBC Bridge Driver/ Bridge Driver)
2) Type-II (Native API Party driver/ Native Driver
3) Type-III (All java Network protocol driver/ Net Protocol
driver/Middleware driver.
4) Type-IV ( Pure Java Driver/ Thin Driver)
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 subprotocol 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.
• 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.
Steps for jdbc Connection
Java Database Connectivity
Java Database Connectivity with MySQL

• Driver class: The driver class for the mysql database


is com.mysql.jdbc.Driver.
• 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.
• Username: The default username for the mysql database is root.
• 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.
DriverManager class
• The DriverManager class acts as an interface between users and
drivers.
• It contains all the appropriate methods to register and deregister the
database driver class and to create a connection between a Java
application and the database.
• The DriverManager class maintains a list of Driver classes that have
registered themselves by calling the method
DriverManager.registerDriver().
Connection interface
• A Connection is a session between a Java application and a database.
It helps to establish a connection with the database.
• Commonly used methods of Connection interface:
1) public Statement createStatement(): creates a statement object that
can be used to execute SQL queries.
2) public Statement createStatement(int resultSetType,int
resultSetConcurrency): Creates a Statement object that will generate
ResultSet objects with the given type and concurrency.
Statement interface
• The Statement interface provides methods to execute queries with
the database. The statement interface is a factory of ResultSet i.e. it
provides factory method to get the object of ResultSet.
• Commonly used methods of Statement interface:
1) public ResultSet executeQuery(String
sql): is used to execute SELECT query. It returns
the object of ResultSet.
2) public int executeUpdate(String sql): is
used to execute specified query, it may be create,
drop, insert, update, delete etc.
3) public boolean execute(String sql): is used
to execute queries that may return multiple
results.
ResultSet interface
• The object of ResultSet maintains a cursor pointing to a row of a
table. Initially, cursor points to before the first row.
• Commonly used methods of ResultSet interface
1) public boolean next(): is used to move the cursor to
the one row next from the
current position.
2) public boolean is used to move the cursor to
previous(): the one row previous from the
current position.
3) public boolean first(): is used to move the cursor to
the first row in result set object.
4) public boolean last(): is used to move the cursor to
the last row in result set object.
1)Create of database
2) Add jar file in java Application
project
• To connect java application with the mysql
database, mysqlconnector.jar file is required to be loaded.
• Download the mysqlconnector.jar file. Go to jre/lib/ext folder and
paste the jar file here.
3) Connect Java Application with mysql database
PreparedStatement interface

• The PreparedStatement interface is a subinterface of Statement. It is used to execute parameterized query.


• String sql="insert into emp values(?,?,?)";
CallableStatement Interface
• CallableStatement interface is used to call the stored procedures and
functions.
• Object of the CallableStatement (interface) is created using
the prepareCall() method of the Connection interface.
• A Callable statement can have input parameters, output parameters
or both.
Table: Employee & Procedure:
myProcedure
JDBC - Statements, PreparedStatement and
CallableStatement
• Once a connection is obtained we can interact with the database. The
JDBC Statement,CallableStatement, and PreparedStatement interface
s define the methods and properties that enable you to send SQL
commands and receive data from your database.

You might also like