Aj Solution
Aj Solution
Aj Solution
The byte buffer (the byte array) is the data that is to be sent in the UDP
datagram. The length of the above buffer, 65508 bytes, is the maximum amount
of data you can send in a single UDP packet.
The InetAddress class represents an IP address (Internet
Address).The getByName() method returns an InetAddress instance with the IP
address matching the given host name.
To send the DatagramPacket you must create a DatagramSocket targeted at
sending data. Here is how that is done:
DatagramSocket datagramSocket = new DatagramSocket();
datagramSocket.receive(packet);
import java.net.*;
InetAddress ip = InetAddress.getByName("127.0.0.1");
import java.net.*;
public class DReceiver{
Ans
Socket:
InputStream in = socket.getInputStream();
Closing a Socket
When you are done using a Java Socket you must close it to close the
connection to the server. This is done by calling the Socket.close() method
socket.close();
ServerSocket: In order to implement a Java server that listens for incoming connections
from clients via TCP/IP, java.net.ServerSocket is to be used.
Creating a ServerSocket
Here is a simple code example that creates a ServerSocket that listens on port
9000:
ServerSocket serverSocket = new ServerSocket(9000);
Incoming connections can only be accepted while the thread running the server
has called accept(). All the time the thread is executing outside of this method no
clients can connect.
InetAddress:
Q.3 Write a java program to find an IP address of the machine on which the program
runs.
Ans Program:
import java.io.*;
import java.net.*;
public class InetDemo{
public static void main(String[] args){
try{
InetAddress ip=InetAddress.getByName("www.gmail.com");
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
InetAddress host=InetAddress.getLocalHost();
System.out.println("java client"+host);
System.out.println("Enter String");
BufferedReader client=new BufferedReader(new InputStreamReader(System.in));
String data=client.readLine();
TCP_Server:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
System.out.println("Connection established...");
Output:
Server:
Server is started...
Server is waiting for client request...
Connection established...
Client:
java clientLenovo-PC/127.0.0.1
Enter string:
This is my text to be changed by the SERVER
Reverse string: REVRES eht yb degnahc eb ot txet ym si sihT
Q.5 What is JDBC? List various types of JDBC Driver. Explain thick and thin driver.
Write code snippet for any type of JDBC connection. Comment on selection of
driver.
Ans
JDBC:
The Java Database Connectivity (JDBC) API is the industry standard for
database-independent connectivity between the Java programming language and
a wide range of databases SQL databases and other tabular data sources, such as
spreadsheets or flat files.
The JDBC classes are contained in the Java Package java.sql and javax.sql.
JDBC helps you to write Java applications that manage these three programming
activities:
1. Connect to a data source, like a database.
2. Send queries and update statements to the database.
3. Retrieve and process the results received from the database in answer to the
query.
JDBC Drivers Types:-
JDBC driver implementations vary because of the wide variety of operating systems and
hardware platforms in which Java operates. Sun Microsystems has divided the
implementation types into four categories, Types 1,2, 3, and 4, which are as under :
Thick Driver:
This was the second JDBC driver introduced by Java after Type 1, hence it
known as type 2.
In this driver, performance was improved by reducing communication layer.
Instead of talking to ODBC driver, JDBC driver directly talks to DB client using
native API. That's why it is also known as native API or partly Java driver or
thick driver.
Type 2 drivers wrap a thin layer of Java around database-specific native code
libraries.
Since it required native API to connect to DB client it is also less portable and
platform dependent.
If native library e.g. ocijdbc11.dll, which is required to connect Oracle
11g database is not present in client machine then you will get error.
For Oracle databases, the native code libraries might be based on the OCI
(Oracle Call Interface) libraries, which were originally designed for C/ C++
programmers. Because
Type 2 drivers are implemented using native code; in some cases they have
better performance than their all-Java counterparts.
They add an element of risk; however, because a defect in a driver’s native code
section can crash the entire server.
Code Snippet:
Thin Driver :
This is the driver which is most likely to be used to connect to modern database
like Oracle, SQL Server, MySQL, SQLLite and PostgreSQL.
This driver is implemented in Java and directly interacts with the database using
its native protocol.
This driver includes all database call in one JAR file, which makes it very easy
to use. All that is needed to connect a database from Java program is to include
JAR file of relevant JDBC driver.
Because of light weight, this is also known as thin JDBC driver.
Since this driver is also written in pure Java, it is portable across all platform,
which means you can use same JAR file to connect to MySQL even if your Java
program is running on Windows, Linux or Solaris.
Performance of this type of JDBC driver is also best among all of them.
Code Snippet:
Type 4 JDBC driver is the most preferable driver. There is hardly any situation when
you need to go to previous version of JDBC driver. Though, if you want to connect to
Oracle database using TNS name using OCI client, you need to use type 2 JDBC driver
also known as thick JDBC driver. That requires database native client library
e.g. ocijdbc11.dll and if that's not present in the machine then your Java program will
throw error at run time.
Q.6 Explain JDBC Architecture.
Ans As shown in the figure, the java application that needs to communicate with a
database has to be programmed using JDBC API.
The JDBC driver supporting data source, such as Oracle and SQL has to be
added in java application for JDBC support, which can be done dynamically at
run time.
The dynamic plugging of the JDBC drivers ensures that the Java application is
vendor independent.
Some of the available drivers are pure Java drivers and are portable for all
environments; whereas others are partial java drivers and require some libraries
to communicate with the database.
The JDBC API uses a driver manager and database-specific drivers to provide
transparent connectivity to heterogeneous databases.
The JDBC API is defined in java.sql package and it consists of the following
interfaces or classes:
Figure: JDBC Architecture
DriverManager:- The JDBC driver manager ensures that the correct driver is
used to access each data source.
Driver: A JDBC driver is required to process the SQL requests and generate
results. There are 4 types of JDBC drivers as specified by Sun Microsystems:
1. JDBC-ODBC bridge driver
2. Native-API driver (partially java driver)
3. Network Protocol driver (fully java driver)
4. Thin driver (Pure java driver)
Statement:- Statement object is used to execute query and also store it's value to
"Resultset" object.
ResultSet:- Resultset object is used to store the result retrieve from database
using Statement, PreparedStatement etc.
Database URL: a string that contains information about the database to connect
to and other configuration properties. This string has its own format and is varied
among different databases.
Following are the examples of JDBC URL for MySQL and Oracle database:
1. MySQL
Examples:
jdbc:mysql://localhost:3306/world
jdbc:mysql://localhost:3306/world?user=root&password=socet
2. Oracle
jdbc:oracle:<drivertype>:@<database>
jdbc:oracle:<drivertype>:<user>/<password>@<database>
Examples:
jdbc:oracle:thin:@localhost:1521:test
jdbc:oracle:thin:root/socet@localhost:1521:test