DT Lab Manual
DT Lab Manual
REG. NO :
SEMESTER - II
April– 2022
DEPARTMENT OF COMPUTER APPLICATIONS
(Recognized under the section 2(f) & 12(B) by UGC Act 1956)
PERAMBALUR-621 212.
DEPARTMENT OF COMPUTER APPLICATIONS
CERTIFICATE
SUBMITTED BY
NAME : ________________________
REG.NO : ________________________
CLASS : ________________________
TITLE : ________________________
SEMESTER : ______________________
EXAMINERS:
1. 2.
INDEX
Page
S.No Date Experiment Signature
No
1. Remote Method Invocation
//Interface Program
import java.rmi.*;
public interface AddRem extends Remote
{
public int addNum(int a,int b) throws RemoteException;
}
//Implementation Program
import java.rmi.*;
import java.rmi.server.*;
public class AddRemImpl extends UnicastRemoteObject implements AddRem
{
public AddRemImpl() throws RemoteException{}
public int addNum(int a,int b)
{
return(a+b);
}
}
//Client Program
import java.rmi.*;
import java.net.*;
import java.io.*;
import java.util.*;
public class AddClient
{
public static void main(String args[])
{
String host="localhost";
Scanner sc=new Scanner(System.in);
System.out.println("Enter First Value");
int a=sc.nextInt();
System.out.println("Enter Second Value");
int b=sc.nextInt();
try{
AddRem remobj=
(AddRem)Naming.lookup("rmi://"+host+"/AddRem");
System.out.println(remobj.addNum(a,b));
}
catch(Exception e) {
System.out.println(e);
}
}
}
//Server Program
import java.rmi.*;
import java.rmi.registry.*;
import java.net.*;
public class AddServer {
public static void main(String args[]) {
try {
AddRemImpl locobj=new AddRemImpl();
Naming.rebind("rmi:///AddRem",locobj); //addService object
is hosted with name AddService
}
catch(Exception e) {
System.out.println(e);
}
}
}
OutPut:
2. Java RMI - Database Application
We created a sample RMI application where a client invokes a method which
displays a GUI window (JavaFX).
In this chapter, we will take an example to see how a client program can retrieve the
records of a table in MySQL database residing on the server.
Assume we have a table named student_data in the database details as shown below.
import java.sql.*;
import java.util.*;
// Implementing the remote interface
public class ImplExample implements Hello {
// Database credentials
String USER = "myuser";
String PASS = "password";
Connection conn = null;
Statement stmt = null;
//Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
//Open a connection
System.out.println("Connecting to a selected database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
System.out.println("Connected database successfully...");
//Execute a query
System.out.println("Creating statement...");
stmt = conn.createStatement();
String sql = "SELECT * FROM student_data";
ResultSet rs = stmt.executeQuery(sql);
//Extract data from result set
while(rs.next()) {
// Retrieve by column name
int id = rs.getInt("id");
String name = rs.getString("name");
String branch = rs.getString("branch");
int percent = rs.getInt("percentage");
String email = rs.getString("email");
// Setting the values
Student student = new Student();
student.setID(id);
student.setName(name);
student.setBranch(branch);
student.setPercent(percent);
student.setEmail(email);
list.add(student);
}
rs.close();
return list;
}}
Server Program
An RMI server program should implement the remote interface or extend the
implementation class. Here, we should create a remote object and bind it to the RMI
registry.
Following is the server program of this application. Here, we will extend the above
created class, create a remote object and register it to the RMI registry with the bind
name hello.
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class Server extends ImplExample {
public Server() {}
public static void main(String args[]) {
try {
// Instantiating the implementation class
ImplExample obj = new ImplExample();
// Exporting the object of implementation class (
here we are exporting the remote object to the stub)
Hello stub = (Hello) UnicastRemoteObject.exportObject(obj, 0);
// Binding the remote object (stub) in the registry
Registry registry = LocateRegistry.getRegistry();
registry.bind("Hello", stub);
System.err.println("Server ready");
} catch (Exception e) {
System.err.println("Server exception: " + e.toString());
e.printStackTrace();
}
}
}
Client Program
Following is the client program of this application. Here, we are fetching the remote
object and invoking the method named getStudents(). It retrieves the records of the
table from the list object and displays them.
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.*;
public class Client {
private Client() {}
public static void main(String[] args)throws Exception {
try {
// Getting the registry
Registry registry = LocateRegistry.getRegistry(null);
// Looking up the registry for the remote object
Hello stub = (Hello) registry.lookup("Hello");
// Calling the remote method using the obtained object
List<Student> list = (List)stub.getStudents();
for (Student s:list)v {
// System.out.println("bc "+s.getBranch());
System.out.println("ID: " + s.getId());
System.out.println("name: " + s.getName());
System.out.println("branch: " + s.getBranch());
System.out.println("percent: " + s.getPercent());
System.out.println("email: " + s.getEmail());
}
System.err.println("Client exception: " + e.toString());
e.printStackTrace();
}} }
// System.out.println(list);
} catch (Exception e) {
System.err.println("Client exception: " + e.toString());
e.printStackTrace(); } } }
3. Webpages using JSP Scriptlet
UserDetails.html
<html>
<title>Scriptlet Tag</title>
<head>
<style type="text/css">
h1 {
color: green;
text-align: center;
}
label {
font-size: 22px;
color: green;
}
</style>
</head>
<body>
<h1>Register User Details</h1>
<form action="User.jsp">
<table>
<tr>
<td><label for="register_name">Enter name:</label></td>
<td><input type="text" name="name" value="" id="register_name"
style="width: 160px" /></td>
</tr>
<tr>
<td><label for="register_password"">Enter password:</label></td>
<td><input type="password" name="password"
id="register_password" style="width: 160px" /></td>
</tr>
<tr>
<td><label for="register_email">Enter Email:</label></td>
<td><input type="email" name="email" value=""
id="register_email" style="width: 160px" /></td>
</tr>
<tr>
<td><label for="register_gender">Enter Gender:</label></td>
<td><input type="radio" name="gender" value="male" /><label
for="register_gendermale">male</label><input type="radio"
name="gender" id="register_genderfemale" value="female" /><label
for="register_genderfemale">female</label></td>
</tr>
<tr>
<td><label for="register_country">Select Country:</label></td>
<td><select name="country" style="width: 160px">
<option value="india">India</option>
<option value="pakistan">US</option>
<option value="africa">Brazil</option>
<option value="china">England</option>
<option value="other">Italy</option>
<option value="other">Others</option>
</select></td>
</tr>
<tr>
<td colspan="2"><div align="right">
<input type="submit" id="register_0" value="Sign Up" />
</div></td>
</tr>
</table>
</form>
</body>
</html>
User.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<!-- CSS Styles -->
<style type="text/css">
h1 {
color: red;
text-align: center;
}
p{
color: green;
font-size: 25px;
border: solid 3px blue;
}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSP Scriptlet Tag</title>
</head>
<body>
<h1>Well Come to My Application Form</h1>
<p>
<!--scriptlet tag -->
<%
//getting all HTML parameters
String name = request.getParameter("name");
String email = request.getParameter("email");
String gender = request.getParameter("gender");
String country = request.getParameter("country");
//out is implicit object of JSP
out.print("My Name is " + name+"<br>");
out.print("My Email is " + email+"<br>");
out.print("My Gender is " + gender+"<br>");
out.print("My Country is " + country);
%>
</p>
</body>
</html>
Output
4. Webpage using JSP of java beans
Test.java
package Action;
public class Test {
private String message = "No message specified";
public String getMessage() {
return (message);
}
public void setMessage(String message) {
this.message = message;
}
}
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Using JavaBeans in JSP</title>
</head>
<body>
<h2>Using JavaBeans in JSP</h2>
<jsp:useBean id="test" class="Action.Test" />
<jsp:setProperty name="test" property="message" value="Hello JSP..." />
<p>Got message....</p>
<jsp:getProperty name="test" property="message" />
</body>
</html>
Output:
5. Performing XML transformation using XML and XSL coding
XSLT stands for Extensible Stylesheet Language Transformation.
Table.xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="rule.css"?>
<tables>
<m:table xmlns:m=""http://www.google.co.in">
<m:tr>
<m:td>Apple</m:td>
<m:td>Banana</m:td>
</m:tr>
</m:table>
<n:table xmlns:m=""http://www.yahoo.co.in">
<n:height>100</n:height>
<n:width>150</n:width>
</n:table>
</tables>
Students.xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl "href="Rule.xsl" ?>
<student>
<s>
<name> Divyank Singh Sikarwar </name>
<branch> CSE</branch>
<age>18</age>
<city> Agra </city>
</s>
<s>
<name> Aniket Chauhan </name>
<branch> CSE</branch>
<age> 20</age>
<city> Shahjahanpur </city>
</s>
<s>
<name> Simran Agarwal</name>
<branch> CSE</branch>
<age> 23</age>
<city> Buland Shar</city>
</s>
<s>
<name> Abhay Chauhan</name>
<branch> CSE</branch>
<age> 17</age>
<city> Shahjahanpur</city>
</s>
<s>
<name> Himanshu Bhatia</name>
<branch> IT</branch>
<age> 25</age>
<city> Indore</city>
</s>
</student>
Rule.xsl
Output :
6. Create XML Schema
employee.xsd
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.javatpoint.com"
xmlns="http://www.javatpoint.com"
elementFormDefault="qualified">
<xs:element name="employee">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
<xs:element name="email" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
employee.xml
<?xml version="1.0"?>
<employee
xmlns="http://www.javatpoint.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.javatpoint.com employee.xsd">
<firstname>vimal</firstname>
<lastname>jaiswal</lastname>
<email>[email protected]</email>
</employee>
7. Creation of a table and insertion of a few records using Disconnected Access.
Create Form:
Imports System.Data.OleDb
Public Class mobiles
Dim con As OleDbConnection
Dim cmd As OleDbCommand
Dim dr As OleDbDataReader
Catch ex As Exception
MsgBox(Err.Description)
End Try
TextBox1.Text = ""
ComboBox1.Text = ""
TextBox3.Text = ""
End Sub
End Class
Imports System.Data.OleDb
Public Class mobsale
Dim con As OleDbConnection
Dim cmd, cmd1, cmd2, cmd3 As OleDbCommand
Dim dr, dr1, dr2 As OleDbDataReader
Private Sub FindBtn_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
Dim st2, a As String
Try
con = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source= azhar.mdb")
con.Open()
a = InputBox("Enter the mobile no")
st2 = "select * from MO1 where MONO='" & Trim(a) & "'"
cmd1 = New OleDbCommand(st2, con)
dr1 = cmd1.ExecuteReader
If dr1.Read Then
TextBox1.Text = dr1(0)
ComboBox1.Text = dr1(1)
TextBox3.Text = dr1(2)
Else
MsgBox("not found")
End If
Catch ex As Exception
MsgBox(Err.Description)
End Try
End Sub
Imports System.Data.OleDb
Public Class mobilesalrpt
Dim con As OleDbConnection
Dim cmd, cmd1 As OleDbCommand
Dim dr, dr1 As OleDbDataReader
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim st, a As String
Try
con = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source= d:\ex1\azhar.mdb")
con.Open()
a = InputBox("Enter the Date")
st = "select * from mosale where date='" & Trim(a) & "'"
DataGridView1.Show()
DataGridView1.RowCount
cmd1 = New OleDbCommand(st, con)
dr = cmd1.ExecuteReader()
DataGridView1.ColumnCount = 5
While (dr.Read)
DataGridView1.Rows.Add(dr(0), dr(1), dr(2), dr(3), dr(4))
End While
con.Close()
Catch ex As Exception
MsgBox(Err.Description)
End Try
End Sub
End Class
9. Generation of a crystal report from an existing database.
Add the Crystal Report from toolbox
NavigateUrl>http://www.Mindcracker.com/Registration/Register.aspx</Navigat
eUrl>
<AlternateText>Advertisement</AlternateText>
<Impressions>100</Impressions>
<Keyword>Header</Keyword>
<Category>Script</Category>
</Ad>
<Ad>
<ImageUrl>~/Banners/468X60_Consultant.gif</ImageUrl>
<NavigateUrl>http://www.mindcracker.com/Consultants/</NavigateUrl>
<AlternateText>Advertisement</AlternateText>
<Impressions>100</Impressions>
<Keyword>Header</Keyword>
<Category>Script</Category>
</Ad>
<Ad>
<ImageUrl>~/Banners/468X60_Employer.gif</ImageUrl>
<NavigateUrl>http://www.Mindcracker.com/Registration/Register.aspx</Naviga
teUrl>
<AlternateText>Advertisement</AlternateText>
<Impressions>100</Impressions>
<Keyword>Header</Keyword>
<Category>Script</Category>
</Ad>
<Ad>
<ImageUrl>~/Banners/468X60_Jobseeker.gif</ImageUrl>
<NavigateUrl>http://www.mindcracker.com/Jobs/</NavigateUrl>
<AlternateText>Advertisement</AlternateText>
<Impressions>100</Impressions>
<Keyword>Header</Keyword>
<Category>Script</Category>
</Ad>
<Ad>
<ImageUrl>~/Banners/468X60_Recruiter.gif</ImageUrl>
<NavigateUrl>http://www.Mindcracker.com/Registration/Register.aspx</Naviga
teUrl>
<AlternateText>Advertisement</AlternateText>
<Impressions>100</Impressions>
<Keyword>Header</Keyword>
<Category>Script</Category>
</Ad>
</Advertisements>
After refreshing page then you will see a different image. The images are loaded
randomly.